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
First, we need to create a User Event object instance from `react-native-testing-library` in order to be able to trigger user events.
223
+
When writing tests containing navigation with animations, you need to wait until animations finish before proceeding further. To do so, you have to use **fake timers**. [`Fake Timers`](https://jestjs.io/docs/timer-mocks) replace real implementation of the native timer functions (e.g. `setTimeout()`, `setInterval()`) with a custom implementation that uses a fake clock. They allow you to instantly skip animation time using `jest.runAllTimers()`. To avoid getting state change error, wrap `runAllTimers` in `act`.
224
+
225
+
```js
226
+
// Enable fake timers
227
+
jest.useFakeTimers();
228
+
229
+
// ...
230
+
231
+
// Wrap jest.runAllTimers in act to prevent state change error
232
+
// Skip all timers including animations
233
+
act(() =>jest.runAllTimers());
234
+
```
235
+
236
+
Even though `BottomTabNavigator` by default does not use any animations, some components of the tab bar do so. If you don't use fake timers in this example, the tests might still pass but you will get a warning from `react-native-tesing-library` about React state updates.
237
+
238
+
After we setup fake timers, we need to create a User Event object instance from `react-native-testing-library` in order to be able to trigger user events.
220
239
221
240
```js
222
241
// Create User Event object instance
223
242
constuser=userEvent.setup();
224
243
```
225
244
226
-
After we create and render our tabs, we get the settings tab bar button using an accessibility label assigned to it and press it using `user.press(button)`.
245
+
After we create and render our tabs, we get the settings tab bar button using an accessibility label assigned to it and press it using `user.press(button)`. We use fake timers to skip the animations.
227
246
228
247
```js
229
248
// Get the setting tab bar button
230
249
constbutton=screen.getByRole('button', { name:'Settings, tab, 2 of 2' });
231
250
232
251
// Simulate user pressing the button
233
252
awaituser.press(button);
253
+
254
+
// Skip tab bar animations
255
+
act(() =>jest.runAllTimers());
234
256
```
235
257
236
258
We expect that after pressing the button, the screen will change and `'Settings screen'` will be visible.
@@ -399,23 +421,6 @@ test('surprise text appears after transition to surprise screen is complete', as
399
421
400
422
We press the "Click here!" button using `user.press()` and check that the text does not appear right away but only after the transition between screens ends.
401
423
402
-
<!-- When writing tests containing navigation with animations (in this example we have a `StackNavigator`, which uses an animation for the transition based on the platform and OS version) you need to wait until animations finish before proceeding further. To do so, you have to use `fake timers`. [`Fake Timers`](https://jestjs.io/docs/timer-mocks) replace real implementation of times function to use fake clock. They allow you to instantly skip animation time. To avoid getting state change error, wrap `runAllTimers` in `act`.
403
-
404
-
```js
405
-
// Enable fake timers
406
-
jest.useFakeTimers();
407
-
408
-
// ...
409
-
410
-
// Wrap jest.runAllTimers in act to prevent state change error
411
-
// Skip all timers including animations
412
-
act(() => jest.runAllTimers());
413
-
```
414
-
415
-
If we hadn't used fake timers in this example, the test would have failed.
416
-
417
-
In the previous example we didn't use fake timers because `BottomTabNavigator` by default does not use any transition animations. -->
418
-
419
424
### Example 3 - Enforce navigator state in response to navigation event
420
425
421
426
Display settings screen after settings tab bar button is pressed.
@@ -567,7 +572,7 @@ export function MyTabs() {
567
572
```js
568
573
import { expect, jest, test } from'@jest/globals';
@@ -649,8 +662,6 @@ test('displays settings screen after settings tab bar button press', async () =>
649
662
650
663
We get tab bar buttons, press them and check if rendered screens are correct.
651
664
652
-
<!-- In this example, we don't need to use fake timers because text from the next screen is available using `getByText` even before the animation ends. -->
653
-
654
665
### Example 4 - `useFocusEffect` hook and data fetching
655
666
656
667
On profile screen focus, display loading state while waiting for data and then show fetched profile on every refocus.
@@ -884,6 +895,8 @@ test('on profile screen focus, displays loading state while waiting for data and
0 commit comments