Stack completion action now targets toChildKey

This commit is contained in:
Eric Vicenti
2018-10-29 18:58:33 -07:00
parent af888dccb3
commit a3372561d0
2 changed files with 67 additions and 8 deletions

View File

@@ -226,6 +226,7 @@ export default (routeConfigs, stackConfig = {}) => {
} else if (action.type === NavigationActions.NAVIGATE) {
// Traverse routes from the top of the stack to the bottom, so the
// active route has the first opportunity, then the one before it, etc.
for (let childRoute of state.routes.slice().reverse()) {
let childRouter = childRouters[childRoute.routeName];
let childAction =
@@ -449,6 +450,7 @@ export default (routeConfigs, stackConfig = {}) => {
if (
action.type === StackActions.COMPLETE_TRANSITION &&
(action.key == null || action.key === state.key) &&
action.toChildKey === state.routes[state.index].key &&
state.isTransitioning
) {
return {

View File

@@ -907,10 +907,10 @@ describe('StackRouter', () => {
},
state
);
expect(state2 && state2.index).toEqual(1);
expect(state2 && state2.routes[1].routeName).toEqual('Bar');
expect(state2 && state2.routes[1].params).toEqual({ name: 'Zoom' });
expect(state2 && state2.routes.length).toEqual(2);
expect(state2.index).toEqual(1);
expect(state2.routes[1].routeName).toEqual('Bar');
expect(state2.routes[1].params).toEqual({ name: 'Zoom' });
expect(state2.routes.length).toEqual(2);
const state3 = router.getStateForAction(
{ type: NavigationActions.BACK, immediate: true },
state2
@@ -1059,6 +1059,7 @@ describe('StackRouter', () => {
const state3 = router.getStateForAction(
{
type: StackActions.COMPLETE_TRANSITION,
toChildKey: state2.routes[1].key,
},
state2
);
@@ -1066,6 +1067,61 @@ describe('StackRouter', () => {
expect(state3 && state3.isTransitioning).toEqual(false);
});
test('Completion action does not work with incorrect key', () => {
const FooScreen = () => <div />;
const router = StackRouter({
Foo: {
screen: FooScreen,
},
Bar: {
screen: FooScreen,
},
});
const state = {
key: 'StackKey',
index: 1,
isTransitioning: true,
routes: [{ key: 'a', routeName: 'Foo' }, { key: 'b', routeName: 'Foo' }],
};
const outputState = router.getStateForAction(
{
type: StackActions.COMPLETE_TRANSITION,
toChildKey: state.routes[state.index].key,
key: 'not StackKey',
},
state
);
expect(outputState.isTransitioning).toEqual(true);
});
test('Completion action does not work with incorrect toChildKey', () => {
const FooScreen = () => <div />;
const router = StackRouter({
Foo: {
screen: FooScreen,
},
Bar: {
screen: FooScreen,
},
});
const state = {
key: 'StackKey',
index: 1,
isTransitioning: true,
routes: [{ key: 'a', routeName: 'Foo' }, { key: 'b', routeName: 'Foo' }],
};
const outputState = router.getStateForAction(
{
type: StackActions.COMPLETE_TRANSITION,
// for this action to toggle isTransitioning, toChildKey should be state.routes[state.index].key,
toChildKey: 'incorrect',
key: 'StackKey',
},
state
);
expect(outputState.isTransitioning).toEqual(true);
});
test('Back action parent is prioritized over inactive child routers', () => {
const Bar = () => <div />;
Bar.router = StackRouter({
@@ -1842,14 +1898,15 @@ describe('StackRouter', () => {
},
state
);
expect(state2 && state2.index).toEqual(0);
expect(state2 && state2.isTransitioning).toEqual(false);
expect(state2 && state2.routes[0].index).toEqual(1);
expect(state2 && state2.routes[0].isTransitioning).toEqual(true);
expect(state2.index).toEqual(0);
expect(state2.isTransitioning).toEqual(false);
expect(state2.routes[0].index).toEqual(1);
expect(state2.routes[0].isTransitioning).toEqual(true);
expect(!!key).toEqual(true);
const state3 = router.getStateForAction(
{
type: StackActions.COMPLETE_TRANSITION,
toChildKey: state2.routes[0].routes[1].key,
},
state2
);