@@ -3,8 +3,11 @@ import { render, act } from 'react-native-testing-library';
33import useNavigationBuilder from '../useNavigationBuilder' ;
44import NavigationContainer from '../NavigationContainer' ;
55import Screen from '../Screen' ;
6- import MockRouter , { MockRouterKey } from './__fixtures__/MockRouter' ;
7- import { NavigationState } from '../types' ;
6+ import MockRouter , {
7+ MockActions ,
8+ MockRouterKey ,
9+ } from './__fixtures__/MockRouter' ;
10+ import { DefaultRouterOptions , NavigationState , Router } from '../types' ;
811
912jest . useFakeTimers ( ) ;
1013
@@ -54,6 +57,180 @@ it('sets options with options prop as an object', () => {
5457 ` ) ;
5558} ) ;
5659
60+ it ( "returns correct value for canGoBack when it's not overridden" , ( ) => {
61+ const TestNavigator = ( props : any ) => {
62+ const { state, descriptors } = useNavigationBuilder <
63+ NavigationState ,
64+ { title ?: string } ,
65+ any
66+ > ( MockRouter , props ) ;
67+ const { render, options } = descriptors [ state . routes [ state . index ] . key ] ;
68+
69+ return (
70+ < main >
71+ < h1 > { options . title } </ h1 >
72+ < div > { render ( ) } </ div >
73+ </ main >
74+ ) ;
75+ } ;
76+
77+ let result = true ;
78+
79+ const TestScreen = ( { navigation } : any ) : any => {
80+ React . useEffect ( ( ) => {
81+ result = navigation . canGoBack ( ) ;
82+ } ) ;
83+
84+ return null ;
85+ } ;
86+
87+ const root = (
88+ < NavigationContainer >
89+ < TestNavigator >
90+ < Screen
91+ name = "foo"
92+ component = { TestScreen }
93+ options = { { title : 'Hello world' } }
94+ />
95+ < Screen name = "bar" component = { jest . fn ( ) } />
96+ </ TestNavigator >
97+ </ NavigationContainer >
98+ ) ;
99+
100+ render ( root ) . update ( root ) ;
101+
102+ expect ( result ) . toEqual ( false ) ;
103+ } ) ;
104+
105+ it ( "returns correct value for canGoBack when it's overridden" , ( ) => {
106+ function ParentRouter ( options : DefaultRouterOptions ) {
107+ const CurrentMockRouter = MockRouter ( options ) ;
108+ const ChildRouter : Router < NavigationState , MockActions > = {
109+ ...CurrentMockRouter ,
110+
111+ canGoBack ( ) {
112+ return true ;
113+ } ,
114+ } ;
115+ return ChildRouter ;
116+ }
117+
118+ const ParentNavigator = ( props : any ) => {
119+ const { state, descriptors } = useNavigationBuilder <
120+ NavigationState ,
121+ { title ?: string } ,
122+ any
123+ > ( ParentRouter , props ) ;
124+ return descriptors [ state . routes [ state . index ] . key ] . render ( ) ;
125+ } ;
126+
127+ const ChildNavigator = ( props : any ) => {
128+ const { state, descriptors } = useNavigationBuilder <
129+ NavigationState ,
130+ { title ?: string } ,
131+ any
132+ > ( MockRouter , props ) ;
133+
134+ return descriptors [ state . routes [ state . index ] . key ] . render ( ) ;
135+ } ;
136+
137+ let result = false ;
138+
139+ const TestScreen = ( { navigation } : any ) : any => {
140+ React . useEffect ( ( ) => {
141+ result = navigation . canGoBack ( ) ;
142+ } ) ;
143+
144+ return null ;
145+ } ;
146+
147+ const root = (
148+ < NavigationContainer >
149+ < ParentNavigator >
150+ < Screen name = "baz" >
151+ { ( ) => (
152+ < ChildNavigator >
153+ < Screen name = "qux" component = { TestScreen } />
154+ </ ChildNavigator >
155+ ) }
156+ </ Screen >
157+ </ ParentNavigator >
158+ </ NavigationContainer >
159+ ) ;
160+
161+ render ( root ) . update ( root ) ;
162+
163+ expect ( result ) . toEqual ( true ) ;
164+ } ) ;
165+
166+ it ( 'returns correct value for canGoBack when parent router overrides it' , ( ) => {
167+ function OverrodeRouter ( options : DefaultRouterOptions ) {
168+ const CurrentMockRouter = MockRouter ( options ) ;
169+ const ChildRouter : Router < NavigationState , MockActions > = {
170+ ...CurrentMockRouter ,
171+
172+ canGoBack ( ) {
173+ return true ;
174+ } ,
175+ } ;
176+ return ChildRouter ;
177+ }
178+
179+ const OverrodeNavigator = ( props : any ) => {
180+ const { state, descriptors } = useNavigationBuilder <
181+ NavigationState ,
182+ { title ?: string } ,
183+ any
184+ > ( OverrodeRouter , props ) ;
185+ return descriptors [ state . routes [ state . index ] . key ] . render ( ) ;
186+ } ;
187+
188+ const TestNavigator = ( props : any ) => {
189+ const { state, descriptors } = useNavigationBuilder <
190+ NavigationState ,
191+ { title ?: string } ,
192+ any
193+ > ( MockRouter , props ) ;
194+
195+ return descriptors [ state . routes [ state . index ] . key ] . render ( ) ;
196+ } ;
197+
198+ let result = true ;
199+
200+ const TestScreen = ( { navigation } : any ) : any => {
201+ React . useEffect ( ( ) => {
202+ result = navigation . canGoBack ( ) ;
203+ } ) ;
204+
205+ return null ;
206+ } ;
207+
208+ const root = (
209+ < NavigationContainer >
210+ < TestNavigator >
211+ < Screen name = "baz" >
212+ { ( ) => (
213+ < TestNavigator >
214+ < Screen name = "qux" component = { TestScreen } />
215+ </ TestNavigator >
216+ ) }
217+ </ Screen >
218+ < Screen name = "qux" >
219+ { ( ) => (
220+ < OverrodeNavigator >
221+ < Screen name = "qux" component = { ( ) => null } />
222+ </ OverrodeNavigator >
223+ ) }
224+ </ Screen >
225+ </ TestNavigator >
226+ </ NavigationContainer >
227+ ) ;
228+
229+ render ( root ) . update ( root ) ;
230+
231+ expect ( result ) . toEqual ( false ) ;
232+ } ) ;
233+
57234it ( 'sets options with options prop as a fuction' , ( ) => {
58235 const TestNavigator = ( props : any ) => {
59236 const { state, descriptors } = useNavigationBuilder <
0 commit comments