Skip to content

Commit

Permalink
Don't delegate inner action to child router, handle inner action when…
Browse files Browse the repository at this point in the history
… handling NAVIGATE
  • Loading branch information
Patrick Monteith authored and Ashoat committed Dec 7, 2017
1 parent 5a26506 commit 950d0c6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/routers/TabRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ export default (
const routes = order.map((routeName: string) => {
const tabRouter = tabRouters[routeName];
if (tabRouter) {
const childAction =
action.action ||
NavigationActions.init({
...(action.params ? { params: action.params } : {}),
});
const childAction = NavigationActions.init({
...(action.params ? { params: action.params } : {}),
});
return {
...tabRouter.getStateForAction(childAction),
key: routeName,
Expand Down Expand Up @@ -106,7 +104,7 @@ export default (
const activeTabRouter = tabRouters[order[state.index]];
if (activeTabRouter) {
const activeTabState = activeTabRouter.getStateForAction(
action.action || action,
action,
activeTabLastState
);
if (!activeTabState && inputState) {
Expand Down
70 changes: 70 additions & 0 deletions src/routers/__tests__/TabRouter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

import React from 'react';
import TabRouter from '../TabRouter';
import StackRouter from '../StackRouter';

import NavigationActions from '../../NavigationActions';

import type { NavigationRoute, NavigationState } from '../../TypeDefinition';

const INIT_ACTION = { type: NavigationActions.INIT };

const BareLeafRouteConfig = {
Expand Down Expand Up @@ -604,4 +607,71 @@ describe('TabRouter', () => {
],
});
});

test('Inner actions are only unpacked if the current tab matches', () => {
const PlainScreen = () => <div />;
const ScreenA = () => <div />;
const ScreenB = () => <div />;
ScreenB.router = StackRouter({
Baz: { screen: PlainScreen },
Zoo: { screen: PlainScreen },
});
ScreenA.router = StackRouter({
Bar: { screen: PlainScreen },
Boo: { screen: ScreenB },
});
const router = TabRouter({
Foo: { screen: ScreenA },
});
const screenApreState = {
index: 0,
key: 'Init',
routeName: 'Foo',
routes: [{ key: 'Init', routeName: 'Bar' }],
};
const preState = {
index: 0,
routes: [screenApreState],
};

type ComparableRoute = {
routeName?: string,
routes?: Array<ComparableRoute>,
};

type RouteOrState =
| NavigationRoute
| NavigationState
| (NavigationRoute & NavigationState);

const comparable = (state: RouteOrState): ComparableRoute => {
let result = {};
if (typeof state.routeName === 'string') {
result = { ...result, routeName: state.routeName };
}
if (state.routes instanceof Array) {
result = {
...result,
routes: state.routes.map(comparable),
};
}
return result;
};

const action = NavigationActions.navigate({
routeName: 'Boo',
action: NavigationActions.navigate({ routeName: 'Zoo' }),
});

const expectedState = ScreenA.router.getStateForAction(
action,
screenApreState
);
const state = router.getStateForAction(action, preState);
const innerState = state ? state.routes[0] : state;

expect(expectedState && comparable(expectedState)).toEqual(
innerState && comparable(innerState)
);
});
});

0 comments on commit 950d0c6

Please sign in to comment.