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