General setup
Table supports long press interactions on its rows in certain configurations. See the following sections on how to handle these behaviors in your tests.
Test utils
@react-aria/test-utils offers common table interaction testing utilities. Install it with your preferred package manager.
npm install @react-aria/test-utils --dev
Requirements
Please note that this library uses @testing-library/react@16 and @testing-library/user-event@14. This means that you need to be on React 18+ in order for these utilities to work.
Initialize a User object at the top of your test file, and use it to create a Table pattern tester in your test cases. The tester has methods that you can call within your test to query for specific subcomponents or simulate common interactions.
// 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);
});
API
User
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. | ||
TableTester
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. | ||