@@ -87,10 +87,7 @@ We are going to write example tests illustrating the difference between `navigat
8787``` js
8888import { Button , Text , View } from ' react-native' ;
8989import { 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
9592const 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
181176import { expect , test } from ' @jest/globals' ;
182177import { 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' ;
184182import { RootNavigator } from ' ./RootNavigator' ;
185183
186184test (' 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
243242import { expect , test } from ' @jest/globals' ;
244- import { createNavigationContainerRef } from ' @react-navigation/native' ;
243+ import {
244+ createNavigationContainerRef ,
245+ createStaticNavigation ,
246+ } from ' @react-navigation/native' ;
245247import { fireEvent , render , screen } from ' @testing-library/react-native' ;
246248import { RootNavigator } from ' ./RootNavigator' ;
247249
248250test (' 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
365368import { expect , jest , test } from ' @jest/globals' ;
366369import { act , fireEvent , render , screen } from ' @testing-library/react-native' ;
370+ import { createStaticNavigation } from ' @react-navigation/native' ;
367371import { RootNavigator } from ' ./RootNavigator' ;
368372
369373test (' 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