StackRouter to return null on idempotent navigation (#3793)

This new behavior indicates that the action has been handled, but the state has not changed.
This commit is contained in:
Eric Vicenti
2018-03-19 16:46:57 -04:00
parent 64e467e465
commit 0de40c6464
2 changed files with 17 additions and 7 deletions

View File

@@ -209,7 +209,7 @@ export default (routeConfigs, stackConfig = {}) => {
if (action.type !== NavigationActions.PUSH && lastRouteIndex !== -1) {
// If index is unchanged and params are not being set, leave state identity intact
if (state.index === lastRouteIndex && !action.params) {
return state;
return null;
}
// Remove the now unused routes at the tail of the routes array

View File

@@ -539,8 +539,7 @@ describe('StackRouter', () => {
NavigationActions.navigate({ routeName: 'bar' }),
barState
);
expect(navigateOnBarState.index).toEqual(1);
expect(navigateOnBarState.routes[1].routeName).toEqual('bar');
expect(navigateOnBarState).toEqual(null);
});
test('Navigate focuses given routeName if already active in stack', () => {
@@ -640,8 +639,7 @@ describe('StackRouter', () => {
NavigationActions.navigate({ routeName: 'foo', key: 'foo' }),
initState
);
expect(pushedState.index).toEqual(0);
expect(pushedState.routes[0].routeName).toEqual('foo');
expect(pushedState).toEqual(null);
});
test('Navigate with key is idempotent', () => {
@@ -660,8 +658,20 @@ describe('StackRouter', () => {
NavigationActions.navigate({ routeName: 'bar', key: 'a' }),
pushedState
);
expect(pushedTwiceState.index).toEqual(1);
expect(pushedTwiceState.routes[1].routeName).toEqual('bar');
expect(pushedTwiceState).toEqual(null);
});
test('Navigate to current routeName returns null to indicate handled action', () => {
const TestRouter = StackRouter({
foo: { screen: () => <div /> },
bar: { screen: () => <div /> },
});
const initState = TestRouter.getStateForAction(NavigationActions.init());
const navigatedState = TestRouter.getStateForAction(
NavigationActions.navigate({ routeName: 'foo' }),
initState
);
expect(navigatedState).toBe(null);
});
test('Push behaves like navigate, except for key', () => {