Skip to content

Commit

Permalink
fix: Pass transition hook's arguments correctly (#4123)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkist authored and timdorr committed Nov 1, 2016
1 parent 32728be commit 70a4272
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
8 changes: 4 additions & 4 deletions modules/TransitionUtils.js
Expand Up @@ -82,9 +82,9 @@ export function runEnterHooks(routes, nextState, callback) {
enterHooks.clear()
const hooks = getEnterHooks(routes)
return runTransitionHooks(hooks.length, (index, replace, next) => {
const wrappedNext = () => {
const wrappedNext = (...args) => {
if (enterHooks.has(hooks[index])) {
next()
next(...args)
enterHooks.remove(hooks[index])
}
}
Expand All @@ -106,9 +106,9 @@ export function runChangeHooks(routes, state, nextState, callback) {
changeHooks.clear()
const hooks = getChangeHooks(routes)
return runTransitionHooks(hooks.length, (index, replace, next) => {
const wrappedNext = () => {
const wrappedNext = (...args) => {
if (changeHooks.has(hooks[index])) {
next()
next(...args)
changeHooks.remove(hooks[index])
}
}
Expand Down
19 changes: 18 additions & 1 deletion modules/__tests__/transitionHooks-test.js
Expand Up @@ -6,6 +6,7 @@ import { routerShape } from '../PropTypes'
import execSteps from './execSteps'
import Router from '../Router'
import Route from '../Route'
import match from '../match'

describe('When a router enters a branch', function () {
let
Expand Down Expand Up @@ -397,13 +398,17 @@ describe('Changing location', () => {
cb()
})
}
const onEnterError = (state, replace, cb) => {
cb(new Error('transition error'))
}
const createRoutes = ({ enter, change }) => [
<Route path="/" onChange={change ? onChange : noop} component={Text('Home')}>
<Route path="child1" component={Text('Child1')} />
<Route path="child2" component={Text('Child2')} />
</Route>,
<Route path="/foo" onEnter={enter ? onEnter : noop} component={Text('Foo')} />,
<Route path="/bar" component={Text('Bar')} />
<Route path="/bar" component={Text('Bar')} />,
<Route path="/error" onEnter={enter ? onEnterError : noop} component={Text('Error')}/>
]

beforeEach(() => {
Expand Down Expand Up @@ -443,4 +448,16 @@ describe('Changing location', () => {
})
})
})

it('should pass error correctly', (done) => {
const routes = createRoutes({ enter: true })

match({ routes, location: '/error' }, (error, redirectLocation, renderProps) => {
expect(error).toExist()
expect(error.message).toEqual('transition error')
expect(redirectLocation).toNotExist()
expect(renderProps).toNotExist()
done()
})
})
})

0 comments on commit 70a4272

Please sign in to comment.