Skip to content

Add tests for incorrect onEnter/onLeave Redirect behavior #1356

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

Closed
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
43 changes: 43 additions & 0 deletions modules/__tests__/Redirect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,47 @@ describe('A <Redirect>', function () {
done();
});
});

it('calls onEnter but not onLeave', function (done) {
var enterCalled = 0;
var leaveCalled = 0;
var onEnter = function () { enterCalled++ };
var onLeave = function() { leaveCalled++ };
render((
<Router history={new MemoryHistory('/notes/5')}>
<Route onEnter={onEnter} onLeave={onLeave}>
<Route path="messages/:id"/>
<Redirect from="notes/:id" to="/messages/:id"/>
</Route>
</Router>
), node, function() {
expect(enterCalled).toEqual(1);
expect(leaveCalled).toEqual(0);
done();
});
});

it('doesn\'t call onEnter or onLeave when redirecting inside the same parent', function (done) {
var enterCalled = 0;
var leaveCalled = 0;
var onEnter = function () { enterCalled++ };
var onLeave = function() { leaveCalled++ };
render((
<Router history={new MemoryHistory('/messages/5/details')}>
<Route onEnter={onEnter} onLeave={onLeave}>
<Redirect from="messages/:id" to="/messages/:id/details"/>
<Route path="messages/:id/details"/>
</Route>
</Router>
), node, function() {
expect(this.state.location.pathname).toEqual('/messages/5/details');
expect(enterCalled).toEqual(1);
expect(leaveCalled).toEqual(0);
this.transitionTo("/messages/6");
expect(this.state.location.pathname).toEqual('/messages/6/details');
expect(enterCalled).toEqual(1);
expect(leaveCalled).toEqual(0);
done();
});
});
});