test: add test for dispatching a function

This commit is contained in:
satyajit.happy
2019-07-13 02:38:30 +02:00
parent 4a42ec4b2b
commit 84c16a7d41

View File

@@ -82,6 +82,53 @@ it('initializes state for a navigator on navigation', () => {
render(element).update(element);
});
it('allows arbitrary state updates by dispatching a function', () => {
expect.assertions(1);
const TestNavigator = (props: any) => {
const { navigation, descriptors } = useNavigationBuilder(MockRouter, props);
return descriptors[
navigation.state.routes[navigation.state.index].key
].render();
};
const FooScreen = (props: any) => {
React.useEffect(() => {
props.navigation.dispatch((state: any) => ({
...state,
routes: state.routes.slice().reverse(),
index: 1,
}));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return null;
};
const BarScreen = () => null;
const element = (
<NavigationContainer
onStateChange={state =>
expect(state).toEqual({
index: 1,
key: 'root',
names: ['foo', 'bar'],
routes: [{ key: 'bar', name: 'bar' }, { key: 'foo', name: 'foo' }],
})
}
>
<TestNavigator initialRouteName="foo">
<Screen name="foo" component={FooScreen} />
<Screen name="bar" component={BarScreen} />
</TestNavigator>
</NavigationContainer>
);
render(element).update(element);
});
it('throws if navigator is not inside a container', () => {
expect.assertions(1);