Beta Preview

Testing Table

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.

Timers

Long press

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

NameTypeDefault
advanceTimer['advanceTimer']Default:
A function used by the test utils to advance timers during interactions. Required for certain aria patterns (e.g. table).
interactionType['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(opts: ): void
createTester<T extends >(patternName: T, opts: <T>): <T>
Creates an aria pattern tester, inheriting the options provided to the original user.

Properties

NameType
rowHeadersHTMLElement[]
Returns the row headers within the table if any.
selectedRowsHTMLElement[]
Returns the currently selected rows within the table if any.
rowsHTMLElement[]
Returns the rows within the table if any.
columnsHTMLElement[]
Returns the columns within the table.
rowGroupsHTMLElement[]
Returns the row groups within the table.
tableHTMLElement
Returns the table.

Methods

constructor(opts: ): void
setInteractionType(type: ['interactionType']): void
Set the interaction type used by the table tester.
toggleRowSelection(opts: ): Promise<void>
Toggles the selection for the specified table row. Defaults to using the interaction type set on the table tester.
toggleSort(opts: ): Promise<void>
Toggles the sort order for the specified table column. Defaults to using the interaction type set on the table tester.
triggerColumnHeaderAction(opts: ): Promise<void>
Triggers an action for the specified table column menu. Defaults to using the interaction type set on the table tester.
triggerRowAction(opts: ): Promise<void>
Triggers the action for the specified table row. Defaults to using the interaction type set on the table tester.
toggleSelectAll(opts: {
interactionType?: ['interactionType']
}): Promise<void>
Toggle selection for all rows in the table. Defaults to using the interaction type set on the table tester.
findRow(opts: {
rowIndexOrText: numberstring
}): HTMLElement
Returns a row matching the specified index or text content.
findCell(opts: {
text: string
}): HTMLElement
Returns a cell matching the specified text content.
cells(opts: {
element?: HTMLElement
}): HTMLElement[]
Returns the cells within the table if any. Can be filtered against a specific row if provided via element.

Testing FAQ