Skip to content

Commit cabc759

Browse files
committed
Clean up test harness
1 parent 6878120 commit cabc759

File tree

7 files changed

+94
-169
lines changed

7 files changed

+94
-169
lines changed

modules/components/Routes.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function defaultTransitionErrorHandler(error) {
6767
* See the <Route> component for more details.
6868
*/
6969
var Routes = React.createClass({
70+
7071
displayName: 'Routes',
7172

7273
propTypes: {
@@ -184,11 +185,18 @@ var Routes = React.createClass({
184185
var transition = new Transition(path);
185186
var routes = this;
186187

187-
var promise = syncWithTransition(routes, transition).then(function (newState) {
188+
var promise = runTransitionHooks(routes, transition).then(function (nextState) {
188189
if (transition.isAborted) {
189190
routes.props.onAbortedTransition(transition);
190-
} else if (newState) {
191-
routes.props.onActiveStateChange(newState);
191+
} else if (nextState) {
192+
routes.setState(nextState);
193+
routes.props.onActiveStateChange(nextState);
194+
195+
// TODO: add functional test
196+
var rootMatch = getRootMatch(nextState.matches);
197+
198+
if (rootMatch)
199+
maybeScrollWindow(routes, rootMatch.route);
192200
}
193201

194202
return transition;
@@ -306,7 +314,7 @@ function updateMatchComponents(matches, refs) {
306314
* if they all pass successfully. Returns a promise that resolves to the new
307315
* state if it needs to be updated, or undefined if not.
308316
*/
309-
function syncWithTransition(routes, transition) {
317+
function runTransitionHooks(routes, transition) {
310318
if (routes.state.path === transition.path)
311319
return Promise.resolve(); // Nothing to do!
312320

@@ -338,18 +346,19 @@ function syncWithTransition(routes, transition) {
338346
toMatches = nextMatches;
339347
}
340348

341-
return checkTransitionFromHooks(fromMatches, transition).then(function () {
349+
return runTransitionFromHooks(fromMatches, transition).then(function () {
342350
if (transition.isAborted)
343351
return; // No need to continue.
344352

345-
return checkTransitionToHooks(toMatches, transition).then(function () {
353+
return runTransitionToHooks(toMatches, transition).then(function () {
346354
if (transition.isAborted)
347355
return; // No need to continue.
348356

349357
var rootMatch = getRootMatch(nextMatches);
350358
var params = (rootMatch && rootMatch.params) || {};
351359
var query = Path.extractQuery(transition.path) || {};
352-
var state = {
360+
361+
return {
353362
path: transition.path,
354363
matches: nextMatches,
355364
activeParams: params,
@@ -358,12 +367,6 @@ function syncWithTransition(routes, transition) {
358367
return match.route;
359368
})
360369
};
361-
362-
// TODO: add functional test
363-
maybeScrollWindow(routes, toMatches[toMatches.length - 1]);
364-
routes.setState(state);
365-
366-
return state;
367370
});
368371
});
369372
}
@@ -374,7 +377,7 @@ function syncWithTransition(routes, transition) {
374377
* the route's handler, so that the deepest nested handlers are called first.
375378
* Returns a promise that resolves after the last handler.
376379
*/
377-
function checkTransitionFromHooks(matches, transition) {
380+
function runTransitionFromHooks(matches, transition) {
378381
var promise = Promise.resolve();
379382

380383
reversedArray(matches).forEach(function (match) {
@@ -394,7 +397,7 @@ function checkTransitionFromHooks(matches, transition) {
394397
* with the transition object and any params that apply to that handler. Returns
395398
* a promise that resolves after the last handler.
396399
*/
397-
function checkTransitionToHooks(matches, transition) {
400+
function runTransitionToHooks(matches, transition) {
398401
var promise = Promise.resolve();
399402

400403
matches.forEach(function (match) {
@@ -458,11 +461,8 @@ function reversedArray(array) {
458461
return array.slice(0).reverse();
459462
}
460463

461-
function maybeScrollWindow(routes, match) {
462-
if (routes.props.preserveScrollPosition)
463-
return;
464-
465-
if (!match || match.route.props.preserveScrollPosition)
464+
function maybeScrollWindow(routes, rootRoute) {
465+
if (routes.props.preserveScrollPosition || rootRoute.props.preserveScrollPosition)
466466
return;
467467

468468
window.scrollTo(0, 0);

specs/ActiveStore.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require('./helper');
2-
var ActiveStore = require('../modules/stores/ActiveStore');
32
var Route = require('../modules/components/Route');
3+
var ActiveStore = require('../modules/stores/ActiveStore');
44

55
var App = React.createClass({
66
displayName: 'App',

specs/AsyncState.spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var AsyncState = require('../modules/mixins/AsyncState');
44

55
describe('AsyncState', function () {
66

7-
87
describe('a component that fetches part of its state asynchronously', function () {
98
it('resolves all state variables correctly', function (done) {
109
var User = React.createClass({

specs/PathStore.spec.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ var PathStore = require('../modules/stores/PathStore');
55
describe('PathStore', function () {
66

77
beforeEach(function () {
8-
PathStore.setup(MemoryLocation);
98
PathStore.push('/one');
109
});
1110

12-
afterEach(function () {
13-
PathStore.teardown();
14-
});
15-
1611
describe('when a new path is pushed to the URL', function () {
1712
beforeEach(function () {
1813
PathStore.push('/two');
@@ -53,6 +48,7 @@ describe('PathStore', function () {
5348
it('has the correct path', function () {
5449
expect(PathStore.getCurrentPath()).toEqual('/one');
5550
});
51+
5652
});
5753

5854
});

specs/Route.spec.js

+43-98
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require('./helper');
22
var Route = require('../modules/components/Route');
33
var Routes = require('../modules/components/Routes');
4-
var URLStore = require('../modules/stores/URLStore');
54

65
var App = React.createClass({
76
displayName: 'App',
@@ -10,16 +9,11 @@ var App = React.createClass({
109
}
1110
});
1211

13-
describe('Route', function() {
12+
describe('a Route', function () {
1413

15-
afterEach(function() {
16-
URLStore.teardown();
17-
window.location.hash = '';
18-
});
19-
20-
describe('a Route that matches a URL', function () {
14+
describe('that matches the URL', function () {
2115
it('returns an array', function () {
22-
var routes = renderComponent(
16+
var routes = ReactTestUtils.renderIntoDocument(
2317
Routes(null,
2418
Route({ handler: App },
2519
Route({ path: '/a/b/c', handler: App })
@@ -33,14 +27,11 @@ describe('Route', function() {
3327

3428
var rootMatch = getRootMatch(matches);
3529
expect(rootMatch.params).toEqual({});
36-
37-
// this causes tests to fail, no clue why ...
38-
//removeComponent(routes);
3930
});
4031

4132
describe('that contains dynamic segments', function () {
4233
it('returns an array with the correct params', function () {
43-
var routes = renderComponent(
34+
var routes = ReactTestUtils.renderIntoDocument(
4435
Routes(null,
4536
Route({ handler: App },
4637
Route({ path: '/posts/:id/edit', handler: App })
@@ -54,15 +45,13 @@ describe('Route', function() {
5445

5546
var rootMatch = getRootMatch(matches);
5647
expect(rootMatch.params).toEqual({ id: 'abc' });
57-
58-
//removeComponent(routes);
5948
});
6049
});
6150
});
6251

63-
describe('a Route that does not match the URL', function () {
52+
describe('that does not match the URL', function () {
6453
it('returns null', function () {
65-
var routes = renderComponent(
54+
var routes = ReactTestUtils.renderIntoDocument(
6655
Routes(null,
6756
Route({ handler: App },
6857
Route({ path: '/a/b/c', handler: App })
@@ -71,103 +60,59 @@ describe('Route', function() {
7160
);
7261

7362
expect(routes.match('/not-found')).toBe(null);
74-
75-
//removeComponent(routes);
7663
});
7764
});
7865

79-
describe('a nested Route that matches the URL', function () {
80-
it('returns the appropriate params for each match', function () {
81-
var routes = renderComponent(
82-
Routes(null,
83-
Route({ handler: App },
84-
Route({ name: 'posts', path: '/posts/:id', handler: App },
85-
Route({ name: 'comment', path: '/posts/:id/comments/:commentId', handler: App })
86-
)
66+
});
67+
68+
describe('a nested Route that matches the URL', function () {
69+
it('returns the appropriate params for each match', function () {
70+
var routes = ReactTestUtils.renderIntoDocument(
71+
Routes(null,
72+
Route({ handler: App },
73+
Route({ name: 'posts', path: '/posts/:id', handler: App },
74+
Route({ name: 'comment', path: '/posts/:id/comments/:commentId', handler: App })
8775
)
8876
)
89-
);
77+
)
78+
);
9079

91-
var matches = routes.match('/posts/abc/comments/123');
92-
assert(matches);
93-
expect(matches.length).toEqual(3);
80+
var matches = routes.match('/posts/abc/comments/123');
81+
assert(matches);
82+
expect(matches.length).toEqual(3);
9483

95-
var rootMatch = getRootMatch(matches);
96-
expect(rootMatch.route.props.name).toEqual('comment');
97-
expect(rootMatch.params).toEqual({ id: 'abc', commentId: '123' });
84+
var rootMatch = getRootMatch(matches);
85+
expect(rootMatch.route.props.name).toEqual('comment');
86+
expect(rootMatch.params).toEqual({ id: 'abc', commentId: '123' });
9887

99-
var postsMatch = matches[1];
100-
expect(postsMatch.route.props.name).toEqual('posts');
101-
expect(postsMatch.params).toEqual({ id: 'abc' });
102-
103-
//removeComponent(routes);
104-
});
88+
var postsMatch = matches[1];
89+
expect(postsMatch.route.props.name).toEqual('posts');
90+
expect(postsMatch.params).toEqual({ id: 'abc' });
10591
});
92+
});
10693

107-
describe('multiple nested Router that match the URL', function () {
108-
it('returns the first one in the subtree, depth-first', function () {
109-
var routes = renderComponent(
110-
Routes(null,
111-
Route({ handler: App },
112-
Route({ path: '/a', handler: App },
113-
Route({ path: '/a/b', name: 'expected', handler: App })
114-
),
115-
Route({ path: '/a/b', handler: App })
116-
)
94+
describe('multiple nested Routes that match the URL', function () {
95+
it('returns the first one in the subtree, depth-first', function () {
96+
var routes = ReactTestUtils.renderIntoDocument(
97+
Routes(null,
98+
Route({ handler: App },
99+
Route({ path: '/a', handler: App },
100+
Route({ path: '/a/b', name: 'expected', handler: App })
101+
),
102+
Route({ path: '/a/b', handler: App })
117103
)
118-
);
119-
120-
var matches = routes.match('/a/b');
121-
assert(matches);
122-
expect(matches.length).toEqual(3);
104+
)
105+
);
123106

124-
var rootMatch = getRootMatch(matches);
125-
expect(rootMatch.route.props.name).toEqual('expected');
107+
var matches = routes.match('/a/b');
108+
assert(matches);
109+
expect(matches.length).toEqual(3);
126110

127-
//removeComponent(routes);
128-
});
111+
var rootMatch = getRootMatch(matches);
112+
expect(rootMatch.route.props.name).toEqual('expected');
129113
});
130114
});
131115

132-
// describe('a Router that renders on the server', function() {
133-
// it('works with async willTransitionTo()', function(done) {
134-
// var dataStore = 'goodbye';
135-
// var Layout = React.createClass({
136-
// render: function() {
137-
// return React.DOM.article(null, this.props.activeRouteHandler());
138-
// }
139-
// });
140-
// var AsyncApp = React.createClass({
141-
// displayName: 'AsyncApp',
142-
// statics: {
143-
// willTransitionTo: function() {
144-
// return new Promise(function(resolve) {
145-
// setTimeout(function() {
146-
// dataStore = 'hello';
147-
// resolve();
148-
// }, 0);
149-
// });
150-
// }
151-
// },
152-
// render: function() {
153-
// return React.DOM.div(null, dataStore);
154-
// }
155-
// });
156-
157-
// var router = Router(
158-
// RouteComponent({ handler: Layout},
159-
// RouteComponent({ path: '/a', handler: AsyncApp }))
160-
// );
161-
162-
// router.renderComponentToString('/a').then(function(result) {
163-
// expect(result.indexOf('div') > -1).toBe(true);
164-
// expect(result.indexOf('hello') > -1).toBe(true);
165-
166-
// done();
167-
// });
168-
// });
169-
// });
170-
171116
function getRootMatch(matches) {
172117
return matches[matches.length - 1];
173118
}

0 commit comments

Comments
 (0)