From f9defd5afb171870aa015fd9c40e91adfa8ffe3b Mon Sep 17 00:00:00 2001 From: Harry Yu Date: Thu, 12 Dec 2019 17:08:41 -0800 Subject: [PATCH] fix: avoid error when updateNextStateHistory called with state --- packages/core/src/routers/SwitchRouter.js | 6 +++++- .../core/src/routers/__tests__/SwitchRouter-test.js | 13 +++++++++++++ .../core/src/routers/__tests__/routerTestHelper.js | 11 +++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/core/src/routers/SwitchRouter.js b/packages/core/src/routers/SwitchRouter.js index a0c406b7..294e3139 100644 --- a/packages/core/src/routers/SwitchRouter.js +++ b/packages/core/src/routers/SwitchRouter.js @@ -82,7 +82,10 @@ export default (routeConfigs, config = {}) => { function getNextState(action, prevState, possibleNextState) { function updateNextStateHistory(prevState, nextState) { - if (backBehavior !== 'history' || nextState.index === prevState.index) { + if ( + backBehavior !== 'history' || + (prevState && nextState && nextState.index === prevState.index) + ) { return nextState; } let nextRouteKeyHistory = prevState ? prevState.routeKeyHistory : []; @@ -104,6 +107,7 @@ export default (routeConfigs, config = {}) => { let nextState = possibleNextState; if ( prevState && + possibleNextState && prevState.index !== possibleNextState.index && resetOnBlur ) { diff --git a/packages/core/src/routers/__tests__/SwitchRouter-test.js b/packages/core/src/routers/__tests__/SwitchRouter-test.js index 5b0606bf..91423858 100644 --- a/packages/core/src/routers/__tests__/SwitchRouter-test.js +++ b/packages/core/src/routers/__tests__/SwitchRouter-test.js @@ -231,6 +231,19 @@ describe('SwitchRouter', () => { navigateTo('Home'); expect(getSubState(1).routeName).toEqual('Home'); }); + + it('does not error for a nested navigate action in an uninitialized history router', () => { + const { navigateTo, getSubState } = getRouterTestHelper( + getExampleRouter({ backBehavior: 'history' }), + { skipInitializeState: true } + ); + + navigateTo('B', { + action: NavigationActions.navigate({ routeName: 'B2' }), + }); + expect(getSubState(1).routeName).toEqual('B'); + expect(getSubState(2).routeName).toEqual('B2'); + }); }); const getExampleRouter = (config = {}) => { diff --git a/packages/core/src/routers/__tests__/routerTestHelper.js b/packages/core/src/routers/__tests__/routerTestHelper.js index 3ac1eb57..c4f766c4 100644 --- a/packages/core/src/routers/__tests__/routerTestHelper.js +++ b/packages/core/src/routers/__tests__/routerTestHelper.js @@ -6,12 +6,15 @@ import * as SwitchActions from '../../routers/SwitchActions'; // it's often convenient to manipulate a structure that keeps the router state to avoid // creating many state1, state2, state3 local variables which are prone to typos... -const defaultInitAction = { - type: NavigationActions.INIT, +const defaultOptions = { + skipInitializeState: false, }; -export const getRouterTestHelper = (router, initAction = defaultInitAction) => { - let state = router.getStateForAction(initAction); +export const getRouterTestHelper = (router, options = defaultOptions) => { + let state = + options && options.skipInitializeState + ? undefined + : router.getStateForAction({ type: NavigationActions.INIT }); const applyAction = action => { state = router.getStateForAction(action, state);