Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
fix(react-router-navigation): correct navigation state initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoLeBras committed Nov 17, 2018
1 parent e99edfa commit 23f0cb8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/react-router-navigation-core/src/RouteUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions packages/react-router-navigation-core/src/StackUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion packages/react-router-navigation-core/src/StateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-router-navigation-core/src/TabStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export default class TabStack extends React.Component<Props, State> {
location,
entries,
'nodes',
undefined,
index,
)
invariant(
navigationState.index !== -1,
Expand Down Expand Up @@ -91,6 +93,7 @@ export default class TabStack extends React.Component<Props, State> {
entries,
'nodes',
navigationState,
history.index,
)
invariant(
nextNavigationState.index !== -1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('<TabStack />', () => {
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)
Expand Down

0 comments on commit 23f0cb8

Please sign in to comment.