From 0c31bc44eaed1e45baa59eaefcad8ba2cee06e22 Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Mon, 12 Mar 2018 15:06:38 -0700 Subject: [PATCH] Make push, pop, and popToTop bubble like navigate (#3617) --- src/routers/StackRouter.js | 22 ++----- src/routers/__tests__/StackRouter-test.js | 72 +++++++++++------------ 2 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/routers/StackRouter.js b/src/routers/StackRouter.js index 9d86ca53..56bcf320 100644 --- a/src/routers/StackRouter.js +++ b/src/routers/StackRouter.js @@ -259,12 +259,8 @@ export default (routeConfigs, stackConfig = {}) => { action.type === NavigationActions.PUSH && childRouters[action.routeName] === undefined ) { - // If we've made it this far with a push action, we return the - // state with a new identity to prevent the action from bubbling - // back up. - return { - ...state, - }; + // Return the state identity to bubble the action up + return state; } // Handle navigation to other child routers that are not yet pushed @@ -313,11 +309,7 @@ export default (routeConfigs, stackConfig = {}) => { // If we're already at the top, then we return the state with a new // identity so that the action is handled by this router. - if (state.index === 0) { - return { - ...state, - }; - } else { + if (state.index > 0) { return { ...state, isTransitioning: action.immediate !== true, @@ -442,15 +434,9 @@ export default (routeConfigs, stackConfig = {}) => { index: backRouteIndex - 1, isTransitioning: immediate !== true, }; - } else if ( - backRouteIndex === 0 && - action.type === NavigationActions.POP - ) { - return { - ...state, - }; } } + return state; }, diff --git a/src/routers/__tests__/StackRouter-test.js b/src/routers/__tests__/StackRouter-test.js index 9fcc675f..245ab55d 100644 --- a/src/routers/__tests__/StackRouter-test.js +++ b/src/routers/__tests__/StackRouter-test.js @@ -366,38 +366,7 @@ describe('StackRouter', () => { expect(pushedState.routes[1].routes[1].routeName).toEqual('qux'); }); - test('pop does not bubble up', () => { - const ChildNavigator = () =>
; - ChildNavigator.router = StackRouter({ - Baz: { screen: () =>
}, - Qux: { screen: () =>
}, - }); - const router = StackRouter({ - Foo: { screen: () =>
}, - Bar: { screen: ChildNavigator }, - }); - - const state = router.getStateForAction({ type: NavigationActions.INIT }); - const state2 = router.getStateForAction( - { - type: NavigationActions.NAVIGATE, - routeName: 'Bar', - key: 'StackRouterRoot', - }, - state - ); - const barKey = state2.routes[1].routes[0].key; - const state3 = router.getStateForAction( - { - type: NavigationActions.POP, - }, - state2 - ); - expect(state3 && state3.index).toEqual(1); - expect(state3 && state3.routes[1].index).toEqual(0); - }); - - test('push does not bubble up', () => { + test('push bubbles up', () => { const ChildNavigator = () =>
; ChildNavigator.router = StackRouter({ Baz: { screen: () =>
}, @@ -424,11 +393,41 @@ describe('StackRouter', () => { }, state2 ); - expect(state3 && state3.index).toEqual(1); - expect(state3 && state3.routes.length).toEqual(2); + expect(state3 && state3.index).toEqual(2); + expect(state3 && state3.routes.length).toEqual(3); }); - test('popToTop does not bubble up', () => { + test('pop bubbles up', () => { + const ChildNavigator = () =>
; + ChildNavigator.router = StackRouter({ + Baz: { screen: () =>
}, + Qux: { screen: () =>
}, + }); + const router = StackRouter({ + Foo: { screen: () =>
}, + Bar: { screen: ChildNavigator }, + }); + + const state = router.getStateForAction({ type: NavigationActions.INIT }); + const state2 = router.getStateForAction( + { + type: NavigationActions.NAVIGATE, + routeName: 'Bar', + key: 'StackRouterRoot', + }, + state + ); + const barKey = state2.routes[1].routes[0].key; + const state3 = router.getStateForAction( + { + type: NavigationActions.POP, + }, + state2 + ); + expect(state3 && state3.index).toEqual(0); + }); + + test('popToTop bubbles up', () => { const ChildNavigator = () =>
; ChildNavigator.router = StackRouter({ Baz: { screen: () =>
}, @@ -453,8 +452,7 @@ describe('StackRouter', () => { }, state2 ); - expect(state3 && state3.index).toEqual(1); - expect(state3 && state3.routes[1].index).toEqual(0); + expect(state3 && state3.index).toEqual(0); }); test('popToTop targets StackRouter by key if specified', () => {