General setup
Table features long press interactions on its rows depending on the row actions provided and if the user is interacting with the table 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 table 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 Table tester in your test cases. This gives you access to Table 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 Table tester, use it to simulate row selection, and verify the table's state after each interaction.
// Table.test.ts
import {render, within} from '@testing-library/react';
import {User} from '@react-aria/test-utils';
let testUtilUser = new User({interactionType: 'mouse', advanceTimer: jest.advanceTimersByTime});
// ...
it('Table can toggle row selection', async function () {
// Render your test component/app and initialize the table tester
let {getByTestId} = render(
<Table data-testid="test-table" selectionMode="multiple">
...
</Table>
);
let tableTester = testUtilUser.createTester('Table', {root: getByTestId('test-table')});
expect(tableTester.selectedRows).toHaveLength(0);
await tableTester.toggleSelectAll();
expect(tableTester.selectedRows).toHaveLength(10);
await tableTester.toggleRowSelection({row: 2});
expect(tableTester.selectedRows).toHaveLength(9);
let checkbox = within(tableTester.rows[2]).getByRole('checkbox');
expect(checkbox).not.toBeChecked();
await tableTester.toggleSelectAll();
expect(tableTester.selectedRows).toHaveLength(10);
expect(checkbox).toBeChecked();
await tableTester.toggleSelectAll();
expect(tableTester.selectedRows).toHaveLength(0);
});
See below for the full definition of the User and the Table 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 | |
|---|---|---|
rowHeaders | HTMLElement | |
| Returns the row headers within the table if any. | ||
selectedRows | HTMLElement | |
| Returns the currently selected rows within the table if any. | ||
rows | HTMLElement | |
| Returns the rows within the table if any. | ||
columns | HTMLElement | |
| Returns the columns within the table. | ||
rowGroups | HTMLElement | |
| Returns the row groups within the table. | ||
table | HTMLElement | |
| Returns the table. | ||
Methods
constructor | ||
setInteractionType | ||
| Set the interaction type used by the table tester. | ||
toggleRowSelection | ||
| Toggles the selection for the specified table row. Defaults to using the interaction type set on the table tester. | ||
toggleSort | ||
| Toggles the sort order for the specified table column. Defaults to using the interaction type set on the table tester. | ||
triggerColumnHeaderAction | ||
| Triggers an action for the specified table column menu. Defaults to using the interaction type set on the table tester. | ||
triggerRowAction | ||
| Triggers the action for the specified table row. Defaults to using the interaction type set on the table tester. | ||
toggleSelectAll | ||
| Toggle selection for all rows in the table. Defaults to using the interaction type set on the table tester. | ||
findRow | ||
| Returns a row matching the specified index or text content. | ||
findCell | ||
| Returns a cell matching the specified text content. | ||
cells | ||
Returns the cells within the table if any. Can be filtered against a specific row if provided via element. | ||