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

Pass transition hook's arguments correctly #4123

Merged
merged 1 commit into from
Nov 1, 2016
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
8 changes: 4 additions & 4 deletions modules/TransitionUtils.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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()
})
})
})