@react-aria/test-utils
v1.0.0-rc.0
Published
Testing utils for react-aria patterns
Readme
@react-aria/test-utils
This package is part of react-spectrum. See the repo for more details.
See the React Aria testing docs for usage.
@react-aria/test-utils is a set of testing utilities that aims to make writing unit tests easier for consumers of React Aria or for users who have built their own components following the respective ARIA pattern specification.
Requirements: This library uses @testing-library/dom@10 and @testing-library/user-event@14. You need to be on React 18+ for these utilities to work.
Installation
npm install @react-aria/test-utils --devSetup
Initialize a User object at the top of your test file, and use it to create an ARIA 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.
// YourTest.test.ts
import {screen} from '@testing-library/react';
import {User} from '@react-aria/test-utils';
// Provide whatever method of advancing timers you use in your test, this example assumes Jest with fake timers.
// 'interactionType' specifies what mode of interaction should be simulated by the tester
// 'advanceTimer' is used by the tester to advance the timers in the tests for specific interactions (e.g. long press)
let testUtilUser = new User({interactionType: 'mouse', advanceTimer: jest.advanceTimersByTime});
// ...
it('my test case', async function () {
// Render your test component/app
render();
// Initialize the table tester via providing the 'Table' pattern name and the root element of said table
let table = testUtilUser.createTester('Table', {root: screen.getByTestId('test_table')});
// ...
});User API
class User {
constructor(opts?: {
interactionType?: 'mouse' | 'keyboard' | 'touch',
advanceTimer?: (time?: number) => void | Promise<unknown>
});
createTester(patternName, opts): PatternTester;
}interactionType— default modality used by testers created from thisUser. Individual testers can override this viasetInteractionTypeor per-method options.advanceTimer— used by testers to advance timers for interactions like long press. Passjest.advanceTimersByTime(or your test framework's equivalent) when using fake timers.createTester(patternName, opts)— returns a tester for the given ARIA pattern.opts.rootis the root element of the component under test.
Patterns
Below is a list of the ARIA patterns supported by createTester. See the accompanying component testing docs pages on the React Aria docs site for sample usage of each tester in a test suite.
