Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit 38c7d18

Browse files
hsourcesatya164
authored andcommitted
fix: avoid error when updateNextStateHistory called with state
1 parent 3d43ed1 commit 38c7d18

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/routers/SwitchRouter.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ export default (routeConfigs, config = {}) => {
8282

8383
function getNextState(action, prevState, possibleNextState) {
8484
function updateNextStateHistory(prevState, nextState) {
85-
if (backBehavior !== 'history' || nextState.index === prevState.index) {
85+
if (
86+
backBehavior !== 'history' ||
87+
(prevState && nextState && nextState.index === prevState.index)
88+
) {
8689
return nextState;
8790
}
8891
let nextRouteKeyHistory = prevState ? prevState.routeKeyHistory : [];
@@ -104,6 +107,7 @@ export default (routeConfigs, config = {}) => {
104107
let nextState = possibleNextState;
105108
if (
106109
prevState &&
110+
possibleNextState &&
107111
prevState.index !== possibleNextState.index &&
108112
resetOnBlur
109113
) {

src/routers/__tests__/SwitchRouter-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ describe('SwitchRouter', () => {
231231
navigateTo('Home');
232232
expect(getSubState(1).routeName).toEqual('Home');
233233
});
234+
235+
it('does not error for a nested navigate action in an uninitialized history router', () => {
236+
const { navigateTo, getSubState } = getRouterTestHelper(
237+
getExampleRouter({ backBehavior: 'history' }),
238+
{ skipInitializeState: true }
239+
);
240+
241+
navigateTo('B', {
242+
action: NavigationActions.navigate({ routeName: 'B2' }),
243+
});
244+
expect(getSubState(1).routeName).toEqual('B');
245+
expect(getSubState(2).routeName).toEqual('B2');
246+
});
234247
});
235248

236249
const getExampleRouter = (config = {}) => {

src/routers/__tests__/routerTestHelper.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import * as SwitchActions from '../../routers/SwitchActions';
66
// it's often convenient to manipulate a structure that keeps the router state to avoid
77
// creating many state1, state2, state3 local variables which are prone to typos...
88

9-
const defaultInitAction = {
10-
type: NavigationActions.INIT,
9+
const defaultOptions = {
10+
skipInitializeState: false,
1111
};
1212

13-
export const getRouterTestHelper = (router, initAction = defaultInitAction) => {
14-
let state = router.getStateForAction(initAction);
13+
export const getRouterTestHelper = (router, options = defaultOptions) => {
14+
let state =
15+
options && options.skipInitializeState
16+
? undefined
17+
: router.getStateForAction({ type: NavigationActions.INIT });
1518

1619
const applyAction = action => {
1720
state = router.getStateForAction(action, state);

0 commit comments

Comments
 (0)