-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement accessible flyout menu and submenus
- focus popper's first child when rendered - focus flyout menu when rendered - close flyout menu with Escape key - add unit tests simulating opening and closing of submenus
- Loading branch information
Showing
5 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
components/menu/src/flyout-menu/__tests__/flyout-menu.test.js
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { render } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import React from 'react' | ||
import { MenuItem } from '../../menu-item/menu-item.js' | ||
import { FlyoutMenu } from '../flyout-menu.js' | ||
|
||
describe('Flyout Menu Component', () => { | ||
it('can handle navigation of submenus', () => { | ||
const { getByText, queryByText, getAllByRole } = render( | ||
<FlyoutMenu> | ||
<MenuItem label="Item 1" /> | ||
<MenuItem label="Item 2"> | ||
<MenuItem label="Item 2 a" /> | ||
</MenuItem> | ||
</FlyoutMenu> | ||
) | ||
|
||
const itemOne = getByText(/Item 1/i) | ||
const itemTwo = getByText(/Item 2/i) | ||
let submenuChild = queryByText(/Item 2 a/i) | ||
|
||
const menuItems = getAllByRole('menuitem') | ||
|
||
expect(menuItems.length).toBe(2) | ||
expect(menuItems[0]).toBe(itemOne.parentNode) | ||
expect(menuItems[1]).toBe(itemTwo.parentNode) | ||
|
||
expect(submenuChild).not.toBeInTheDocument() | ||
|
||
userEvent.tab() | ||
expect(menuItems[0].parentNode).toHaveFocus() | ||
expect(menuItems[1].parentNode).not.toHaveFocus() | ||
|
||
userEvent.keyboard('{ArrowDown}') | ||
expect(menuItems[0].parentNode).not.toHaveFocus() | ||
expect(menuItems[1].parentNode).toHaveFocus() | ||
|
||
userEvent.keyboard('{ArrowRight}') | ||
submenuChild = getByText(/Item 2 a/i) | ||
|
||
expect(submenuChild).toBeInTheDocument() | ||
expect(submenuChild.parentElement.parentElement).toHaveFocus() | ||
|
||
userEvent.keyboard('{ArrowLeft}') | ||
expect(queryByText(/Item 2 a/i)).not.toBeInTheDocument() | ||
expect(menuItems[1].parentNode).toHaveFocus() | ||
}) | ||
}) |
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