@@ -3,91 +3,7 @@ import { render, act } from 'react-native-testing-library';
33import Screen from '../Screen' ;
44import NavigationContainer from '../NavigationContainer' ;
55import useNavigationBuilder from '../useNavigationBuilder' ;
6- import { Router } from '../types' ;
7-
8- export const MockRouter : Router < { type : string } > & { key : number } = {
9- key : 0 ,
10-
11- getInitialState ( {
12- routeNames,
13- initialRouteName = routeNames [ 0 ] ,
14- initialParamsList,
15- } ) {
16- const index = routeNames . indexOf ( initialRouteName ) ;
17-
18- return {
19- key : String ( MockRouter . key ++ ) ,
20- index,
21- routeNames,
22- routes : routeNames . map ( name => ( {
23- name,
24- key : name ,
25- params : initialParamsList [ name ] ,
26- } ) ) ,
27- } ;
28- } ,
29-
30- getRehydratedState ( { routeNames, partialState } ) {
31- let state = partialState ;
32-
33- if ( state . routeNames === undefined || state . key === undefined ) {
34- state = {
35- ...state ,
36- routeNames,
37- key : String ( MockRouter . key ++ ) ,
38- } ;
39- }
40-
41- return state ;
42- } ,
43-
44- getStateForRouteNamesChange ( state , { routeNames } ) {
45- return {
46- ...state ,
47- routeNames,
48- routes : state . routes . filter ( route => routeNames . includes ( route . name ) ) ,
49- } ;
50- } ,
51-
52- getStateForAction ( state , action ) {
53- switch ( action . type ) {
54- case 'UPDATE' :
55- return { ...state } ;
56-
57- case 'NOOP' :
58- return state ;
59-
60- default :
61- return null ;
62- }
63- } ,
64-
65- getStateForChildUpdate ( state , { update, focus, key } ) {
66- const index = state . routes . findIndex ( r => r . key === key ) ;
67-
68- if ( index === - 1 ) {
69- return state ;
70- }
71-
72- return {
73- ...state ,
74- index : focus ? index : state . index ,
75- routes : state . routes . map ( ( route , i ) =>
76- i === index ? { ...route , state : update } : route
77- ) ,
78- } ;
79- } ,
80-
81- shouldActionPropagateToChildren ( ) {
82- return false ;
83- } ,
84-
85- shouldActionChangeFocus ( ) {
86- return false ;
87- } ,
88-
89- actionCreators : { } ,
90- } ;
6+ import MockRouter from './__fixtures__/MockRouter' ;
917
928beforeEach ( ( ) => ( MockRouter . key = 0 ) ) ;
939
@@ -399,75 +315,6 @@ it('cleans up state when the navigator unmounts', () => {
399315 expect ( onStateChange ) . lastCalledWith ( undefined ) ;
400316} ) ;
401317
402- it ( "lets parent handle the action if child didn't" , ( ) => {
403- const ParentRouter : Router < { type : string } > = {
404- ...MockRouter ,
405-
406- getStateForAction ( state , action ) {
407- if ( action . type === 'REVERSE' ) {
408- return {
409- ...state ,
410- routes : state . routes . slice ( ) . reverse ( ) ,
411- } ;
412- }
413-
414- return MockRouter . getStateForAction ( state , action ) ;
415- } ,
416- } ;
417-
418- const ParentNavigator = ( props : any ) => {
419- const { state, descriptors } = useNavigationBuilder ( ParentRouter , props ) ;
420-
421- return descriptors [ state . routes [ state . index ] . key ] . render ( ) ;
422- } ;
423-
424- const ChildNavigator = ( props : any ) => {
425- const { state, descriptors } = useNavigationBuilder ( MockRouter , props ) ;
426-
427- return descriptors [ state . routes [ state . index ] . key ] . render ( ) ;
428- } ;
429-
430- const TestScreen = ( props : any ) => {
431- React . useEffect ( ( ) => {
432- props . navigation . dispatch ( { type : 'REVERSE' } ) ;
433-
434- // eslint-disable-next-line react-hooks/exhaustive-deps
435- } , [ ] ) ;
436-
437- return null ;
438- } ;
439-
440- const onStateChange = jest . fn ( ) ;
441-
442- render (
443- < NavigationContainer onStateChange = { onStateChange } >
444- < ParentNavigator initialRouteName = "baz" >
445- < Screen name = "foo" > { ( ) => null } </ Screen >
446- < Screen name = "bar" > { ( ) => null } </ Screen >
447- < Screen name = "baz" >
448- { ( ) => (
449- < ChildNavigator >
450- < Screen name = "qux" component = { TestScreen } />
451- </ ChildNavigator >
452- ) }
453- </ Screen >
454- </ ParentNavigator >
455- </ NavigationContainer >
456- ) ;
457-
458- expect ( onStateChange ) . toBeCalledTimes ( 1 ) ;
459- expect ( onStateChange ) . lastCalledWith ( {
460- index : 2 ,
461- key : '0' ,
462- routeNames : [ 'foo' , 'bar' , 'baz' ] ,
463- routes : [
464- { key : 'baz' , name : 'baz' } ,
465- { key : 'bar' , name : 'bar' } ,
466- { key : 'foo' , name : 'foo' } ,
467- ] ,
468- } ) ;
469- } ) ;
470-
471318it ( 'allows arbitrary state updates by dispatching a function' , ( ) => {
472319 const TestNavigator = ( props : any ) => {
473320 const { state, descriptors } = useNavigationBuilder ( MockRouter , props ) ;
0 commit comments