Skip to content

Commit

Permalink
Add component name to [data-module] selector
Browse files Browse the repository at this point in the history
For example, test HTML with multiple components will only match the one being tested
  • Loading branch information
colinrotherham committed Oct 13, 2023
1 parent 4ff6de6 commit e9842b7
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions shared/helpers/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,22 @@ async function renderAndInitialise(
) {
await goTo(page, '/tests/boilerplate')

const html = renderComponent(componentName, renderOptions)
const exportName = componentNameToClassName(componentName)
const selector = `[data-module="govuk-${componentName}"]`

// Inject rendered HTML into the page
await page.$eval(
'#slot',
(slot, htmlForSlot) => {
slot.innerHTML = htmlForSlot
},
html
'#slot', // See boilerplate.njk `<div id="slot">`
(slot, html) => (slot.innerHTML = html),
renderComponent(componentName, renderOptions)
)

// Call `beforeInitialisation` in a separate `$eval` call
// as running it inside the body of the next `evaluate`
// didn't provide a reliable execution
if (browserOptions?.beforeInitialisation) {
await page.$eval(
'[data-module]',
selector,
browserOptions.beforeInitialisation,
browserOptions.context
)
Expand All @@ -118,11 +117,11 @@ async function renderAndInitialise(
// gather and `return` the values we need from inside the browser, and throw
// them when back in Jest (to keep them triggering a Promise rejection)
const error = await page.evaluate(
async (exportName, config) => {
async (selector, exportName, config) => {
const namespace = await import('govuk-frontend')

// Find all matching modules
const $modules = document.querySelectorAll('[data-module]')
const $modules = document.querySelectorAll(selector)

try {
// Loop and initialise all $modules or use default
Expand All @@ -134,7 +133,8 @@ async function renderAndInitialise(
return { name, message }
}
},
componentNameToClassName(componentName),
selector,
exportName,
browserOptions?.config
)

Expand Down

0 comments on commit e9842b7

Please sign in to comment.