Add check for undefined prevState in SwitchRouter (#3856)

* Add check for undefined prevState in SwitchRouter (Fixes #3788)

* Add test coverage
This commit is contained in:
Sean Morton
2018-04-05 09:37:25 -07:00
committed by Brent Vatne
parent 3eeea48fa3
commit efa53aab44
2 changed files with 18 additions and 0 deletions

View File

@@ -77,6 +77,10 @@ export default (routeConfigs, config = {}) => {
},
getNextState(prevState, possibleNextState) {
if (!prevState) {
return possibleNextState;
}
let nextState;
if (prevState.index !== possibleNextState.index && resetOnBlur) {
const prevRouteName = prevState.routes[prevState.index].routeName;

View File

@@ -25,6 +25,20 @@ describe('SwitchRouter', () => {
expect(state3.routes[0].routes.length).toEqual(1);
});
test('sets the next state even if no previous state is provided', () => {
const router = getExampleRouter();
const initialState = router.getStateForAction({
type: NavigationActions.INIT,
});
const nextState = router.getStateForAction({
type: NavigationActions.NAVIGATE,
routeName: 'A2',
});
expect(nextState.routes[0].index).toEqual(1);
expect(nextState.routes[0].routes.length).toEqual(2);
});
test('does not reset the route on unfocus if resetOnBlur is false', () => {
const router = getExampleRouter({ resetOnBlur: false });
const state = router.getStateForAction({ type: NavigationActions.INIT });