Testing Menu
General setup
Menu features long press interactions depending on the actions provided and if the user is interacting with the menu on a touch device. Please see the following sections in the general testing documentation for more information on how to handle these behaviors in your test suite.
Test utils
@react-aria/test-utils offers common menu interaction utilities which you may find helpful when writing tests. To install, simply
add it to your dev dependencies via your preferred package manager.
Once installed, you can access the User that @react-aria/test-utils provides in your test file as shown below. This user only needs to be initialized once and then can be used to generate
the Menu tester in your test cases. This gives you access to Menu specific utilities that you can then call within your test to query for specific subcomponents or simulate common interactions.
The example test case below shows how you might go about setting up the Menu tester, use it to find and open a submenu, and verify the menu's state after each interaction.
// Menu.test.ts
import {render} from '@testing-library/react';
import {User} from '@react-aria/test-utils';
let testUtilUser = new User({interactionType: 'mouse'});
// ...
it('Menu can open its submenu via keyboard', async function () {
// Render your test component/app and initialize the menu tester
let {getByTestId} = render(
<MenuTrigger>
<Button data-testid="test-menutrigger">Menu trigger</Button>
...
</MenuTrigger>
);
let menuTester = testUtilUser.createTester('Menu', {root: getByTestId('test-menutrigger'), interactionType: 'keyboard'});
await menuTester.open();
expect(menuTester.menu).toBeInTheDocument();
let submenuTriggers = menuTester.submenuTriggers;
expect(submenuTriggers).toHaveLength(1);
let submenuTester = await menuTester.openSubmenu({submenuTrigger: 'Share…'});
expect(submenuTester.menu).toBeInTheDocument();
await submenuTester.selectOption({option: submenuTester.options()[0]});
expect(submenuTester.menu).not.toBeInTheDocument();
expect(menuTester.menu).not.toBeInTheDocument();
});
See below for the full definition of the User and the Menu tester.
Properties
| Name | Type | Default |
|---|---|---|
advanceTimer | UserOpts['advanceTimer'] | Default: — |
| A function used by the test utils to advance timers during interactions. Required for certain aria patterns (e.g. table). | ||
interactionType | UserOpts['interactionType'] | Default: mouse
|
| The interaction type (mouse, touch, keyboard) that the test util user will use when interacting with a component. This can be overridden at the aria pattern util level if needed. | ||
Methods
constructor | ||
createTester | ||
| Creates an aria pattern tester, inheriting the options provided to the original user. | ||
Properties
| Name | Type | |
|---|---|---|
submenuTriggers | HTMLElement | |
| Returns the menu's submenu triggers if any. | ||
sections | HTMLElement | |
| Returns the menu's sections if any. | ||
menu | HTMLElement | null | |
| Returns the menu if present. | ||
trigger | HTMLElement | |
| Returns the menu's trigger. | ||
Methods
constructor | ||
setInteractionType | ||
| Set the interaction type used by the menu tester. | ||
open | ||
| Opens the menu. Defaults to using the interaction type set on the menu tester. | ||
findOption | ||
| Returns a option matching the specified index or text content. | ||
selectOption | ||
| Selects the desired menu option. Defaults to using the interaction type set on the menu tester. If necessary, will open the menu dropdown beforehand. The desired option can be targeted via the option's node, the option's text, or the option's index. | ||
openSubmenu | ||
| Opens the submenu. Defaults to using the interaction type set on the menu tester. The submenu trigger can be targeted via the trigger's node or the trigger's text. | ||
close | ||
| Closes the menu. | ||
options | ||
Returns the menu's options if present. Can be filtered to a subsection of the menu if provided via element. | ||