Skip to content

Commit 6f506b0

Browse files
committed
clean up
1 parent 7da2155 commit 6f506b0

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

versioned_docs/version-7.x/testing.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ test('navigates to settings by "Go to Settings" button press', () => {
208208
</TabItem>
209209
</Tabs>
210210

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.
212212

213213
### Example 2
214214

@@ -346,10 +346,10 @@ test('navigates to settings by tab bar button press', () => {
346346

347347
We get settings tab bar button, press it and check if rendered content is correct.
348348

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.
350350

351351
```js
352-
// Pass name of settings tab
352+
// Pass object with settings tab name
353353
const button = screen.getByRole('button', { name: 'Settings, tab, 2 of 2' });
354354
```
355355

@@ -361,22 +361,22 @@ const event = {};
361361
fireEvent.press(button, event);
362362
```
363363

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`.
365365

366366
```js
367367
// Enable fake timers
368368
jest.useFakeTimers();
369369

370370
// ...
371371

372-
// Wrap jest.runAllTimers to prevent state change error
372+
// Wrap jest.runAllTimers in act to prevent state change error
373373
// Skip all timers including animations
374374
act(() => jest.runAllTimers());
375375
```
376376

377377
### Example 3
378378

379-
Always displays settings screen after tab bar settings button press.
379+
Always displays settings screen after settings tab bar button press.
380380

381381
<Tabs groupId="example" queryString="example">
382382
<TabItem value="static" label="Static" default>
@@ -410,7 +410,7 @@ function SettingsScreen() {
410410
);
411411
}
412412

413-
const DetailsScreen = () => {
413+
function DetailsScreen() {
414414
const navigation = useNavigation();
415415

416416
useEffect(() => {
@@ -425,7 +425,7 @@ const DetailsScreen = () => {
425425
<Text>Details screen</Text>
426426
</View>
427427
);
428-
};
428+
}
429429

430430
const SettingsNavigator = createStackNavigator({
431431
screens: {
@@ -474,7 +474,7 @@ function SettingsScreen({ navigation }) {
474474
);
475475
}
476476

477-
const DetailsScreen = ({ navigation }) => {
477+
function DetailsScreen({ navigation }) {
478478
useEffect(() => {
479479
const unsubscribe = navigation.getParent().addListener('tabPress', (e) => {
480480
navigation.popToTop();
@@ -487,7 +487,7 @@ const DetailsScreen = ({ navigation }) => {
487487
<Text>Details screen</Text>
488488
</View>
489489
);
490-
};
490+
}
491491

492492
const SettingsStack = createStackNavigator();
493493

@@ -525,7 +525,7 @@ import { act, fireEvent, render, screen } from '@testing-library/react-native';
525525

526526
import { TabNavigator } from './TabNavigator';
527527

528-
test('always displays settings screen after tab bar settings button press', () => {
528+
test('always displays settings screen after settings tab bar button press', () => {
529529
jest.useFakeTimers();
530530

531531
const TabNavigation = createStaticNavigation(TabNavigator);
@@ -569,7 +569,7 @@ import { act, fireEvent, render, screen } from '@testing-library/react-native';
569569

570570
import { TabNavigator } from './TabNavigator';
571571

572-
test('always displays settings screen after tab bar settings button press', () => {
572+
test('always displays settings screen after settings tab bar button press', () => {
573573
jest.useFakeTimers();
574574

575575
render(
@@ -653,6 +653,7 @@ function ProfileScreen() {
653653
};
654654
}, [])
655655
);
656+
656657
return (
657658
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
658659
{loading && <Text>Loading</Text>}
@@ -712,6 +713,7 @@ function ProfileScreen() {
712713
};
713714
}, [])
714715
);
716+
715717
return (
716718
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
717719
{loading && <Text>Loading</Text>}
@@ -859,7 +861,7 @@ test('Display loading state while waiting for data and then fetched profile nick
859861
</TabItem>
860862
</Tabs>
861863

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.
863865

864866
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`.
865867

0 commit comments

Comments
 (0)