Skip to content

Commit b5b7bdf

Browse files
committed
Clean up tests
1 parent 3e3ef10 commit b5b7bdf

File tree

6 files changed

+75
-73
lines changed

6 files changed

+75
-73
lines changed

modules/stores/RouteStore.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,28 @@ var _namedRoutes = {};
1414
var RouteStore = {
1515

1616
/**
17-
* Registers a <Route> and all of its children with the RouteStore. Also,
17+
* Removes all references to <Route>s from the store. Should only ever
18+
* really be used in tests to clear the store between test runs.
19+
*/
20+
unregisterAllRoutes: function () {
21+
_namedRoutes = {};
22+
},
23+
24+
/**
25+
* Removes the reference to the given <Route> and all of its children
26+
* from the store.
27+
*/
28+
unregisterRoute: function (route) {
29+
if (route.props.name)
30+
delete _namedRoutes[route.props.name];
31+
32+
React.Children.forEach(route.props.children, function (child) {
33+
RouteStore.unregisterRoute(route);
34+
});
35+
},
36+
37+
/**
38+
* Registers a <Route> and all of its children with the store. Also,
1839
* does some normalization and validation on route props.
1940
*/
2041
registerRoute: function (route, _parentRoute) {
@@ -64,19 +85,6 @@ var RouteStore = {
6485
});
6586
},
6687

67-
/**
68-
* Removes the reference to the given <Route> and all of its children from
69-
* the RouteStore.
70-
*/
71-
unregisterRoute: function (route) {
72-
if (route.props.name)
73-
delete _namedRoutes[route.props.name];
74-
75-
React.Children.forEach(route.props.children, function (child) {
76-
RouteStore.unregisterRoute(route);
77-
});
78-
},
79-
8088
/**
8189
* Returns the Route object with the given name, if one exists.
8290
*/

specs/AsyncState.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('AsyncState', function () {
3131
}
3232
});
3333

34-
var user = TestUtils.renderIntoDocument(
34+
var user = ReactTestUtils.renderIntoDocument(
3535
User()
3636
);
3737

specs/Route.spec.js

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ var App = React.createClass({
99
}
1010
});
1111

12-
describe('a Route that matches the URL', function () {
12+
describe('a Route that matches a URL', function () {
1313
it('returns an array', function () {
14-
var routes = TestUtils.renderIntoDocument(
14+
var routes = renderComponent(
1515
Routes(null,
1616
Route({ handler: App },
1717
Route({ path: '/a/b/c', handler: App })
@@ -25,11 +25,13 @@ describe('a Route that matches the URL', function () {
2525

2626
var rootMatch = getRootMatch(matches);
2727
expect(rootMatch.params).toEqual({});
28+
29+
removeComponent(routes);
2830
});
2931

3032
describe('that contains dynamic segments', function () {
3133
it('returns an array with the correct params', function () {
32-
var routes = TestUtils.renderIntoDocument(
34+
var routes = renderComponent(
3335
Routes(null,
3436
Route({ handler: App },
3537
Route({ path: '/posts/:id/edit', handler: App })
@@ -43,13 +45,15 @@ describe('a Route that matches the URL', function () {
4345

4446
var rootMatch = getRootMatch(matches);
4547
expect(rootMatch.params).toEqual({ id: 'abc' });
48+
49+
removeComponent(routes);
4650
});
4751
});
4852
});
4953

5054
describe('a Route that does not match the URL', function () {
5155
it('returns null', function () {
52-
var routes = TestUtils.renderIntoDocument(
56+
var routes = renderComponent(
5357
Routes(null,
5458
Route({ handler: App },
5559
Route({ path: '/a/b/c', handler: App })
@@ -58,12 +62,14 @@ describe('a Route that does not match the URL', function () {
5862
);
5963

6064
expect(routes.match('/not-found')).toBe(null);
65+
66+
removeComponent(routes);
6167
});
6268
});
6369

6470
describe('a nested Route that matches the URL', function () {
6571
it('returns the appropriate params for each match', function () {
66-
var routes = TestUtils.renderIntoDocument(
72+
var routes = renderComponent(
6773
Routes(null,
6874
Route({ handler: App },
6975
Route({ name: 'posts', path: '/posts/:id', handler: App },
@@ -84,12 +90,14 @@ describe('a nested Route that matches the URL', function () {
8490
var postsMatch = matches[1];
8591
expect(postsMatch.route.props.name).toEqual('posts');
8692
expect(postsMatch.params).toEqual({ id: 'abc' });
93+
94+
removeComponent(routes);
8795
});
8896
});
8997

9098
describe('multiple nested Router that match the URL', function () {
9199
it('returns the first one in the subtree, depth-first', function () {
92-
var routes = TestUtils.renderIntoDocument(
100+
var routes = renderComponent(
93101
Routes(null,
94102
Route({ handler: App },
95103
Route({ path: '/a', handler: App },
@@ -106,52 +114,8 @@ describe('multiple nested Router that match the URL', function () {
106114

107115
var rootMatch = getRootMatch(matches);
108116
expect(rootMatch.route.props.name).toEqual('expected');
109-
});
110-
});
111-
112-
describe('a route handler', function () {
113-
it('may not receive children', function (done) {
114-
var InvalidHandler = React.createClass({
115-
displayName: 'InvalidHandler',
116-
render: function () {
117-
try {
118-
var result = this.props.activeRouteHandler({}, React.DOM.div());
119-
assert(false, 'activeRouteHandler rendered with children');
120-
return result;
121-
} catch (error) {
122-
assert(error);
123-
}
124-
125-
done();
126-
}
127-
});
128-
129-
var routes = TestUtils.renderIntoDocument(
130-
Routes(null,
131-
Route({ handler: InvalidHandler },
132-
Route({ path: '/home', handler: App })
133-
)
134-
)
135-
);
136-
137-
routes.dispatch('/home');
138-
});
139-
});
140-
141-
describe('a Route', function() {
142-
it('requires a handler');
143-
});
144-
145-
describe('a child route', function() {
146-
describe('path', function() {
147-
it('defaults to /');
148-
it('is not required to start with /');
149-
it('can be inferred from its name');
150-
it('must contain all dynamic segments of its parent route path');
151-
});
152117

153-
describe('name', function() {
154-
it('cannot be reused');
118+
removeComponent(routes);
155119
});
156120
});
157121

specs/RouteStore.spec.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ describe('when a route is looked up by name', function () {
2222
RouteStore.registerRoute(route);
2323
});
2424

25-
afterEach(function () {
26-
RouteStore.unregisterRoute(route);
27-
});
28-
2925
it('returns that route', function () {
3026
expect(RouteStore.getRouteByName('products')).toEqual(route);
3127
});
@@ -58,6 +54,18 @@ describe('when registering a route', function () {
5854
});
5955
});
6056

57+
describe('with the same name as another route', function () {
58+
beforeEach(function () {
59+
RouteStore.registerRoute(Route({ name: 'users', handler: App }));
60+
});
61+
62+
it('throws an error', function () {
63+
expect(function () {
64+
RouteStore.registerRoute(Route({ name: 'users', handler: App }));
65+
}).toThrow(Error);
66+
});
67+
});
68+
6169
describe('that is missing a parameter its parent route needs', function () {
6270
it('throws an error', function () {
6371
expect(function () {

specs/helper.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
assert = require('assert');
22
expect = require('expect');
3+
React = require('react/addons');
4+
ReactTestUtils = React.addons.TestUtils;
35

46
refute = function (condition, message) {
57
assert(!condition, message);
68
};
79

8-
React = require('react/addons');
9-
TestUtils = React.addons.TestUtils;
10+
var RouteStore = require('../modules/stores/RouteStore');
11+
12+
beforeEach(function () {
13+
RouteStore.unregisterAllRoutes();
14+
});
15+
16+
var ExecutionEnvironment = require('react/lib/ExecutionEnvironment');
17+
18+
// TODO: Use this as a guard for tests that require DOM.
19+
__DOM__ = ExecutionEnvironment.canUseDOM;
20+
21+
if (__DOM__) {
22+
var ROOT_NODE = document.createElement('div');
23+
document.body.appendChild(ROOT_NODE);
24+
25+
renderComponent = function (component) {
26+
return React.renderComponent(component, ROOT_NODE);
27+
};
28+
29+
removeComponent = function (component) {
30+
React.unmountComponentAtNode(ROOT_NODE);
31+
};
32+
}

specs/main.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ require('./Path.spec.js');
66
require('./Route.spec.js');
77
require('./RouteStore.spec.js');
88
require('./URLStore.spec.js');
9-

0 commit comments

Comments
 (0)