|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + useNavigationBuilder, |
| 4 | + createNavigatorFactory, |
| 5 | + StackRouter, |
| 6 | + TabRouter, |
| 7 | + NavigationHelpersContext, |
| 8 | + NavigationContainerRef, |
| 9 | +} from '@react-navigation/core'; |
| 10 | +import { act, render } from 'react-native-testing-library'; |
| 11 | +import NavigationContainer from '../NavigationContainer'; |
| 12 | +import window from '../__mocks__/window'; |
| 13 | + |
| 14 | +// @ts-ignore |
| 15 | +global.window = window; |
| 16 | + |
| 17 | +// We want to use the web version of useLinking |
| 18 | +jest.mock('../useLinking', () => require('../useLinking.tsx').default); |
| 19 | + |
| 20 | +it('integrates with the history API', () => { |
| 21 | + jest.useFakeTimers(); |
| 22 | + |
| 23 | + const createStackNavigator = createNavigatorFactory((props: any) => { |
| 24 | + const { navigation, state, descriptors } = useNavigationBuilder( |
| 25 | + StackRouter, |
| 26 | + props |
| 27 | + ); |
| 28 | + |
| 29 | + return ( |
| 30 | + <NavigationHelpersContext.Provider value={navigation}> |
| 31 | + {state.routes.map((route, i) => ( |
| 32 | + <div key={route.key} aria-current={state.index === i || undefined}> |
| 33 | + {descriptors[route.key].render()} |
| 34 | + </div> |
| 35 | + ))} |
| 36 | + </NavigationHelpersContext.Provider> |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + const createTabNavigator = createNavigatorFactory((props: any) => { |
| 41 | + const { navigation, state, descriptors } = useNavigationBuilder( |
| 42 | + TabRouter, |
| 43 | + props |
| 44 | + ); |
| 45 | + |
| 46 | + return ( |
| 47 | + <NavigationHelpersContext.Provider value={navigation}> |
| 48 | + {state.routes.map((route, i) => ( |
| 49 | + <div key={route.key} aria-current={state.index === i || undefined}> |
| 50 | + {descriptors[route.key].render()} |
| 51 | + </div> |
| 52 | + ))} |
| 53 | + </NavigationHelpersContext.Provider> |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + const Stack = createStackNavigator(); |
| 58 | + const Tab = createTabNavigator(); |
| 59 | + |
| 60 | + const TestScreen = ({ route }: any): any => |
| 61 | + `${route.name} ${JSON.stringify(route.params)}`; |
| 62 | + |
| 63 | + const linking = { |
| 64 | + prefixes: [], |
| 65 | + config: { |
| 66 | + Home: { |
| 67 | + path: '', |
| 68 | + initialRouteName: 'Feed', |
| 69 | + screens: { |
| 70 | + Profile: ':user', |
| 71 | + Settings: 'edit', |
| 72 | + Updates: 'updates', |
| 73 | + Feed: 'feed', |
| 74 | + }, |
| 75 | + }, |
| 76 | + Chat: 'chat', |
| 77 | + }, |
| 78 | + }; |
| 79 | + |
| 80 | + const navigation = React.createRef<NavigationContainerRef>(); |
| 81 | + |
| 82 | + render( |
| 83 | + <NavigationContainer ref={navigation} linking={linking}> |
| 84 | + <Tab.Navigator> |
| 85 | + <Tab.Screen name="Home"> |
| 86 | + {() => ( |
| 87 | + <Stack.Navigator initialRouteName="Feed"> |
| 88 | + <Stack.Screen name="Profile" component={TestScreen} /> |
| 89 | + <Stack.Screen name="Settings" component={TestScreen} /> |
| 90 | + <Stack.Screen name="Feed" component={TestScreen} /> |
| 91 | + <Stack.Screen name="Updates" component={TestScreen} /> |
| 92 | + </Stack.Navigator> |
| 93 | + )} |
| 94 | + </Tab.Screen> |
| 95 | + <Tab.Screen name="Chat" component={TestScreen} /> |
| 96 | + </Tab.Navigator> |
| 97 | + </NavigationContainer> |
| 98 | + ); |
| 99 | + |
| 100 | + expect(window.location.pathname).toBe('/feed'); |
| 101 | + |
| 102 | + act(() => navigation.current?.navigate('Profile', { user: 'jane' })); |
| 103 | + |
| 104 | + expect(window.location.pathname).toBe('/jane'); |
| 105 | + |
| 106 | + act(() => navigation.current?.navigate('Updates')); |
| 107 | + |
| 108 | + expect(window.location.pathname).toBe('/updates'); |
| 109 | + |
| 110 | + act(() => navigation.current?.goBack()); |
| 111 | + |
| 112 | + jest.runAllTimers(); |
| 113 | + |
| 114 | + expect(window.location.pathname).toBe('/jane'); |
| 115 | + |
| 116 | + act(() => { |
| 117 | + window.history.back(); |
| 118 | + jest.runAllTimers(); |
| 119 | + }); |
| 120 | + |
| 121 | + expect(window.location.pathname).toBe('/feed'); |
| 122 | + |
| 123 | + act(() => { |
| 124 | + window.history.forward(); |
| 125 | + jest.runAllTimers(); |
| 126 | + }); |
| 127 | + |
| 128 | + expect(window.location.pathname).toBe('/jane'); |
| 129 | + |
| 130 | + act(() => navigation.current?.navigate('Settings')); |
| 131 | + |
| 132 | + expect(window.location.pathname).toBe('/edit'); |
| 133 | + |
| 134 | + act(() => { |
| 135 | + window.history.go(-2); |
| 136 | + jest.runAllTimers(); |
| 137 | + }); |
| 138 | + |
| 139 | + expect(window.location.pathname).toBe('/feed'); |
| 140 | + |
| 141 | + act(() => navigation.current?.navigate('Settings')); |
| 142 | + act(() => navigation.current?.navigate('Chat')); |
| 143 | + |
| 144 | + expect(window.location.pathname).toBe('/chat'); |
| 145 | + |
| 146 | + act(() => navigation.current?.navigate('Home')); |
| 147 | + |
| 148 | + expect(window.location.pathname).toBe('/edit'); |
| 149 | +}); |
0 commit comments