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
Copy file name to clipboardExpand all lines: website/docs/14.x/cookbook/basics/async-events.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,18 @@
1
-
# Async tests
1
+
# Async Events
2
2
3
3
## Summary
4
4
5
-
Typically, you would write synchronous tests, as they are simple and get the work done. However, there are cases when using asynchronous (async) tests might be necessary or beneficial. The two most common cases are:
5
+
In RNTL v14, all testsare async since `render()`, `fireEvent()`, and other core APIs return Promises. Beyond the basic async APIs, there are additional async utilities for handling events that complete over time:
6
6
7
-
1.**Testing Code with asynchronous operations**: When your code relies on asynchronous operations, such as network calls or database queries, async tests are essential. Even though you should mock these network calls, the mock should act similarly to the actual behavior and hence by async.
8
-
2.**UserEvent API:** Using the [User Event API](docs/api/events/user-event) in your tests creates more realistic event handling. These interactions introduce delays (even though these are typically event-loop ticks with 0 ms delays), requiring async tests to handle the timing correctly.
7
+
1.**Waiting for elements to appear**: Use `findBy*` queries when elements appear after some delay (e.g., after data fetching).
8
+
2.**Waiting for conditions**: Use `waitFor()` to wait for arbitrary conditions to be met.
9
+
3.**Waiting for elements to disappear**: Use `waitForElementToBeRemoved()` when elements should be removed after some action.
9
10
10
-
Using async tests when needed ensures your tests are reliable and simulate real-world conditions accurately.
11
+
These utilities help you write reliable tests that properly handle timing in your application.
11
12
12
13
### Example
13
14
14
-
Consider a basic asynchronous test for a user signing in with correct credentials:
15
+
Consider a test for a user signing in with correct credentials:
15
16
16
17
```javascript
17
18
test('User can sign in with correct credentials', async () => {
@@ -51,8 +51,8 @@ Example [full source code](https://github.com/callstack/react-native-testing-lib
51
51
A custom render function might accept additional parameters to allow for setting up different start conditions for a test, e.g., the initial state for global state management.
52
52
53
53
```tsx title=SomeScreen.test.tsx
54
-
test('renders SomeScreen for logged in user', () => {
@@ -66,13 +66,18 @@ function renderNavigator(ui, options);
66
66
function renderScreen(ui, options);
67
67
```
68
68
69
-
#### Async function
69
+
#### Async setup
70
70
71
-
Make it async if you want to put some async setup in your custom render function.
71
+
Since `render` is async, your custom render function should be marked as `async` and use `await render()`. This pattern also makes it easy to add additional async setup if needed:
@@ -144,12 +144,11 @@ We can now use the `renderWithAtoms` function to render the `TaskList` component
144
144
In our test, we populated only one atom and its initial value, but you can add other Jotai atoms and their corresponding values to the initialValues array as needed.
Copy file name to clipboardExpand all lines: website/docs/14.x/docs/start/intro.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ You want to write maintainable tests for your React Native components. As a part
6
6
7
7
## This solution
8
8
9
-
The React Native Testing Library (RNTL) is a lightweight solution for testing React Native components. It provides light utility functions on top of [Test Renderer](https://github.com/mdjastrzebski/test-renderer), in a way that encourages better testing practices. Its primary guiding principle is:
9
+
The React Native Testing Library (RNTL) is a comprehensive solution for testing React Native components. It provides React Native runtime simulation on top of [Test Renderer](https://github.com/mdjastrzebski/test-renderer), in a way that encourages better testing practices. Its primary guiding principle is:
10
10
11
11
> The more your tests resemble how your software is used, the more confidence they can give you.
Copy file name to clipboardExpand all lines: website/docs/14.x/docs/start/quick-start.mdx
+10-1Lines changed: 10 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,16 @@ Open a Terminal in your project's folder and run:
13
13
}}
14
14
/>
15
15
16
-
This library uses [Test Renderer](https://github.com/mdjastrzebski/test-renderer) as its underlying renderer. Test Renderer is automatically installed as a dependency and provides better compatibility with React 19 and improved type safety compared to the deprecated [React Test Renderer](https://reactjs.org/docs/test-renderer.html).
16
+
This library has a peer dependency on [Test Renderer](https://github.com/mdjastrzebski/test-renderer). Make sure to install it:
17
+
18
+
<PackageManagerTabs
19
+
command={{
20
+
yarn: 'yarn add -D test-renderer',
21
+
npm: 'npm install -D test-renderer',
22
+
}}
23
+
/>
24
+
25
+
Test Renderer provides better compatibility with React 19 and improved type safety compared to the deprecated [React Test Renderer](https://reactjs.org/docs/test-renderer.html).
0 commit comments