Skip to content

Commit c8e9a0c

Browse files
committed
move create static navigation
1 parent 02e0b0e commit c8e9a0c

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

versioned_docs/version-7.x/testing.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ We are going to write example tests illustrating the difference between `navigat
8787
```js
8888
import { Button, Text, View } from 'react-native';
8989
import { createNativeStackNavigator } from '@react-navigation/native-stack';
90-
import {
91-
createStaticNavigation,
92-
useNavigation,
93-
} from '@react-navigation/native';
90+
import { useNavigation } from '@react-navigation/native';
9491

9592
const Profile = () => {
9693
const navigation = useNavigation();
@@ -117,14 +114,12 @@ const Settings = () => {
117114
);
118115
};
119116

120-
const RootStack = createNativeStackNavigator({
117+
export const RootNavigator = createNativeStackNavigator({
121118
screens: {
122119
Profile,
123120
Settings,
124121
},
125122
});
126-
127-
export const RootNavigator = createStaticNavigation(RootStack);
128123
```
129124

130125
</TabItem>
@@ -180,12 +175,16 @@ export const RootNavigator = () => {
180175
```js
181176
import { expect, test } from '@jest/globals';
182177
import { fireEvent, render, screen } from '@testing-library/react-native';
183-
import { createNavigationContainerRef } from '@react-navigation/native';
178+
import {
179+
createNavigationContainerRef,
180+
createStaticNavigation,
181+
} from '@react-navigation/native';
184182
import { RootNavigator } from './RootNavigator';
185183

186184
test('navigates to settings screen twice', () => {
185+
const RootNavigation = createStaticNavigation(RootNavigator);
187186
const navigation = createNavigationContainerRef();
188-
render(<RootNavigator ref={navigation} />);
187+
render(<RootNavigation ref={navigation} />);
189188

190189
const button = screen.getByText('Navigate to Settings');
191190
fireEvent.press(button);
@@ -241,13 +240,17 @@ test('navigates to settings screen twice', () => {
241240

242241
```js
243242
import { expect, test } from '@jest/globals';
244-
import { createNavigationContainerRef } from '@react-navigation/native';
243+
import {
244+
createNavigationContainerRef,
245+
createStaticNavigation,
246+
} from '@react-navigation/native';
245247
import { fireEvent, render, screen } from '@testing-library/react-native';
246248
import { RootNavigator } from './RootNavigator';
247249

248250
test('pushes settings screen twice', () => {
251+
const RootNavigation = createStaticNavigation(RootNavigator);
249252
const navigation = createNavigationContainerRef();
250-
render(<RootNavigator ref={navigation} />);
253+
render(<RootNavigation ref={navigation} />);
251254

252255
const button = screen.getByText('Push Settings');
253256
fireEvent.press(button);
@@ -364,13 +367,15 @@ Fake timers test example:
364367
```js
365368
import { expect, jest, test } from '@jest/globals';
366369
import { act, fireEvent, render, screen } from '@testing-library/react-native';
370+
import { createStaticNavigation } from '@react-navigation/native';
367371
import { RootNavigator } from './RootNavigator';
368372

369373
test('navigates to settings screen after 10000 ms delay', () => {
370374
// Enable fake timers
371375
jest.useFakeTimers();
372376

373-
render(<RootNavigator />);
377+
const RootNavigation = createStaticNavigation(RootNavigator);
378+
render(<RootNavigation />);
374379

375380
fireEvent.press(screen.getByText('Navigate to Settings with 10000 ms delay'));
376381

0 commit comments

Comments
 (0)