diff --git a/packages/react-navigation/src/__tests__/addNavigationHelpers-test.js b/packages/react-navigation/src/__tests__/addNavigationHelpers-test.js deleted file mode 100644 index 642d7677..00000000 --- a/packages/react-navigation/src/__tests__/addNavigationHelpers-test.js +++ /dev/null @@ -1,144 +0,0 @@ -import NavigationActions from '../NavigationActions'; -import addNavigationHelpers from '../addNavigationHelpers'; - -const dummyEventSubscriber = (name: string, handler: (*) => void) => ({ - remove: () => {}, -}); - -describe('addNavigationHelpers', () => { - it('handles dismiss action', () => { - const mockedDispatch = jest - .fn(() => false) - .mockImplementationOnce(() => true); - const child = { key: 'A', routeName: 'Home' }; - expect( - addNavigationHelpers({ - state: child, - dispatch: mockedDispatch, - addListener: dummyEventSubscriber, - dangerouslyGetParent: () => ({ - state: { - key: 'P', - routeName: 'Parent', - routes: [child], - }, - }), - }).dismiss() - ).toEqual(true); - expect(mockedDispatch).toBeCalledWith({ - type: NavigationActions.BACK, - key: 'P', - }); - expect(mockedDispatch.mock.calls.length).toBe(1); - }); - - it('handles Back action', () => { - const mockedDispatch = jest - .fn(() => false) - .mockImplementationOnce(() => true); - expect( - addNavigationHelpers({ - state: { key: 'A', routeName: 'Home' }, - dispatch: mockedDispatch, - addListener: dummyEventSubscriber, - }).goBack('A') - ).toEqual(true); - expect(mockedDispatch).toBeCalledWith({ - type: NavigationActions.BACK, - key: 'A', - }); - expect(mockedDispatch.mock.calls.length).toBe(1); - }); - - it('handles Back action when the key is not defined', () => { - const mockedDispatch = jest - .fn(() => false) - .mockImplementationOnce(() => true); - expect( - addNavigationHelpers({ - state: { routeName: 'Home' }, - dispatch: mockedDispatch, - addListener: dummyEventSubscriber, - }).goBack() - ).toEqual(true); - expect(mockedDispatch).toBeCalledWith({ type: NavigationActions.BACK }); - expect(mockedDispatch.mock.calls.length).toBe(1); - }); - - it('handles Navigate action', () => { - const mockedDispatch = jest - .fn(() => false) - .mockImplementationOnce(() => true); - expect( - addNavigationHelpers({ - state: { routeName: 'Home' }, - dispatch: mockedDispatch, - addListener: dummyEventSubscriber, - }).navigate('Profile', { name: 'Matt' }) - ).toEqual(true); - expect(mockedDispatch).toBeCalledWith({ - type: NavigationActions.NAVIGATE, - params: { name: 'Matt' }, - routeName: 'Profile', - }); - expect(mockedDispatch.mock.calls.length).toBe(1); - }); - - it('handles SetParams action', () => { - const mockedDispatch = jest - .fn(() => false) - .mockImplementationOnce(() => true); - expect( - addNavigationHelpers({ - state: { key: 'B', routeName: 'Settings' }, - dispatch: mockedDispatch, - addListener: dummyEventSubscriber, - }).setParams({ notificationsEnabled: 'yes' }) - ).toEqual(true); - expect(mockedDispatch).toBeCalledWith({ - type: NavigationActions.SET_PARAMS, - key: 'B', - params: { notificationsEnabled: 'yes' }, - }); - expect(mockedDispatch.mock.calls.length).toBe(1); - }); - - it('handles GetParams action', () => { - const mockedDispatch = jest - .fn(() => false) - .mockImplementationOnce(() => true); - expect( - addNavigationHelpers({ - state: { key: 'B', routeName: 'Settings', params: { name: 'Peter' } }, - dispatch: mockedDispatch, - addListener: dummyEventSubscriber, - }).getParam('name', 'Brent') - ).toEqual('Peter'); - }); - - it('handles GetParams action with default param', () => { - const mockedDispatch = jest - .fn(() => false) - .mockImplementationOnce(() => true); - expect( - addNavigationHelpers({ - state: { key: 'B', routeName: 'Settings' }, - dispatch: mockedDispatch, - addListener: dummyEventSubscriber, - }).getParam('name', 'Brent') - ).toEqual('Brent'); - }); - - it('handles GetParams action with param value as null', () => { - const mockedDispatch = jest - .fn(() => false) - .mockImplementationOnce(() => true); - expect( - addNavigationHelpers({ - state: { key: 'B', routeName: 'Settings', params: { name: null } }, - dispatch: mockedDispatch, - addListener: dummyEventSubscriber, - }).getParam('name') - ).toEqual(null); - }); -}); diff --git a/packages/react-navigation/src/addNavigationHelpers.js b/packages/react-navigation/src/addNavigationHelpers.js deleted file mode 100644 index 57bbd0f7..00000000 --- a/packages/react-navigation/src/addNavigationHelpers.js +++ /dev/null @@ -1,105 +0,0 @@ -/* Helpers for navigation */ - -import NavigationActions from './NavigationActions'; -import invariant from './utils/invariant'; -export default function(navigation) { - return { - ...navigation, - // Go back from the given key, default to active key - goBack: key => { - let actualizedKey = key; - if (key === undefined && navigation.state.key) { - invariant( - typeof navigation.state.key === 'string', - 'key should be a string' - ); - actualizedKey = navigation.state.key; - } - return navigation.dispatch( - NavigationActions.back({ key: actualizedKey }) - ); - }, - // Go back from the parent key. If this is a nested stack, the entire - // stack will be dismissed. - dismiss: () => { - let parent = navigation.dangerouslyGetParent(); - if (parent && parent.state) { - return navigation.dispatch( - NavigationActions.back({ key: parent.state.key }) - ); - } else { - return false; - } - }, - navigate: (navigateTo, params, action) => { - if (typeof navigateTo === 'string') { - return navigation.dispatch( - NavigationActions.navigate({ routeName: navigateTo, params, action }) - ); - } - invariant( - typeof navigateTo === 'object', - 'Must navigateTo an object or a string' - ); - invariant( - params == null, - 'Params must not be provided to .navigate() when specifying an object' - ); - invariant( - action == null, - 'Child action must not be provided to .navigate() when specifying an object' - ); - return navigation.dispatch(NavigationActions.navigate(navigateTo)); - }, - pop: (n, params) => - navigation.dispatch( - NavigationActions.pop({ n, immediate: params && params.immediate }) - ), - popToTop: params => - navigation.dispatch( - NavigationActions.popToTop({ immediate: params && params.immediate }) - ), - /** - * For updating current route params. For example the nav bar title and - * buttons are based on the route params. - * This means `setParams` can be used to update nav bar for example. - */ - setParams: params => { - invariant( - navigation.state.key && typeof navigation.state.key === 'string', - 'setParams cannot be called by root navigator' - ); - const key = navigation.state.key; - return navigation.dispatch(NavigationActions.setParams({ params, key })); - }, - - getParam: (paramName, defaultValue) => { - const params = navigation.state.params; - - if (params && paramName in params) { - return params[paramName]; - } - - return defaultValue; - }, - - push: (routeName, params, action) => - navigation.dispatch( - NavigationActions.push({ routeName, params, action }) - ), - - replace: (routeName, params, action) => - navigation.dispatch( - NavigationActions.replace({ - routeName, - params, - action, - key: navigation.state.key, - }) - ), - - openDrawer: () => navigation.dispatch(NavigationActions.openDrawer()), - closeDrawer: () => navigation.dispatch(NavigationActions.closeDrawer()), - toggleDrawer: () => navigation.dispatch(NavigationActions.toggleDrawer()), - }; -} diff --git a/packages/react-navigation/src/routers/__tests__/createConfigGetter-test.js b/packages/react-navigation/src/routers/__tests__/createConfigGetter-test.js index 3852598f..0a79d886 100644 --- a/packages/react-navigation/src/routers/__tests__/createConfigGetter-test.js +++ b/packages/react-navigation/src/routers/__tests__/createConfigGetter-test.js @@ -1,6 +1,5 @@ import { Component } from 'react'; import createConfigGetter from '../createConfigGetter'; -import addNavigationHelpers from '../../addNavigationHelpers'; const dummyEventSubscriber = (name: string, handler: (*) => void) => ({ remove: () => {}, @@ -67,81 +66,81 @@ test('should get config for screen', () => { expect( getScreenOptions( - addNavigationHelpers({ + { state: routes[0], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ).title ).toEqual('Welcome anonymous'); expect( getScreenOptions( - addNavigationHelpers({ + { state: routes[1], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ).title ).toEqual('Welcome jane'); expect( getScreenOptions( - addNavigationHelpers({ + { state: routes[0], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ).gesturesEnabled ).toEqual(true); expect( getScreenOptions( - addNavigationHelpers({ + { state: routes[2], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ).title ).toEqual('Settings!!!'); expect( getScreenOptions( - addNavigationHelpers({ + { state: routes[2], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ).gesturesEnabled ).toEqual(false); expect( getScreenOptions( - addNavigationHelpers({ + { state: routes[3], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ).title ).toEqual('10 new notifications'); expect( getScreenOptions( - addNavigationHelpers({ + { state: routes[3], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ).gesturesEnabled ).toEqual(true); expect( getScreenOptions( - addNavigationHelpers({ + { state: routes[4], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ).gesturesEnabled ).toEqual(false); @@ -164,11 +163,11 @@ test('should throw if the route does not exist', () => { expect(() => getScreenOptions( - addNavigationHelpers({ + { state: routes[0], dispatch: () => false, addListener: dummyEventSubscriber, - }), + }, {} ) ).toThrowError(