diff --git a/src/routers/StackRouter.js b/src/routers/StackRouter.js index 00ba9b2d..5f6f7dbd 100644 --- a/src/routers/StackRouter.js +++ b/src/routers/StackRouter.js @@ -257,9 +257,13 @@ export default (routeConfigs, stackConfig = {}) => { let childRouter = childRouters[childRoute.routeName]; let debug = action.params && action.params.debug; + let childAction = + action.routeName === childRoute.routeName && action.action + ? action.action + : action; if (childRouter) { const nextRouteState = childRouter.getStateForAction( - action, + childAction, childRoute ); diff --git a/src/routers/__tests__/Routers-test.js b/src/routers/__tests__/Routers-test.js index 38e34457..33e3a5af 100644 --- a/src/routers/__tests__/Routers-test.js +++ b/src/routers/__tests__/Routers-test.js @@ -4,6 +4,7 @@ import React from 'react'; import StackRouter from '../StackRouter'; import TabRouter from '../TabRouter'; +import SwitchRouter from '../SwitchRouter'; import NavigationActions from '../../NavigationActions'; import { _TESTING_ONLY_normalize_keys } from '../KeyGenerator'; @@ -89,6 +90,75 @@ Object.keys(ROUTERS).forEach(routerName => { }); }); +test('Nested navigate behavior test', () => { + const Leaf = () =>
; + + const First = () => ; + First.router = StackRouter({ + First1: Leaf, + First2: Leaf, + }); + + const Second = () => ; + Second.router = StackRouter({ + Second1: Leaf, + Second2: Leaf, + }); + + const Main = () => ; + Main.router = StackRouter({ + First, + Second, + }); + const TestRouter = SwitchRouter({ + Login: Leaf, + Main, + }); + + const state1 = TestRouter.getStateForAction({ type: NavigationActions.INIT }); + + const state2 = TestRouter.getStateForAction( + { type: NavigationActions.NAVIGATE, routeName: 'First' }, + state1 + ); + expect(state2.index).toEqual(1); + expect(state2.routes[1].index).toEqual(0); + expect(state2.routes[1].routes[0].index).toEqual(0); + + const state3 = TestRouter.getStateForAction( + { type: NavigationActions.NAVIGATE, routeName: 'Second2' }, + state2 + ); + expect(state3.index).toEqual(1); + expect(state3.routes[1].index).toEqual(1); // second + expect(state3.routes[1].routes[1].index).toEqual(1); //second.second2 + + const state4 = TestRouter.getStateForAction( + { + type: NavigationActions.NAVIGATE, + routeName: 'First', + action: { type: NavigationActions.NAVIGATE, routeName: 'First2' }, + }, + state3, + true + ); + expect(state4.index).toEqual(1); // main + expect(state4.routes[1].index).toEqual(0); // first + expect(state4.routes[1].routes[0].index).toEqual(1); // first2 + + const state5 = TestRouter.getStateForAction( + { + type: NavigationActions.NAVIGATE, + routeName: 'First', + action: { type: NavigationActions.NAVIGATE, routeName: 'First1' }, + }, + state3 // second.second2 is active on state3 + ); + expect(state5.index).toEqual(1); // main + expect(state5.routes[1].index).toEqual(0); // first + expect(state5.routes[1].routes[0].index).toEqual(0); // first.first1 +}); + test('Handles no-op actions with tabs within stack router', () => { const BarView = () => ; const FooTabNavigator = () => ;