Skip to content

Commit d16d658

Browse files
committed
Add tests for async transitions
1 parent 91d4380 commit d16d658

File tree

2 files changed

+173
-4
lines changed

2 files changed

+173
-4
lines changed

modules/__tests__/Router-test.js

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,60 @@ var {
1212
Bar,
1313
EchoFooProp,
1414
Foo,
15+
Async,
1516
Nested,
1617
EchoBarParam,
17-
RedirectToFoo
18+
RedirectToFoo,
19+
RedirectToFooAsync,
20+
Abort,
21+
AbortAsync
1822
} = require('./TestHandlers');
1923

2024
describe('Router', function () {
2125
describe('transitions', function () {
2226

2327
var routes = [
2428
<Route path="/redirect" handler={RedirectToFoo}/>,
25-
<Route path="/foo" handler={Foo}/>
29+
<Route path="/redirect-async" handler={RedirectToFooAsync}/>,
30+
<Route path="/abort" handler={Abort}/>,
31+
<Route path="/abort-async" handler={AbortAsync}/>,
32+
<Route path="/foo" handler={Foo}/>,
33+
<Route path="/bar" handler={Bar}/>,
34+
<Route path="/async" handler={Async}/>
2635
];
2736

37+
describe('transition.wait', function () {
38+
it('waits asynchronously in willTransitionTo', function (done) {
39+
TestLocation.history = [ '/bar' ];
40+
41+
var div = document.createElement('div');
42+
var steps = [];
43+
44+
steps.push(function () {
45+
expect(div.innerHTML).toMatch(/Bar/);
46+
TestLocation.push('/async');
47+
expect(div.innerHTML).toMatch(/Bar/);
48+
49+
setTimeout(function () {
50+
expect(div.innerHTML).toMatch(/Async/);
51+
done();
52+
}, Async.delay + 10);
53+
});
54+
55+
steps.push(function () {
56+
expect(div.innerHTML).toMatch(/Async/);
57+
});
58+
59+
Router.run(routes, TestLocation, function (Handler) {
60+
React.render(<Handler/>, div, function () {
61+
steps.shift()();
62+
});
63+
});
64+
});
65+
});
66+
2867
describe('transition.redirect', function () {
29-
it('redirects in willTransitionTo', function (done) {
68+
it('redirects synchronously in willTransitionTo', function (done) {
3069
TestLocation.history = [ '/redirect' ];
3170

3271
var div = document.createElement('div');
@@ -38,10 +77,80 @@ describe('Router', function () {
3877
});
3978
});
4079
});
80+
81+
it('redirects asynchronously in willTransitionTo', function (done) {
82+
TestLocation.history = [ '/bar' ];
83+
84+
var div = document.createElement('div');
85+
var steps = [];
86+
87+
steps.push(function () {
88+
expect(div.innerHTML).toMatch(/Bar/);
89+
TestLocation.push('/redirect-async');
90+
expect(div.innerHTML).toMatch(/Bar/);
91+
92+
setTimeout(function () {
93+
expect(div.innerHTML).toMatch(/Foo/);
94+
done();
95+
}, RedirectToFooAsync.delay + 10);
96+
});
97+
98+
steps.push(function () {
99+
expect(div.innerHTML).toMatch(/Foo/);
100+
done();
101+
});
102+
103+
Router.run(routes, TestLocation, function (Handler) {
104+
React.render(<Handler/>, div, function () {
105+
steps.shift()();
106+
});
107+
});
108+
});
41109
});
42110

43111
describe('transition.abort', function () {
44-
it('aborts in willTransitionTo');
112+
it('aborts synchronously in willTransitionTo', function (done) {
113+
TestLocation.history = [ '/foo' ];
114+
115+
var div = document.createElement('div');
116+
117+
Router.run(routes, TestLocation, function (Handler) {
118+
React.render(<Handler/>, div, function () {
119+
TestLocation.push('/abort');
120+
expect(div.innerHTML).toMatch(/Foo/);
121+
expect(TestLocation.getCurrentPath() === 'foo');
122+
done();
123+
});
124+
});
125+
});
126+
127+
it('aborts asynchronously in willTransitionTo', function (done) {
128+
TestLocation.history = [ '/bar' ];
129+
130+
var div = document.createElement('div');
131+
var steps = [];
132+
133+
steps.push(function () {
134+
expect(div.innerHTML).toMatch(/Bar/);
135+
TestLocation.push('/abort-async');
136+
expect(div.innerHTML).toMatch(/Bar/);
137+
138+
setTimeout(function () {
139+
expect(div.innerHTML).toMatch(/Bar/);
140+
done();
141+
}, AbortAsync.delay + 10);
142+
});
143+
144+
steps.push(function () {
145+
expect(div.innerHTML).toMatch(/Bar/);
146+
});
147+
148+
Router.run(routes, TestLocation, function (Handler) {
149+
React.render(<Handler/>, div, function () {
150+
steps.shift()();
151+
});
152+
});
153+
});
45154
});
46155
});
47156

modules/__tests__/TestHandlers.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var React = require('react');
22
var RouteHandler = require('../components/RouteHandler');
33
var State = require('../mixins/State');
4+
var delay = require('when/delay');
45

56
exports.Nested = React.createClass({
67
render: function () {
@@ -19,6 +20,20 @@ exports.Foo = React.createClass({
1920
}
2021
});
2122

23+
exports.Async = React.createClass({
24+
statics: {
25+
delay: 10,
26+
27+
willTransitionTo: function (transition) {
28+
transition.wait(delay(this.delay));
29+
}
30+
},
31+
32+
render: function () {
33+
return <div className="Async">Async</div>
34+
}
35+
});
36+
2237
exports.Bar = React.createClass({
2338
render: function () {
2439
return <div className="Bar">Bar</div>
@@ -37,6 +52,51 @@ exports.RedirectToFoo = React.createClass({
3752
}
3853
});
3954

55+
exports.RedirectToFooAsync = React.createClass({
56+
statics: {
57+
delay: 10,
58+
59+
willTransitionTo: function (transition) {
60+
transition.wait(delay(this.delay).then(function () {
61+
transition.redirect('/foo');
62+
}));
63+
}
64+
},
65+
66+
render: function () {
67+
return null;
68+
}
69+
});
70+
71+
72+
exports.Abort = React.createClass({
73+
statics: {
74+
willTransitionTo: function (transition) {
75+
transition.abort();
76+
}
77+
},
78+
79+
render: function () {
80+
return null;
81+
}
82+
});
83+
84+
exports.AbortAsync = React.createClass({
85+
statics: {
86+
delay: 10,
87+
88+
willTransitionTo: function (transition) {
89+
transition.wait(delay(this.delay).then(function () {
90+
transition.abort();
91+
}));
92+
}
93+
},
94+
95+
render: function () {
96+
return null;
97+
}
98+
});
99+
40100
exports.EchoFooProp = React.createClass({
41101
render: function () {
42102
return <div>{this.props.foo}</div>;

0 commit comments

Comments
 (0)