Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restores test and refactors component and test for layout-super-navigation-header #3939

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@
* Improve test coverage of contextual breadcrumb logic ([PR #3944](https://github.com/alphagov/govuk_publishing_components/pull/3944) and [PR #3945](https://github.com/alphagov/govuk_publishing_components/pull/3945))
* Remove GA4 callout tracking from the govspeak component ([PR #3946](https://github.com/alphagov/govuk_publishing_components/pull/3946))
* Remove GA4 callout tracking from notice & warning text components ([PR #3947](https://github.com/alphagov/govuk_publishing_components/pull/3947))
* Restores test and refactors component and test for layout-super-navigation-header ([PR #3939](https://github.com/alphagov/govuk_publishing_components/pull/3939))

## 37.9.1

Expand Down
Expand Up @@ -134,8 +134,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
var $navMenuLinks = this.$navMenu.querySelectorAll('li a')
var $firstNavLink = $navMenuLinks[0]
var $lastNavLink = $navMenuLinks[$navMenuLinks.length - 1]
var $searchMenuLinks = this.$searchMenu.querySelectorAll('li a')
var $lastSearchLink = $searchMenuLinks[$searchMenuLinks.length - 1]
var $searchMenuTabbable = this.$searchMenu.querySelectorAll('li a, input, button')
var $lastSearchMenuTabbable = $searchMenuTabbable[$searchMenuTabbable.length - 1]

if (event.keyCode === KEY_TAB) {
if (!this.$navMenu.hasAttribute('hidden')) {
Expand Down Expand Up @@ -169,7 +169,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
break
}
} else if (!this.$searchMenu.hasAttribute('hidden')) {
if (document.activeElement === $lastSearchLink) {
if (document.activeElement === $lastSearchMenuTabbable) {
if (!event.shiftKey) {
hide(this.$searchToggle, this.$searchMenu)
}
Expand Down
25 changes: 21 additions & 4 deletions spec/javascripts/components/layout-super-navigation-header-spec.js
Expand Up @@ -328,23 +328,29 @@ describe('The super header navigation', function () {
var $navLinks
var $firstNavLink
var $lastNavLink
var $searchMenu
var $searchMenuTabbable
var $lastSearchMenuTabbable

beforeEach(function () {
thisModule.init()
$navMenuButton = document.querySelector('#super-navigation-menu-toggle')
$searchMenuButton = document.querySelector('#super-search-menu-toggle')
$navMenu = document.querySelector('#super-navigation-menu')
$navLinks = $navMenu.querySelectorAll('li')
$firstNavLink = $navLinks[0].querySelector('a')
$lastNavLink = $navLinks[$navLinks.length - 1].querySelector('a')
$navLinks = $navMenu.querySelectorAll('li a')
$firstNavLink = $navLinks[0]
$lastNavLink = $navLinks[$navLinks.length - 1]
$searchMenu = document.querySelector('#super-search-menu')
$searchMenuTabbable = $searchMenu.querySelectorAll('li a, input, button')
$lastSearchMenuTabbable = $searchMenuTabbable[$searchMenuTabbable.length - 1]
})

it('when the Menu button is focussed, the nav menu is open and the tab key is pressed focus moves to the first nav menu link', function () {
$navMenu.removeAttribute('hidden')
$navMenuButton.focus()
window.GOVUK.triggerEvent($navMenuButton, 'keydown', { keyCode: 9, cancelable: true })

expect(document.activeElement).toEqual($navLinks[0].querySelector('a'))
expect(document.activeElement).toEqual($navLinks[0])
})

it('when the last nav menu link is focussed and the tab key is pressed focus moves to the search button and the nav menu is closed', function () {
Expand Down Expand Up @@ -374,6 +380,17 @@ describe('The super header navigation', function () {

expect(document.activeElement).toEqual($lastNavLink)
})

it('when the last tabbable element in the search menu is focussed and the tab key is pressed the search menu is closed', function () {
$searchMenu.removeAttribute('hidden')
$lastSearchMenuTabbable.focus()
window.GOVUK.triggerEvent($lastSearchMenuTabbable, 'keydown', { keyCode: 9, cancelable: true })

expect($searchMenu.hasAttribute('hidden')).toEqual(true)
expect($searchMenuButton.getAttribute('aria-expanded')).toEqual('false')
expect($searchMenuButton.getAttribute('aria-label')).toEqual('Show search menu')
expect($searchMenuButton.classList).not.toContain('gem-c-layout-super-navigation-header__open-button')
})
})

describe('escape key', function () {
Expand Down