-
Notifications
You must be signed in to change notification settings - Fork 742
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
Keyboard navigation in mobile web (Issue #2159) #2166
Merged
julianguyen
merged 10 commits into
ifmeorg:main
from
LMulvey:lmulvey/issue-2159-keyboard-navigation-in-mobile-web
Oct 13, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a58318e
feat: update testing-library/jest-dom + testing-library/react + testi…
LMulvey 5467b7b
feat(Header): improve keyboard accessibility with Header
LMulvey 213746c
feat(Header): Trap focus on mobile navbar while it is open
LMulvey 52bb84b
test: update tests to use new @testing-library/user-event API
LMulvey 4c11f3e
chore: properly lint code and update to please the linter
LMulvey f8b591b
fix: change desktop displayLinks to use short-circuit evaluation
LMulvey b148a74
feat: lift up useFocusTrap hook for Modal and Header + fix Header foc…
LMulvey 3be61b1
fix(useFocusTrap): update filtering strategy to only filter display: …
LMulvey 699af39
test(InputTextarea*): remove use of Promise.all and replace with forE…
LMulvey 15dc31d
test(InputSwitch): update test and resolve Todo regarding use of RTL …
LMulvey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,13 @@ import { render, screen } from '@testing-library/react'; | |
import userEvent from '@testing-library/user-event'; | ||
import Header from 'components/Header'; | ||
|
||
function setup(jsx) { | ||
return { | ||
user: userEvent.setup(), | ||
...render(jsx), | ||
}; | ||
} | ||
|
||
const component = ( | ||
<Header | ||
home={{ name: 'Home', url: '/some-path' }} | ||
|
@@ -16,7 +23,7 @@ const component = ( | |
|
||
describe('Header', () => { | ||
it('renders correctly', () => { | ||
render(component); | ||
setup(component); | ||
|
||
expect(screen.getByRole('banner')).toBeInTheDocument(); | ||
expect(screen.getByRole('navigation')).toBeInTheDocument(); | ||
|
@@ -28,15 +35,59 @@ describe('Header', () => { | |
expect(screen.getByRole('link', { name: /link 2/i })).toBeInTheDocument(); | ||
}); | ||
|
||
it('toggles hamburger correctly', () => { | ||
const { container } = render(component); | ||
it('toggles hamburger correctly', async () => { | ||
const { container, user } = setup(component); | ||
const hamburger = container.querySelector('#headerHamburger'); | ||
userEvent.click(hamburger); | ||
await user.click(hamburger); | ||
|
||
const mobile = container.querySelector('#headerMobile'); | ||
expect(mobile).toBeInTheDocument(); | ||
|
||
userEvent.click(hamburger); | ||
await user.click(hamburger); | ||
expect(mobile).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('toggles hamburger from keyboard input when expected', async () => { | ||
const { container, user } = setup(component); | ||
const hamburger = container.querySelector('#headerHamburger'); | ||
|
||
await user.keyboard('{Tab}{Tab}'); | ||
expect(hamburger).toHaveFocus(); | ||
await user.keyboard('{Enter}'); | ||
expect(container.querySelector('#headerMobile')).toBeInTheDocument(); | ||
await user.keyboard('{Enter}'); | ||
expect(container.querySelector('#headerMobile')).not.toBeInTheDocument(); | ||
await user.keyboard('[Space]'); | ||
expect(container.querySelector('#headerMobile')).toBeInTheDocument(); | ||
await user.keyboard('[Space]'); | ||
expect(container.querySelector('#headerMobile')).not.toBeInTheDocument(); | ||
|
||
await user.keyboard('{Tab}'); | ||
expect(container.querySelector('#headerMobile')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('traps focus when the mobile navbar is open', async () => { | ||
const { container, user } = setup(component); | ||
const hamburger = container.querySelector('#headerHamburger'); | ||
|
||
await user.keyboard('{Tab}{Tab}'); | ||
expect(hamburger).toHaveFocus(); | ||
await user.keyboard('{Enter}'); | ||
expect(container.querySelector('#headerMobile')).toBeInTheDocument(); | ||
|
||
await user.keyboard('{Tab}'); | ||
expect(screen.getByRole('link', { name: /link 1/i })).toHaveFocus(); | ||
await user.keyboard('{Tab}'); | ||
expect(screen.getByRole('link', { name: /link 2/i })).toHaveFocus(); | ||
await user.keyboard('{Tab}'); | ||
expect(screen.getByRole('link', { name: /home/i })).toHaveFocus(); | ||
await user.keyboard('{Tab}'); | ||
expect(hamburger).toHaveFocus(); | ||
await user.keyboard('{Tab}'); | ||
expect(screen.getByRole('link', { name: /link 1/i })).toHaveFocus(); | ||
|
||
// Shift-tab back to the hamburger and close the mobile menu | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
await user.keyboard('{Shift>}{Tab}{/Shift}{Enter}'); | ||
expect(container.querySelector('#headerMobile')).not.toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain what exactly we're testing when we hit the
Space
key twice?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensuring that it opens AND closes the navbar with the space key!