mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-01-12 22:51:18 +08:00
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:
@@ -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;
|
||||
|
||||
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user