Skip to content
Merged
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
16 changes: 16 additions & 0 deletions modules/__tests__/transitionHooks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import expect, { spyOn } from 'expect'
import React from 'react'
import createHistory from 'history/lib/createMemoryHistory'
import useQueries from 'history/lib/useQueries'
import execSteps from './execSteps'
import Router from '../Router'

Expand Down Expand Up @@ -193,4 +194,19 @@ describe('When a router enters a branch', function () {
})
})

describe('and then the query changes', function () {
it('calls the onEnter hooks of all routes in that branch', function (done) {
const newsFeedRouteEnterSpy = spyOn(NewsFeedRoute, 'onEnter').andCallThrough()
const history = useQueries(createHistory)('/inbox')

React.render(<Router history={history} routes={routes}/>, node, function () {
history.pushState(null, '/news', { q: 1 })
expect(newsFeedRouteEnterSpy.calls.length).toEqual(1)
history.pushState(null, '/news', { q: 2 })
expect(newsFeedRouteEnterSpy.calls.length).toEqual(2)
done()
})
})
})

})
12 changes: 10 additions & 2 deletions modules/computeChangedRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ function routeParamsChanged(route, prevState, nextState) {
})
}

function routeQueryChanged(prevState, nextState) {
return prevState.location.search !== nextState.location.search
}

/**
* Returns an object of { leaveRoutes, enterRoutes } determined by
* the change from prevState to nextState. We leave routes if either
* 1) they are not in the next state or 2) they are in the next state
* but their params have changed (i.e. /users/123 => /users/456).
* but their params have changed (i.e. /users/123 => /users/456) or
* 3) they are in the next state but the query has changed
* (i.e. /search?query=foo => /search?query=bar)
*
* leaveRoutes are ordered starting at the leaf route of the tree
* we're leaving up to the common parent route. enterRoutes are ordered
Expand All @@ -28,7 +34,9 @@ function computeChangedRoutes(prevState, nextState) {
let leaveRoutes, enterRoutes
if (prevRoutes) {
leaveRoutes = prevRoutes.filter(function (route) {
return nextRoutes.indexOf(route) === -1 || routeParamsChanged(route, prevState, nextState)
return nextRoutes.indexOf(route) === -1
|| routeParamsChanged(route, prevState, nextState)
|| routeQueryChanged(prevState, nextState)
})

// onLeave hooks start at the leaf route.
Expand Down