Skip to content

Commit

Permalink
Align how we access the menu element with the checkboxes and radios
Browse files Browse the repository at this point in the history
See: 266f877
  • Loading branch information
romaricpascal committed Sep 22, 2023
1 parent 9b7b370 commit d41d913
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/govuk-frontend/src/govuk/components/header/header.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,16 @@ export class Header extends GOVUKFrontendComponent {
return this
}

const $menu = $module.querySelector(
`#${$menuButton.getAttribute('aria-controls')}`
)
const menuId = $menuButton.getAttribute('aria-controls')
if (!menuId) {
throw new ElementError($menuButton, {
componentName: 'Header',
identifier: '$menuButton["aria-controls"]',
expectedType: 'string'
})
}

const $menu = document.getElementById(menuId)

if (!($menuButton instanceof HTMLElement)) {
throw new ElementError($menu, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@ describe('Header navigation', () => {
})
})

it("throws when the toggle's aria-control attribute is missing", async () => {
await expect(
renderAndInitialise(page, 'header', {
params: examples['with navigation'],
beforeInitialisation($module) {
$module
.querySelector('.govuk-js-header-toggle')
.removeAttribute('aria-controls')
}
})
).rejects.toEqual({
name: 'ElementError',
message:
'Header: $menuButton["aria-controls"] is not of type "string"'
})
})

it('throws when the menu is missing, but a toggle is present', async () => {
await expect(
renderAndInitialise(page, 'header', {
Expand Down

0 comments on commit d41d913

Please sign in to comment.