You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Table` follows the [Collection Components API](collections.html?component=Table), accepting both static and dynamic collections.
94
+
`Table` follows the [Collection Components API](collections.html?component=Table), accepting both static and dynamic collections.
93
95
In this example, both the columns and the rows are provided to the table via a render function, enabling the user to hide and show columns and add additional rows.
94
96
95
97
```tsx render
@@ -707,3 +709,88 @@ function ReorderableTable() {
<Content>Please note that this library uses [@testing-library/react@16](https://www.npmjs.com/package/@testing-library/react) and [@testing-library/user-event@14](https://www.npmjs.com/package/@testing-library/user-event). This means that you need to be on React 18+ in order for these utilities to work.</Content>
739
+
</InlineAlert>
740
+
741
+
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
742
+
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.
743
+
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.
- When using the test utils, what if a certain interaction errors or doesn't seem to result in the expected state?
788
+
789
+
In cases like this, first double check your test setup and make sure that your test is rendering your table in its expected
790
+
state before the test util interaction call. If everything looks correct, you can always fall back to simulating interactions manually,
791
+
and using the test util to query your table's state post interaction.
792
+
793
+
- The tester doesn't offer a specific interaction flow, what should I do?
794
+
795
+
Whenever the table tester queries its rows/cells/etc or triggers a user flow, it does so against the current state of the table. Therefore the table test can be used alongside
Copy file name to clipboardExpand all lines: packages/dev/s2-docs/pages/react-aria/testing.mdx
+80Lines changed: 80 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,6 +99,86 @@ userEvent.tab();
99
99
userEvent.click(document.activeElement);
100
100
```
101
101
102
+
## Test setup and common gotchas
103
+
104
+
### Timers
105
+
106
+
If you are using fake timers in your test suite, be aware that you may need to advance your timers after various interactions. We have `requestAnimationFrame` calls in various underlying hooks that you will need to also handle by advancing your timers in the tests.
107
+
This happens most prominently in our collection components after selection. In Jest, this can be handled by calling `act(() => jest.runAllTimers());` but you may require more precise control
108
+
depending on the other time-sensitive behavior you are testing. Please see [Jest's timer docs](https://jestjs.io/docs/timer-mocks) or the equivalent docs of your test frameworks for more information on how to do so.
109
+
It is also a good idea to run all timers to completion after each test case to avoid any left over transitions or timeouts that a component may have setup during its lifecycle.
110
+
111
+
```tsx
112
+
afterEach(() => {
113
+
act(() =>jest.runAllTimers());
114
+
});
115
+
```
116
+
117
+
Consider adding a `act(() => jest.runAllTimers());` after your simulated user interaction if you run into a test failure that looks like the following:
118
+
119
+
```
120
+
TestingLibraryElementError: Unable to find an accessible element with the role "listbox"
121
+
```
122
+
123
+
If you are using real timers instead, you can await a particular state of your app to be reached. If you are using React Testing Library, you can perform a `waitFor` query
124
+
to wait for a dialog to appear:
125
+
126
+
```tsx
127
+
awaitwaitFor(() => {
128
+
expect(getByRole('dialog')).toBeInTheDocument();
129
+
});
130
+
```
131
+
132
+
### Simulating user long press
133
+
134
+
Some components like Menu support long press operations. Unfortunately, the approach of using the userEvent library to simulate a press event and running timers to hit the
135
+
long press internal timer threshold isn't sufficient due to `useLongPress`'s usage of `PointerEvent` and our own detection of `virtual` vs `mouse`/`touch` pointer types. Mock [PointerEvent](https://github.com/adobe/react-spectrum/blob/16ff0efac57eebeb1cd601ab376ce7c58a4e4efd/packages/dev/test-utils/src/events.ts#L70-L103)
136
+
globally and use `fireEvent` from `@testing-library/react` to properly simulate these long press events in your tests.
137
+
If you are using Jest, you can call our <TypeLinklinks={testUtilDocs.links}type={testUtilDocs.exports.installPointerEvent} /> utility to automatically set up and tear down this mock in your test.
138
+
Additionally, if you are using fake timers and don't need to control the specific timings around the long press interaction, feel free to use our <TypeLinklinks={testUtilDocs.links}type={testUtilDocs.exports.triggerLongPress} /> utility as shown below.
Components like ColorArea, ColorSlider, ColorWheel, and Slider each feature a draggable handle that a user can interact with to change the component's value. Similar to long press, the interactions offered by userEvent library aren't sufficient to trigger
160
+
the underlying event handlers governing these drag/move operations. [Mock MouseEvent globally](https://github.com/adobe/react-spectrum/blob/f40b575e38837e1aa7cabf0431406e81275d118a/packages/%40react-aria/test-utils/src/testSetup.ts#L16-L36) and `fireEvent` from `@testing-library/react` to simulate these drag/move events in your tests.
161
+
If you are using Jest, you can call our <TypeLinklinks={testUtilDocs.links}type={testUtilDocs.exports.installMouseEvent} /> utility to automatically set up and tear down this mock in your test. Additionally, the track dimensions
162
+
for the draggable handle should be mocked so that the move operation calculations can be properly computed.
0 commit comments