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
If you're noticing that components are not being found on a list, even after mocking a scroll event, try changing the [`initialNumToRender`](https://reactnative.dev/docs/flatlist#initialnumtorender) that you have set. If you aren't comfortable changing the code to accept this prop from the unit test, try using an e2e test that might better suit what use case you're attempting to replicate.
438
-
:::
439
-
440
399
## Helper functions
441
400
442
401
### `waitFor`
@@ -469,7 +428,7 @@ await waitFor(() => false);
469
428
470
429
```jsx
471
430
// ❌ missing `await`: `waitFor` will just return Promise that will be rejected when the timeout is reached
472
-
waitFor(() =>expect(1).toBe(2))
431
+
waitFor(() =>expect(1).toBe(2));
473
432
```
474
433
475
434
:::note
@@ -481,9 +440,9 @@ Since `waitFor` is likely to run `expectation` callback multiple times, it is hi
481
440
```jsx
482
441
awaitwaitFor(() => {
483
442
// ❌ button will be pressed on each waitFor iteration
484
-
fireEvent.press(screen.getByText('press me'))
485
-
expect(mockOnPress).toHaveBeenCalled()
486
-
})
443
+
fireEvent.press(screen.getByText('press me'));
444
+
expect(mockOnPress).toHaveBeenCalled();
445
+
});
487
446
```
488
447
489
448
:::note
@@ -495,7 +454,7 @@ It is also recommended to have a [single assertion per each `waitFor`](https://k
495
454
#### Using a React Native version < 0.71 with Jest fake timers
496
455
497
456
:::caution
498
-
When using a version of React Native < 0.71 and modern fake timers (the default for `Jest` >= 27), `waitFor` won't work (it will always timeout even if `expectation()` doesn't throw) unless you use the custom [@testing-library/react-native preset](https://github.com/callstack/react-native-testing-library#custom-jest-preset).
457
+
When using a version of React Native < 0.71 and modern fake timers (the default for `Jest` >= 27), `waitFor` won't work (it will always timeout even if `expectation()` doesn't throw) unless you use the custom [@testing-library/react-native preset](https://github.com/callstack/react-native-testing-library#custom-jest-preset).
499
458
:::
500
459
501
460
`waitFor` checks whether Jest fake timers are enabled and adapts its behavior in such case. The following snippet is a simplified version of how it behaves when fake timers are enabled:
@@ -504,7 +463,7 @@ When using a version of React Native < 0.71 and modern fake timers (the default
In the following example we test that a function is called after 10 seconds using fake timers. Since we're using fake timers, the test won't depend on real time passing and thus be much faster and more reliable. Also we don't have to advance fake timers through Jest fake timers API because `waitFor` already does this for us.
481
+
In the following example we test that a function is called after 10 seconds using fake timers. Since we're using fake timers, the test won't depend on real time passing and thus be much faster and more reliable. Also we don't have to advance fake timers through Jest fake timers API because `waitFor` already does this for us.
- queries scoped to a single item inside a FlatList containing many items
616
577
- queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation)
617
578
618
-
619
579
### `act`
620
580
621
581
Useful function to help testing components that use hooks API. By default any `render`, `update`, `fireEvent`, and `waitFor` calls are wrapped by this function, so there is no need to wrap it manually. This method is re-exported from [`react-test-renderer`](https://github.com/facebook/react/blob/main/packages/react-test-renderer/src/ReactTestRenderer.js#L567]).
0 commit comments