From 84c16a7d411afe4bae2995d4c59cd649a4df2ca4 Mon Sep 17 00:00:00 2001 From: "satyajit.happy" Date: Sat, 13 Jul 2019 02:38:30 +0200 Subject: [PATCH] test: add test for dispatching a function --- src/__tests__/index.test.tsx | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/__tests__/index.test.tsx b/src/__tests__/index.test.tsx index b12ac652..2ce377e0 100644 --- a/src/__tests__/index.test.tsx +++ b/src/__tests__/index.test.tsx @@ -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 = ( + + expect(state).toEqual({ + index: 1, + key: 'root', + names: ['foo', 'bar'], + routes: [{ key: 'bar', name: 'bar' }, { key: 'foo', name: 'foo' }], + }) + } + > + + + + + + ); + + render(element).update(element); +}); + it('throws if navigator is not inside a container', () => { expect.assertions(1);