Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit a80eb3d

Browse files
committed
Stack completion action now targets toChildKey
1 parent cdc4d91 commit a80eb3d

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

src/routers/StackRouter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export default (routeConfigs, stackConfig = {}) => {
226226
} else if (action.type === NavigationActions.NAVIGATE) {
227227
// Traverse routes from the top of the stack to the bottom, so the
228228
// active route has the first opportunity, then the one before it, etc.
229+
229230
for (let childRoute of state.routes.slice().reverse()) {
230231
let childRouter = childRouters[childRoute.routeName];
231232
let childAction =
@@ -449,6 +450,7 @@ export default (routeConfigs, stackConfig = {}) => {
449450
if (
450451
action.type === StackActions.COMPLETE_TRANSITION &&
451452
(action.key == null || action.key === state.key) &&
453+
action.toChildKey === state.routes[state.index].key &&
452454
state.isTransitioning
453455
) {
454456
return {

src/routers/__tests__/StackRouter-test.js

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -907,10 +907,10 @@ describe('StackRouter', () => {
907907
},
908908
state
909909
);
910-
expect(state2 && state2.index).toEqual(1);
911-
expect(state2 && state2.routes[1].routeName).toEqual('Bar');
912-
expect(state2 && state2.routes[1].params).toEqual({ name: 'Zoom' });
913-
expect(state2 && state2.routes.length).toEqual(2);
910+
expect(state2.index).toEqual(1);
911+
expect(state2.routes[1].routeName).toEqual('Bar');
912+
expect(state2.routes[1].params).toEqual({ name: 'Zoom' });
913+
expect(state2.routes.length).toEqual(2);
914914
const state3 = router.getStateForAction(
915915
{ type: NavigationActions.BACK, immediate: true },
916916
state2
@@ -1059,13 +1059,69 @@ describe('StackRouter', () => {
10591059
const state3 = router.getStateForAction(
10601060
{
10611061
type: StackActions.COMPLETE_TRANSITION,
1062+
toChildKey: state2.routes[1].key,
10621063
},
10631064
state2
10641065
);
10651066
expect(state3 && state3.index).toEqual(1);
10661067
expect(state3 && state3.isTransitioning).toEqual(false);
10671068
});
10681069

1070+
test('Completion action does not work with incorrect key', () => {
1071+
const FooScreen = () => <div />;
1072+
const router = StackRouter({
1073+
Foo: {
1074+
screen: FooScreen,
1075+
},
1076+
Bar: {
1077+
screen: FooScreen,
1078+
},
1079+
});
1080+
const state = {
1081+
key: 'StackKey',
1082+
index: 1,
1083+
isTransitioning: true,
1084+
routes: [{ key: 'a', routeName: 'Foo' }, { key: 'b', routeName: 'Foo' }],
1085+
};
1086+
const outputState = router.getStateForAction(
1087+
{
1088+
type: StackActions.COMPLETE_TRANSITION,
1089+
toChildKey: state.routes[state.index].key,
1090+
key: 'not StackKey',
1091+
},
1092+
state
1093+
);
1094+
expect(outputState.isTransitioning).toEqual(true);
1095+
});
1096+
1097+
test('Completion action does not work with incorrect toChildKey', () => {
1098+
const FooScreen = () => <div />;
1099+
const router = StackRouter({
1100+
Foo: {
1101+
screen: FooScreen,
1102+
},
1103+
Bar: {
1104+
screen: FooScreen,
1105+
},
1106+
});
1107+
const state = {
1108+
key: 'StackKey',
1109+
index: 1,
1110+
isTransitioning: true,
1111+
routes: [{ key: 'a', routeName: 'Foo' }, { key: 'b', routeName: 'Foo' }],
1112+
};
1113+
const outputState = router.getStateForAction(
1114+
{
1115+
type: StackActions.COMPLETE_TRANSITION,
1116+
// for this action to toggle isTransitioning, toChildKey should be state.routes[state.index].key,
1117+
toChildKey: 'incorrect',
1118+
key: 'StackKey',
1119+
},
1120+
state
1121+
);
1122+
expect(outputState.isTransitioning).toEqual(true);
1123+
});
1124+
10691125
test('Back action parent is prioritized over inactive child routers', () => {
10701126
const Bar = () => <div />;
10711127
Bar.router = StackRouter({
@@ -1842,14 +1898,15 @@ describe('StackRouter', () => {
18421898
},
18431899
state
18441900
);
1845-
expect(state2 && state2.index).toEqual(0);
1846-
expect(state2 && state2.isTransitioning).toEqual(false);
1847-
expect(state2 && state2.routes[0].index).toEqual(1);
1848-
expect(state2 && state2.routes[0].isTransitioning).toEqual(true);
1901+
expect(state2.index).toEqual(0);
1902+
expect(state2.isTransitioning).toEqual(false);
1903+
expect(state2.routes[0].index).toEqual(1);
1904+
expect(state2.routes[0].isTransitioning).toEqual(true);
18491905
expect(!!key).toEqual(true);
18501906
const state3 = router.getStateForAction(
18511907
{
18521908
type: StackActions.COMPLETE_TRANSITION,
1909+
toChildKey: state2.routes[0].routes[1].key,
18531910
},
18541911
state2
18551912
);

0 commit comments

Comments
 (0)