|
1 |
| -import * as React from 'react'; |
2 |
| -import { View, Button } from 'react-native'; |
| 1 | +import { default as React, useState } from 'react'; |
3 | 2 | import * as renderer from 'react-test-renderer';
|
4 |
| -import { createSwitchNavigator } from '@react-navigation/core'; |
| 3 | +import { |
| 4 | + createSwitchNavigator, |
| 5 | + NavigationActions, |
| 6 | +} from '@react-navigation/core'; |
5 | 7 | import { createAppContainer } from '@react-navigation/native';
|
| 8 | +// TODO: move to "react-navigation" when https://github.com/react-navigation/react-navigation/pull/5276 |
| 9 | +// get merged |
| 10 | +import { NavigationEventPayload } from 'react-navigation-types-only'; |
6 | 11 |
|
7 | 12 | import {
|
8 |
| - useNavigation |
| 13 | + useNavigation, |
| 14 | + useNavigationParam, |
| 15 | + useNavigationState, |
| 16 | + useNavigationKey, |
| 17 | + useNavigationEvents, |
| 18 | + useFocusState, |
9 | 19 | } from '../../dist/Hooks';
|
10 | 20 |
|
11 | 21 | const HomeScreen = () => {
|
12 | 22 | const { navigate } = useNavigation();
|
13 |
| - return <Button title="Go to Details" onPress={() => navigate('Details')} />; |
14 |
| -} |
| 23 | + return navigate('Details', { from: 'Home' }); |
| 24 | +}; |
| 25 | + |
| 26 | +const DetailsScreen = () => { |
| 27 | + const from = useNavigationParam('from'); |
| 28 | + return <p>{from}</p>; |
| 29 | +}; |
| 30 | + |
| 31 | +const OtherScreen = () => { |
| 32 | + const { routeName } = useNavigationState(); |
| 33 | + return <p>{routeName}</p>; |
| 34 | +}; |
| 35 | + |
| 36 | +const KeyScreen = () => { |
| 37 | + const key = useNavigationKey(); |
| 38 | + return <p>{key}</p>; |
| 39 | +}; |
15 | 40 |
|
16 |
| -const DetailsScreen = () => <View />; |
| 41 | +const EventScreen = () => { |
| 42 | + const [events, setEvents] = useState([] as NavigationEventPayload[]); |
| 43 | + useNavigationEvents((evt) => { |
| 44 | + // latest state on evt.state |
| 45 | + // prev state on evt.lastState |
| 46 | + // triggering navigation action on evt.action |
| 47 | + |
| 48 | + setEvents(events => [...events, evt]); |
| 49 | + }); |
| 50 | + // evt.type is [will/did][Focus/Blur] |
| 51 | + return ( |
| 52 | + <> |
| 53 | + {events.map((evt, i) => ( |
| 54 | + <p key={i}>{evt.type}</p> |
| 55 | + ))} |
| 56 | + </> |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +const FocusScreen = () => ( |
| 61 | + <p>{useFocusState().isFocused ? 'Focused' : 'Not Focused'}</p> |
| 62 | +); |
17 | 63 |
|
18 |
| -const AppNavigator = createSwitchNavigator( |
| 64 | +const AppNavigator1 = createSwitchNavigator( |
19 | 65 | {
|
20 |
| - Home: HomeScreen, |
21 |
| - Details: DetailsScreen, |
| 66 | + Home: HomeScreen, // { nav: { "index": 0 ... |
| 67 | + Details: DetailsScreen, // { nav: { "index": 1 ... |
| 68 | + Other: OtherScreen, // { nav: { "index": 2 ... |
22 | 69 | },
|
23 | 70 | {
|
24 | 71 | initialRouteName: 'Home',
|
| 72 | + } |
| 73 | +); |
| 74 | + |
| 75 | +const AppNavigator2 = createSwitchNavigator( |
| 76 | + { |
| 77 | + Other: OtherScreen, |
| 78 | + Key: KeyScreen, |
| 79 | + Event: EventScreen, |
| 80 | + Focus: FocusScreen, |
25 | 81 | },
|
| 82 | + { |
| 83 | + initialRouteName: 'Other', |
| 84 | + } |
26 | 85 | );
|
27 | 86 |
|
28 |
| -const App = createAppContainer(AppNavigator); |
| 87 | +describe('AppNavigator1 Stack', () => { |
| 88 | + const App = createAppContainer(AppNavigator1); |
| 89 | + let navigationContainer; |
| 90 | + beforeEach(() => { |
| 91 | + navigationContainer = renderer.create(<App />); |
| 92 | + }); |
29 | 93 |
|
30 |
| -describe('Navigating to a new screen', () => { |
31 |
| - it('renders successfully', () => { |
32 |
| - const rendered = renderer.create(<App />); |
33 |
| - let tree = rendered.toJSON(); |
34 |
| - expect(tree).toMatchSnapshot(); |
| 94 | + it('useNavigation: Navigating to "DetailsScreen"', () => { |
| 95 | + const instance = navigationContainer.getInstance() as { state?: any }; |
| 96 | + expect(instance.state.nav).toMatchObject({ index: 1 }); |
| 97 | + }); |
35 | 98 |
|
36 |
| - // Manually trigger onPress |
37 |
| - //tree!.props.onPress(); |
38 |
| - tree = rendered.toJSON(); |
| 99 | + it('useNavigationParam: Get passed parameter', () => { |
| 100 | + const children = navigationContainer.toJSON().children; |
| 101 | + expect(children).toContain('Home'); |
| 102 | + }); |
| 103 | + |
| 104 | + afterEach(() => { |
| 105 | + navigationContainer.unmount(); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('AppNavigator2 Stack', () => { |
| 110 | + const App = createAppContainer(AppNavigator2); |
| 111 | + let navigationContainer; |
| 112 | + beforeEach(() => { |
| 113 | + navigationContainer = renderer.create(<App />); |
| 114 | + }); |
| 115 | + |
| 116 | + const eventTypes = ['willFocus', 'didFocus', 'willBlur', 'didBlur', 'action']; |
| 117 | + |
| 118 | + it('usenNavigationState: Get current route name', () => { |
| 119 | + const children = navigationContainer.toJSON().children; |
| 120 | + expect(children).toContain('Other'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('usenNavigationKey: Get current key name', () => { |
| 124 | + const instance = navigationContainer.getInstance(); |
| 125 | + instance.dispatch(NavigationActions.navigate({ routeName: 'Key' })); |
| 126 | + const children = navigationContainer.toJSON().children; |
| 127 | + expect(children).toContain('Key'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('useNavigationEvents: Get current subscribed events', () => { |
| 131 | + const instance = navigationContainer.getInstance(); |
| 132 | + instance.dispatch(NavigationActions.navigate({ routeName: 'Event' })); |
| 133 | + const elems = navigationContainer.toJSON(); |
| 134 | + expect(eventTypes).toContain(elems[0].children.toString()); |
| 135 | + expect(eventTypes).toContain(elems[1].children.toString()); |
| 136 | + }); |
| 137 | + |
| 138 | + it('useFocusState: Get focus state', () => { |
| 139 | + const instance = navigationContainer.getInstance(); |
| 140 | + instance.dispatch(NavigationActions.navigate({ routeName: 'Focus' })); |
| 141 | + const children = navigationContainer.toJSON().children; |
| 142 | + expect(children).toContain('Focused'); |
| 143 | + }); |
39 | 144 |
|
40 |
| - expect(rendered).toMatchSnapshot(); |
| 145 | + afterEach(() => { |
| 146 | + navigationContainer.unmount(); |
41 | 147 | });
|
42 | 148 | });
|
0 commit comments