From 23f0cb872e4a580e7260b76ff5bbc237eb0888f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20LE=20BRAS?= Date: Sat, 17 Nov 2018 16:07:16 +0100 Subject: [PATCH] fix(react-router-navigation): correct navigation state initialization --- .../src/RouteUtils.js | 2 +- .../src/StackUtils.js | 20 ++++++++-- .../src/StateUtils.js | 4 +- .../src/TabStack.js | 3 ++ .../src/__tests__/StackUtils.spec.js | 40 +++++++++++++++++++ .../src/__tests__/TabStack.spec.js | 2 +- 6 files changed, 65 insertions(+), 6 deletions(-) diff --git a/packages/react-router-navigation-core/src/RouteUtils.js b/packages/react-router-navigation-core/src/RouteUtils.js index 0734043..58db30a 100644 --- a/packages/react-router-navigation-core/src/RouteUtils.js +++ b/packages/react-router-navigation-core/src/RouteUtils.js @@ -7,7 +7,7 @@ let uniqueBaseId = `id-${Date.now()}` let uuidCount = 0 export default { - create(item: RouteProps, location?: ?Location, staleRoute?: Route): ?Route { + create(item: RouteProps, location?: ?Location, staleRoute?: ?Route): ?Route { if (!item || !item.path) return null const routeName = item.path const routeMatch = location ? matchPath(location.pathname, item) : null diff --git a/packages/react-router-navigation-core/src/StackUtils.js b/packages/react-router-navigation-core/src/StackUtils.js index 937ccf3..292de6f 100644 --- a/packages/react-router-navigation-core/src/StackUtils.js +++ b/packages/react-router-navigation-core/src/StackUtils.js @@ -25,17 +25,31 @@ export default { stack: RouteProps[], entries: Location[], location: Location, + historyIndex?: number, ): Location[] { const startHistoryIndex = entries.reduce((acc, entry, index) => { if (stack.find(item => matchPath(entry.pathname, item))) { - if (acc === -1) return index + if (acc === -1) { + return index + } + return acc + } + if (typeof historyIndex === 'number' && index > historyIndex) { return acc } return -1 }, -1) const lastHistoryIndex = entries.reduce((acc, entry, index) => { - if (index < startHistoryIndex) return -1 - if (location.pathname === entry.pathname) return index + if ( + index < startHistoryIndex && + typeof historyIndex === 'number' && + index <= historyIndex + ) { + return -1 + } + if (location.pathname === entry.pathname) { + return index + } return acc }, -1) return entries.slice(startHistoryIndex, lastHistoryIndex + 1) diff --git a/packages/react-router-navigation-core/src/StateUtils.js b/packages/react-router-navigation-core/src/StateUtils.js index 93fb31c..cbba721 100644 --- a/packages/react-router-navigation-core/src/StateUtils.js +++ b/packages/react-router-navigation-core/src/StateUtils.js @@ -12,12 +12,14 @@ export default { location: Location, entries: Location[], buildFrom: 'history' | 'nodes', - staleNavigationState?: NavigationState<>, + staleNavigationState?: ?NavigationState<>, + index?: number, ): NavigationState<> { const historyEntries = StackUtils.getHistoryEntries( nodes, entries, location, + index, ) const staleRoutes = staleNavigationState && staleNavigationState.routes if (buildFrom === 'nodes') { diff --git a/packages/react-router-navigation-core/src/TabStack.js b/packages/react-router-navigation-core/src/TabStack.js index 633a709..d6d5376 100755 --- a/packages/react-router-navigation-core/src/TabStack.js +++ b/packages/react-router-navigation-core/src/TabStack.js @@ -57,6 +57,8 @@ export default class TabStack extends React.Component { location, entries, 'nodes', + undefined, + index, ) invariant( navigationState.index !== -1, @@ -91,6 +93,7 @@ export default class TabStack extends React.Component { entries, 'nodes', navigationState, + history.index, ) invariant( nextNavigationState.index !== -1, diff --git a/packages/react-router-navigation-core/src/__tests__/StackUtils.spec.js b/packages/react-router-navigation-core/src/__tests__/StackUtils.spec.js index 1aa59ba..a6e78d4 100644 --- a/packages/react-router-navigation-core/src/__tests__/StackUtils.spec.js +++ b/packages/react-router-navigation-core/src/__tests__/StackUtils.spec.js @@ -155,5 +155,45 @@ describe('StackUtils', () => { { pathname: '/a' }, ]) }) + + it('should return history entries with history index (1)', () => { + const stack = [{ path: '/a' }, { path: '/b' }] + const location = { pathname: '/a' } + const entries = [ + { pathname: '/c' }, + { pathname: '/a' }, + { pathname: '/d' }, + { pathname: '/a' }, + { pathname: '/b' }, + { pathname: '/a' }, + { pathname: '/c' }, + ] + const index = 5 + expect( + StackUtils.getHistoryEntries(stack, entries, location, index), + ).toMatchObject([ + { pathname: '/a' }, + { pathname: '/b' }, + { pathname: '/a' }, + ]) + }) + + it('should return history entries with history index (2)', () => { + const stack = [{ path: '/a' }, { path: '/b' }] + const location = { pathname: '/a' } + const entries = [ + { pathname: '/c' }, + { pathname: '/a' }, + { pathname: '/d' }, + { pathname: '/a' }, + { pathname: '/b' }, + { pathname: '/a' }, + { pathname: '/c' }, + ] + const index = 6 + expect( + StackUtils.getHistoryEntries(stack, entries, location, index), + ).toMatchObject([]) + }) }) }) diff --git a/packages/react-router-navigation-core/src/__tests__/TabStack.spec.js b/packages/react-router-navigation-core/src/__tests__/TabStack.spec.js index 61d3292..88fc74f 100644 --- a/packages/react-router-navigation-core/src/__tests__/TabStack.spec.js +++ b/packages/react-router-navigation-core/src/__tests__/TabStack.spec.js @@ -79,7 +79,7 @@ describe('', () => { it('should render correctly with initialIndex and initialEntries props ', () => { const history = createHistory({ initialIndex: 3, - initialEntries: ['/', '/yolo', '/goodbye', '/hello', '/'], + initialEntries: ['/', '/yolo', '/goodbye', '/hello', '/aa'], }) const TabViewComponent = jest.fn(props => { return renderTabView(props)