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: versioned_docs/version-7.x/testing.md
+18-17Lines changed: 18 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -79,11 +79,11 @@ If you're not using Jest, then you'll need to mock these modules according to th
79
79
80
80
We recommend using [React Native Testing Library](https://callstack.github.io/react-native-testing-library/) along with [`jest-native`](https://github.com/testing-library/jest-native) to write your tests.
81
81
82
-
We will go through some real-world case test code examples. Each code example consists of navigator and test code file.
82
+
We will go through some real-world case test code examples. Each code example consists of tested navigator and test code file.
83
83
84
84
### Example 1
85
85
86
-
Navigate to settings screen by button press.
86
+
Navigate to settings screen by "Go to Settings" button press.
test('navigates to settings by button press', () => {
196
+
test('navigates to settings by "Go to Settings" button press', () => {
197
197
render(
198
198
<NavigationContainer>
199
199
<StackNavigator />
@@ -208,7 +208,7 @@ test('navigates to settings by button press', () => {
208
208
</TabItem>
209
209
</Tabs>
210
210
211
-
We press button by `FireEvent` to navigate to settings screen and call `expect` to check if rendered content is correct.
211
+
We use `FireEvent` to press button and `expect` to check if rendered screen's content matches settings.
212
212
213
213
### Example 2
214
214
@@ -344,31 +344,32 @@ test('navigates to settings by tab bar button press', () => {
344
344
</TabItem>
345
345
</Tabs>
346
346
347
-
We get settings tab bar button, press by `FireEvent` and check if rendered content is correct.
347
+
We get settings tab bar button, press it and check if rendered content is correct.
348
348
349
-
To find settings tab button you cannot use query by text, because there is no text you can use to do that. You can use `getByRole` instead and pass `name` prop.
349
+
To find settings tab button you cannot use `queryByText`, because there is no text that can be queried. You can use `getByRole` instead and pass object with `name` prop as the second argument.
350
350
351
351
```js
352
-
// Pass name of tab as second prop
352
+
// Pass name of settings tab
353
353
constbutton=screen.getByRole('button', { name:'Settings, tab, 2 of 2' });
354
354
```
355
355
356
-
Bottom tabs bar buttons `handlePress` function expects `GestureResponderEvent`. To avoid error you should pass `event` object as the second argument of `fireEvent`.
356
+
Tab bar buttons `handlePress` function expects to receive`GestureResponderEvent`. To avoid error you should pass `event` object as the second argument of `fireEvent`.
357
357
358
358
```js
359
-
// Pass event object to fireEvent to prevent error
359
+
// Pass event object to avoid error
360
360
constevent= {};
361
361
fireEvent.press(button, event);
362
362
```
363
363
364
-
Sometimes navigation animations need some time to finish. You need to wait until animations finish before querying components. Therefore, you have to use `fake timers`. [`Fake Timers`](https://jestjs.io/docs/timer-mocks) replace real implementation of times function to use fake clock ticks. They allow you to instantly skip animations time and avoid getting state change error.
364
+
While writing tests containing navigation with animations you need to wait until animations finish before querying components. 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 ticks. They allow you to instantly skip animation time. To avoid getting state change error wrap `runAllTimers` with `act`.
365
365
366
366
```js
367
367
// Enable fake timers
368
368
jest.useFakeTimers();
369
369
370
370
// ...
371
371
372
+
// Wrap jest.runAllTimers to prevent state change error
fireEvent.press(screen.queryByText('Go to Details'), event);
548
549
act(() =>jest.runAllTimers());
@@ -630,7 +631,7 @@ function HomeScreen() {
630
631
);
631
632
}
632
633
633
-
consturl='place_your_url';
634
+
consturl='place_your_url_here';
634
635
635
636
functionProfileScreen() {
636
637
const [loading, setLoading] =useState(true);
@@ -689,7 +690,7 @@ function HomeScreen() {
689
690
);
690
691
}
691
692
692
-
consturl='place_url_here';
693
+
consturl='place_your_url_here';
693
694
694
695
functionProfileScreen() {
695
696
const [loading, setLoading] =useState(true);
@@ -858,7 +859,7 @@ test('Display loading state while waiting for data and then fetched profile nick
858
859
</TabItem>
859
860
</Tabs>
860
861
861
-
We query tab buttons and mock fetch function using `spyOn` and `mockImplementation`. We navigate to profile screen and check if loading state is rendered correctly. Then, to check if fetched data is displayed correctly we use `findByText` - we need to wait for the fetch to finish before checking it's result. To ensure that operation will succeed on every focus, we navigate back to home, then to settings and check rendered content again.
862
+
We query tab buttons and mock fetch function using `spyOn` and `mockImplementation`. We navigate to profile screen and check if loading state is rendered correctly. Then, to check if fetched data is displayed, we use `findByText` - we need to wait for the fetch to finish before checking it's result. To ensure that operation will succeed on every focus, we navigate back to home, then to settings and check loading state and fetched data again.
862
863
863
864
To make test deterministic and isolate it from the real backend you can mock fetch function. You can use `spyOn` to override real implementation of fetch with `mockedFetch`.
0 commit comments