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/13.x/cookbook/advanced/network-requests.md
+11-12Lines changed: 11 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,12 @@
2
2
3
3
## Introduction
4
4
5
-
Mocking network requests is an essential part of testing React Native applications. By mocking
6
-
network
7
-
requests, you can control the data that is returned from the server and test how your application
8
-
behaves in different scenarios, such as when the request is successful or when it fails.
5
+
Mocking network requests is essential for testing React Native applications. By mocking
6
+
network requests, you can control the data returned from the server and test how your application
7
+
behaves in different scenarios, such as when the request succeeds or fails.
9
8
10
-
In this guide, we will show you how to mock network requests and guard your test suits from unwanted
11
-
and unmocked/unhandled network requests
9
+
This guide shows how to mock network requests and guard your test suites against unwanted
10
+
and unmocked/unhandled network requests.
12
11
13
12
:::info
14
13
To simulate a real-world scenario, we will use the [Random User Generator API](https://randomuser.me/) that provides random user data.
@@ -363,14 +362,14 @@ Which will result in a warning in the console if you forget to mock an API reque
363
362
364
363
## Conclusion
365
364
366
-
Testing a component that makes network requests in combination with MSW takes some initial preparation to configure and describe the overridden networks.
367
-
We can achieve that by using MSW's request handlers and intercepting APIs.
365
+
Testing components that make network requests with MSW requires initial setup to configure and describe the overridden networks.
366
+
Use MSW's request handlers and intercepting APIs to achieve this.
368
367
369
-
Once up and running we gain full grip over the network requests, their responses, statuses.
370
-
Doing so is crucial to be able to test how our application behaves in different
371
-
scenarios, such as when the request is successful or when it fails.
368
+
Once configured, you have full control over network requests, their responses, and statuses.
369
+
This lets you test how your application behaves in different
370
+
scenarios, such as when requests succeed or fail.
372
371
373
-
When global configuration is in place, MSW's will also warn us when an unhandled network requests has occurred throughout a test suite.
372
+
With global configuration in place, MSW will also warn you when an unhandled network request occurs during a test suite.
Copy file name to clipboardExpand all lines: website/docs/13.x/docs/advanced/testing-env.mdx
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ This document is intended for a more advanced audience who want to understand th
6
6
7
7
:::
8
8
9
-
React Native Testing Library allows you to write integration and component tests for your React Native app or library. While the JSX code used in tests closely resembles your React Native app, things are not as simple as they might appear. This document will describe the key elements of our testing environment and highlight things to be aware of when writing more advanced tests or diagnosing issues.
9
+
React Native Testing Library allows you to write integration and component tests for your React Native app or library. While the JSX code used in tests closely resembles your React Native app, things aren't as simple as they might appear. This document describes the key elements of our testing environment and highlights things to be aware of when writing more advanced tests or diagnosing issues.
10
10
11
11
## React renderers
12
12
@@ -37,7 +37,7 @@ Disadvantages:
37
37
- Assertions do not operate on native view hierarchy
38
38
- Runtime behaviors are simulated, sometimes imperfectly
39
39
40
-
It's worth noting that the React Testing Library (web one) works a bit differently. While RTL also runs in Jest, it has access to a simulated browser DOM environment from the `jsdom` package, which allows it to use a regular React DOM renderer. Unfortunately, there is no similar React Native runtime environment package. This is probably because while the browser environment is well-defined and highly standardized, the React Native environment constantly evolves in sync with the evolution of underlying OS-es. Maintaining such an environment would require duplicating countless React Native behaviors and keeping them in sync as React Native develops.
40
+
The React Testing Library (web one) works differently. While RTL also runs in Jest, it has access to a simulated browser DOM environment from the `jsdom` package, which allows it to use a regular React DOM renderer. Unfortunately, there's no similar React Native runtime environment package. This is probably because while the browser environment is well-defined and highly standardized, the React Native environment constantly evolves in sync with the evolution of underlying OS-es. Maintaining such an environment would require duplicating countless React Native behaviors and keeping them in sync as React Native develops.
41
41
42
42
## Element tree
43
43
@@ -111,7 +111,7 @@ function isHostElement(element: ReactTestInstance) {
111
111
112
112
## Tree nodes
113
113
114
-
We encourage you to only assert values on host views in your tests because they represent the user interface view and controls which the user can see and interact with. Users cannot see or interact with composite views as they exist purely in the JavaScript domain and do not generate any visible UI.
114
+
We encourage you to only assert values on host views in your tests because they represent the user interface view and controls that users can see and interact with. Users can't see or interact with composite views as they exist purely in the JavaScript domain and don't generate any visible UI.
Copy file name to clipboardExpand all lines: website/docs/13.x/docs/advanced/third-party-integration.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Third-Party Library Integration
2
2
3
-
The React Native Testing Library is designed to simulate the core behaviors of React Native. However, it does not replicate the internal logic of third-party libraries. This guide explains how to integrate your library with RNTL.
3
+
The React Native Testing Library is designed to simulate the core behaviors of React Native. However, it doesn't replicate the internal logic of third-party libraries. This guide explains how to integrate your library with RNTL.
Copy file name to clipboardExpand all lines: website/docs/13.x/docs/advanced/understanding-act.mdx
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Understanding `act` function
2
2
3
-
When writing RNTL tests one of the things that confuses developers the most are cryptic [`act()`](https://reactjs.org/docs/testing-recipes.html#act) function errors logged into console. In this article I will try to build an understanding of the purpose and behaviour of `act()` so you can build your tests with more confidence.
3
+
When writing RNTL tests, one of the things that confuses developers the most are cryptic [`act()`](https://reactjs.org/docs/testing-recipes.html#act) function errors logged to the console. This article explains the purpose and behavior of `act()` so you can write tests with more confidence.
4
4
5
5
## `act` warnings
6
6
@@ -33,7 +33,7 @@ This function is intended only for using in automated tests and works only in de
33
33
34
34
The responsibility for `act` function is to make React renders and updates work in tests in a similar way they work in real application by grouping and executing related units of interaction (e.g. renders, effects, etc) together.
35
35
36
-
To showcase that behaviour let make a small experiment. First we define a function component that uses `useEffect` hook in a trivial way.
36
+
To show that behavior, let's make a small experiment. First we define a function component that uses `useEffect` hook in a trivial way.
37
37
38
38
```jsx
39
39
functionTestComponent() {
@@ -58,7 +58,7 @@ test('render without act', () => {
58
58
});
59
59
```
60
60
61
-
When testing without `act` call wrapping rendering call, we see that the assertion runs just after the rendering but before `useEffect`hooks effects are applied. Which is not what we expected in our tests.
61
+
When testing without wrapping the rendering call in `act`, the assertion runs just after rendering but before `useEffect`hooks effects are applied. This is not what we expected in our tests.
62
62
63
63
```jsx
64
64
test('render with act', () => {
@@ -73,21 +73,21 @@ test('render with act', () => {
73
73
});
74
74
```
75
75
76
-
When wrapping rendering call with `act` we see that the changes caused by `useEffect` hook have been applied as we would expect.
76
+
When wrapping the rendering call with `act`, the changes caused by the `useEffect` hook are applied as expected.
77
77
78
78
### When to use act
79
79
80
80
The name `act` comes from [Arrange-Act-Assert](http://wiki.c2.com/?ArrangeActAssert) unit testing pattern. Which means it’s related to part of the test when we execute some actions on the component tree.
81
81
82
-
So far we learned that `act` function allows tests to wait for all pending React interactions to be applied before we make our assertions. When using `act` we get guarantee that any state updates will be executed as well as any enqueued effects will be executed.
82
+
The `act` function allows tests to wait for all pending React interactions to be applied before making assertions. When using `act`, we get a guarantee that any state updates will be executed and any enqueued effects will be executed.
83
83
84
84
Therefore, we should use `act` whenever there is some action that causes element tree to render, particularly:
- re-rendering of component -`renderer.update` call
88
88
- triggering any event handlers that cause component tree render
89
89
90
-
Thankfully, for these basic cases RNTL has got you covered as our `render`, `update` and `fireEvent` methods already wrap their calls in sync `act` so that you do not have to do it explicitly.
90
+
For these basic cases, RNTL handles it for you. Our `render`, `update`, and `fireEvent` methods already wrap their calls in sync `act` so you don't have to do it explicitly.
91
91
92
92
Note that `act` calls can be safely nested and internally form a stack of calls. However, overlapping `act` calls, which can be achieved using async version of `act`, [are not supported](https://github.com/facebook/react/blob/main/packages/react/src/ReactAct.js#L161).
93
93
@@ -103,9 +103,9 @@ So far we have seen synchronous version of `act` which runs its callback immedia
103
103
104
104
### Asynchronous code
105
105
106
-
Asynchronous version of `act`also is executed immediately, but the callback is not yet completed because of some asynchronous operations inside.
106
+
The asynchronous version of `act` is also executed immediately, but the callback doesn't complete right away because of asynchronous operations inside.
107
107
108
-
Lets look at a simple example with component using `setTimeout`call to simulate asynchronous behaviour:
108
+
Let's look at a simple example with a component using `setTimeout` to simulate asynchronous behavior:
If we test our component in a native way without handling its asynchronous behaviour we will end up with sync act warning:
132
+
If we test our component without handling its asynchronous behavior, we'll get a sync act warning:
133
133
134
134
```
135
135
Warning: An update to TestAsyncComponent inside a test was not wrapped in act(...).
@@ -142,7 +142,7 @@ act(() => {
142
142
/* assert on the output */
143
143
```
144
144
145
-
Note that this is not yet the infamous async act warning. It only asks us to wrap our event code with `act` calls. However, this time our immediate state change does not originate from externally triggered events but rather forms an internal part of the component. So how can we apply `act` in such scenario?
145
+
This is not yet the async act warning. It only asks us to wrap our event code with `act` calls. However, this time the state change doesn't come from externally triggered events but from an internal part of the component. So how can we apply `act` in this scenario?
That way we can wrap `jest.runAllTimers()` call which triggers the `setTimeout` updates inside an `act` call, hence resolving the act warning. Note that this whole code is synchronous thanks to usage of Jest fake timers.
163
+
This way we can wrap the `jest.runAllTimers()` call, which triggers the `setTimeout` updates, inside an `act` call, resolving the act warning. Note that this whole code is synchronous thanks to Jest fake timers.
164
164
165
165
### Solution with real timers
166
166
@@ -177,7 +177,7 @@ test('render with real timers - sleep', async () => {
177
177
});
178
178
```
179
179
180
-
This works correctly as we use an explicit async `act()` call that resolves the console error. However, it relies on our knowledge of exact implementation details which is a bad practice.
180
+
This works correctly because we use an explicit async `act()` call that resolves the console error. However, it relies on knowing exact implementation details, which is a bad practice.
181
181
182
182
Let’s try more elegant solution using `waitFor` that will wait for our desired state:
183
183
@@ -190,7 +190,7 @@ test('render with real timers - waitFor', async () => {
190
190
});
191
191
```
192
192
193
-
This also works correctly, because `waitFor`call executes async `act()` call internally.
193
+
This also works correctly because `waitFor` executes async `act()` internally.
194
194
195
195
The above code can be simplified using `findBy` query:
196
196
@@ -202,13 +202,13 @@ test('render with real timers - findBy', async () => {
202
202
});
203
203
```
204
204
205
-
This also works since`findByText` internally calls `waitFor` which uses async `act()`.
205
+
This also works because`findByText` internally calls `waitFor`, which uses async `act()`.
206
206
207
207
Note that all of the above examples are async tests using & awaiting async `act()` function call.
208
208
209
209
### Async act warning
210
210
211
-
If we modify any of the above async tests and remove `await` keyword, then we will trigger the notorious async `act()`warning:
211
+
If we modify any of the above async tests and remove the `await` keyword, we'll trigger the async `act()`warning:
212
212
213
213
```jsx
214
214
Warning: You called act(async () =>...) without await. This could lead to unexpected
@@ -218,7 +218,7 @@ testing behaviour, interleaving multiple act calls and mixing their scopes. You
218
218
219
219
React decides to show this error whenever it detects that async `act()`call [has not been awaited](https://github.com/facebook/react/blob/ce13860281f833de8a3296b7a3dad9caced102e9/packages/react/src/ReactAct.js#L93).
220
220
221
-
The exact reasons why you might see async `act()` warnings vary, but finally it means that `act()` has been called with callback that returns `Promise`-like object, but it has not been waited on.
221
+
The exact reasons why you might see async `act()` warnings vary, but it means that `act()` has been called with a callback that returns a `Promise`-like object, but it hasn't been awaited.
0 commit comments