Skip to content

Commit 55e6cb8

Browse files
committed
text clean up
1 parent 5c5ec89 commit 55e6cb8

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

versioned_docs/version-7.x/testing.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ If you're not using Jest, then you'll need to mock these modules according to th
7979

8080
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.
8181

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

8484
### Example 1
8585

86-
Navigate to settings screen by button press.
86+
Navigate to settings screen by "Go to Settings" button press.
8787

8888
<Tabs groupId="example" queryString="example">
8989
<TabItem value="static" label="Static" default>
@@ -174,7 +174,7 @@ import { fireEvent, render, screen } from '@testing-library/react-native';
174174

175175
import { StackNavigator } from './StackNavigator';
176176

177-
test('navigates to settings by button press', () => {
177+
test('navigates to settings by "Go to Settings" button press', () => {
178178
const StackNavigation = createStaticNavigation(StackNavigator);
179179
render(<StackNavigation />);
180180

@@ -193,7 +193,7 @@ import { fireEvent, render, screen } from '@testing-library/react-native';
193193

194194
import { StackNavigator } from './StackNavigator';
195195

196-
test('navigates to settings by button press', () => {
196+
test('navigates to settings by "Go to Settings" button press', () => {
197197
render(
198198
<NavigationContainer>
199199
<StackNavigator />
@@ -208,7 +208,7 @@ test('navigates to settings by button press', () => {
208208
</TabItem>
209209
</Tabs>
210210

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

213213
### Example 2
214214

@@ -344,31 +344,32 @@ test('navigates to settings by tab bar button press', () => {
344344
</TabItem>
345345
</Tabs>
346346

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

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

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

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

358358
```js
359-
// Pass event object to fireEvent to prevent error
359+
// Pass event object to avoid error
360360
const event = {};
361361
fireEvent.press(button, event);
362362
```
363363

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

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

370370
// ...
371371

372+
// Wrap jest.runAllTimers to prevent state change error
372373
// Skip all timers including animations
373374
act(() => jest.runAllTimers());
374375
```
@@ -449,7 +450,7 @@ export const TabNavigator = createBottomTabNavigator({
449450

450451
```js
451452
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
452-
import { createNativeStackNavigator } from '@react-navigation/native-stack';
453+
import { createStackNavigator } from '@react-navigation/stack';
453454
import { useEffect } from 'react';
454455
import { Button, Text, View } from 'react-native';
455456

@@ -488,7 +489,7 @@ const DetailsScreen = ({ navigation }) => {
488489
);
489490
};
490491

491-
const SettingsStack = createNativeStackNavigator();
492+
const SettingsStack = createStackNavigator();
492493

493494
function SettingsStackScreen() {
494495
return (
@@ -541,8 +542,8 @@ test('always displays settings screen after tab bar settings button press', () =
541542
const event = {};
542543

543544
fireEvent.press(settingsTabButton, event);
544-
expect(screen.queryByText('Settings screen')).toBeOnTheScreen();
545545
act(() => jest.runAllTimers());
546+
expect(screen.queryByText('Settings screen')).toBeOnTheScreen();
546547

547548
fireEvent.press(screen.queryByText('Go to Details'), event);
548549
act(() => jest.runAllTimers());
@@ -630,7 +631,7 @@ function HomeScreen() {
630631
);
631632
}
632633

633-
const url = 'place_your_url';
634+
const url = 'place_your_url_here';
634635

635636
function ProfileScreen() {
636637
const [loading, setLoading] = useState(true);
@@ -689,7 +690,7 @@ function HomeScreen() {
689690
);
690691
}
691692

692-
const url = 'place_url_here';
693+
const url = 'place_your_url_here';
693694

694695
function ProfileScreen() {
695696
const [loading, setLoading] = useState(true);
@@ -858,7 +859,7 @@ test('Display loading state while waiting for data and then fetched profile nick
858859
</TabItem>
859860
</Tabs>
860861

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

863864
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`.
864865

0 commit comments

Comments
 (0)