Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable walking up route hierarchy to trigger onEnter #3197

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions modules/__tests__/transitionHooks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,33 @@ describe('When a router enters a branch', function () {
})
})

describe('and then enters a parent route', function () {
it('calls the onEnter hooks of the parent', function (done) {
const parentEnterSpy = spyOn(UserRoute, 'onEnter').andCallThrough()
const childEnterSpy = spyOn(AssignmentRoute, 'onEnter').andCallThrough()
const childLeaveSpy = spyOn(AssignmentRoute, 'onLeave').andCallThrough()
const history = createHistory('/users/123/assignments/456')

const steps = [
function () {
expect(parentEnterSpy).toHaveBeenCalled()
expect(childEnterSpy).toHaveBeenCalled()
history.push('/users/123')
},
function () {
expect(childLeaveSpy).toHaveBeenCalled()
expect(parentEnterSpy.calls.length).toEqual(2)
}
]

const execNextStep = execSteps(steps, done)

render(
<Router history={history}
routes={routes}
onUpdate={execNextStep}
/>, node, execNextStep)

})
})
})
7 changes: 6 additions & 1 deletion modules/computeChangedRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ function computeChangedRoutes(prevState, nextState) {
// onLeave hooks start at the leaf route.
leaveRoutes.reverse()

const lastRoute = (routes) => (
routes[routes.length - 1]
)

enterRoutes = nextRoutes.filter(function (route) {
return prevRoutes.indexOf(route) === -1 || leaveRoutes.indexOf(route) !== -1
const isParentRoute = (nextRoutes.indexOf(route) === (nextRoutes.length - 1)) && (lastRoute(prevRoutes) !== lastRoute(nextRoutes))
return prevRoutes.indexOf(route) === -1 || leaveRoutes.indexOf(route) !== -1 || isParentRoute
})
} else {
leaveRoutes = []
Expand Down