Skip to content

Commit

Permalink
Fix accessibility tests for examples without JS enhancements
Browse files Browse the repository at this point in the history
The `render` helper assumed all examples of a component for which
a JavaScript component exist would have a `data-module` and as such
forced initialising a JavaScript component when no element with the appropriate
`data-module` was found on the page.

This behaviour is handy to spot if a component suddenly stops rendering `data-module`,
so we don't want to change the `render` function. For the File Upload,
we can decide whether the error is relevant in the test, based on whether the `javascript`
macro option is set.
  • Loading branch information
romaricpascal committed Jan 24, 2025
1 parent a0bcc00 commit 59da920
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,37 @@ describe('/components/file-upload', () => {
const examples = await getExamples('file-upload')

for (const exampleName in examples) {
await render(page, 'file-upload', examples[exampleName])
// JavaScript enhancements being optional, some examples will not have
// any element with `data-module="govuk-file-upload"`. This causes an error
// as `render` assumes that if a component is exported by GOV.UK Frontend
// its rendered markup will have a `data-module` and tried to initialise
// the JavaScript component, even if no element with the right `data-module`
// is on the page.
//
// Because of this, we need to filter `ElementError` thrown by the JavaScript
// component to examples that actually run the JavaScript enhancements
try {
await render(page, 'file-upload', examples[exampleName])
} catch (e) {
const macroOptions = /** @type {MacroOptions} */ (
examples[exampleName].context
)

const exampleUsesJavaScript = macroOptions.javascript
const exampleLackedRoot = e.message.includes(
'govuk-file-upload: Root element'
)

if (!exampleLackedRoot || exampleUsesJavaScript) {
throw e
}
}
await expect(axe(page)).resolves.toHaveNoViolations()
}
}, 120000)
})
})

/**
* @typedef {import('@govuk-frontend/lib/components').MacroOptions} MacroOptions
*/

0 comments on commit 59da920

Please sign in to comment.