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
+15-13Lines changed: 15 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -208,7 +208,7 @@ test('navigates to settings by "Go to Settings" button press', () => {
208
208
</TabItem>
209
209
</Tabs>
210
210
211
-
We use `FireEvent` to press button and `expect` to check if rendered screen's content matches settings.
211
+
We use `FireEvent` to press button and `expect` to check if rendered screen's content matches settings screen.
212
212
213
213
### Example 2
214
214
@@ -346,10 +346,10 @@ test('navigates to settings by tab bar button press', () => {
346
346
347
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 `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.
349
+
To find settings tab bar 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 settings tab
352
+
// Pass object with settings tab name
353
353
constbutton=screen.getByRole('button', { name:'Settings, tab, 2 of 2' });
354
354
```
355
355
@@ -361,22 +361,22 @@ const event = {};
361
361
fireEvent.press(button, event);
362
362
```
363
363
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`.
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. They allow you to instantly skip animation time. To avoid getting state change error, wrap `runAllTimers`in`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
372
+
// Wrap jest.runAllTimers in act to prevent state change error
373
373
// Skip all timers including animations
374
374
act(() =>jest.runAllTimers());
375
375
```
376
376
377
377
### Example 3
378
378
379
-
Always displays settings screen after tab bar settings button press.
379
+
Always displays settings screen after settings tab bar button press.
380
380
381
381
<TabsgroupId="example"queryString="example">
382
382
<TabItemvalue="static"label="Static"default>
@@ -410,7 +410,7 @@ function SettingsScreen() {
410
410
);
411
411
}
412
412
413
-
constDetailsScreen= () => {
413
+
functionDetailsScreen() {
414
414
constnavigation=useNavigation();
415
415
416
416
useEffect(() => {
@@ -425,7 +425,7 @@ const DetailsScreen = () => {
425
425
<Text>Details screen</Text>
426
426
</View>
427
427
);
428
-
};
428
+
}
429
429
430
430
constSettingsNavigator=createStackNavigator({
431
431
screens: {
@@ -474,7 +474,7 @@ function SettingsScreen({ navigation }) {
@@ -859,7 +861,7 @@ test('Display loading state while waiting for data and then fetched profile nick
859
861
</TabItem>
860
862
</Tabs>
861
863
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.
864
+
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 not only on first focus, we navigate back to home, then to settings and check loading state and fetched data again.
863
865
864
866
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