Skip to content

Commit 47eb68e

Browse files
committed
Fix more specs
1 parent ab3ffef commit 47eb68e

File tree

6 files changed

+168
-225
lines changed

6 files changed

+168
-225
lines changed

modules/stores/URLStore.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
var ExecutionEnvironment = require('react/lib/ExecutionEnvironment');
12
var invariant = require('react/lib/invariant');
23
var warning = require('react/lib/warning');
3-
var ExecutionEnvironment = require('react/lib/ExecutionEnvironment');
4-
var normalizePath = require('../helpers/Path').normalize;
54

65
var CHANGE_EVENTS = {
76
hash: 'hashchange',
@@ -61,7 +60,7 @@ var URLStore = {
6160
window.location.hash = path;
6261
} else {
6362
_lastPath = _currentPath;
64-
_currentPath = normalizePath(path);
63+
_currentPath = path;
6564
notifyChange();
6665
}
6766
},
@@ -77,7 +76,7 @@ var URLStore = {
7776
} else if (_location === 'hash') {
7877
window.location.replace(getWindowPath() + '#' + path);
7978
} else {
80-
_currentPath = normalizePath(path);
79+
_currentPath = path;
8180
notifyChange();
8281
}
8382
},

specs/Route.spec.js

Lines changed: 111 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require('./helper');
2-
var Route = require('../modules/Route');
3-
var RouteComponent = require('../modules/components/Route');
2+
var Route = require('../modules/components/Route');
43

54
var App = React.createClass({
65
displayName: 'App',
@@ -9,53 +8,129 @@ var App = React.createClass({
98
}
109
});
1110

12-
describe('a Route with a named handler', function () {
13-
var route;
14-
beforeEach(function () {
15-
route = new Route({ handler: App });
16-
});
11+
describe('a Route that matches the URL', function () {
12+
it('returns an array', function () {
13+
var route = Route({ path: '/a/b/c', handler: App });
14+
15+
var matches = route.match('/a/b/c');
16+
assert(matches);
17+
expect(matches.length).toEqual(1);
1718

18-
it('has the correct displayName', function () {
19-
expect(route.displayName).toEqual('AppRoute');
19+
var rootMatch = getRootMatch(matches);
20+
expect(rootMatch.params).toEqual({});
2021
});
2122

22-
it('has the correct toString representation', function () {
23-
expect(route + '').toEqual('<AppRoute>');
23+
describe('that contains dynamic segments', function () {
24+
it('returns an array with the correct params', function () {
25+
var route = Route({ path: '/posts/:id/edit', handler: App });
26+
27+
var matches = route.match('/posts/abc/edit');
28+
assert(matches);
29+
expect(matches.length).toEqual(1);
30+
31+
var rootMatch = getRootMatch(matches);
32+
expect(rootMatch.params).toEqual({ id: 'abc' });
33+
});
2434
});
2535
});
2636

27-
describe('a Route without a handler component', function () {
28-
it('throws an Error', function () {
29-
expect(function () {
30-
new Route({ handler: null });
31-
}).toThrow(Error);
37+
describe('a Route that does not match the URL', function () {
38+
it('returns null', function () {
39+
var route = Route({ path: '/a/b/c', handler: App });
40+
expect(route.match('/not-found')).toBe(null);
3241
});
3342
});
3443

35-
describe('a Route with no path', function () {
36-
describe('or a name', function () {
37-
it('uses / as its path', function () {
38-
var route = new Route({ handler: App });
39-
expect(route.path).toEqual('/');
40-
});
44+
describe('a nested Route that matches the URL', function () {
45+
it('returns the appropriate params for each match', function () {
46+
var route = Route({ name: 'posts', path: '/posts/:id', handler: App },
47+
Route({ name: 'comment', path: '/posts/:id/comments/:commentId', handler: App })
48+
);
49+
50+
var matches = route.match('/posts/abc/comments/123');
51+
assert(matches);
52+
expect(matches.length).toEqual(2);
53+
54+
var rootMatch = getRootMatch(matches);
55+
expect(rootMatch.route.props.name).toEqual('comment');
56+
expect(rootMatch.params).toEqual({ id: 'abc', commentId: '123' });
57+
58+
var firstMatch = matches[0];
59+
expect(firstMatch.route.props.name).toEqual('posts');
60+
expect(firstMatch.params).toEqual({ id: 'abc' });
4161
});
62+
});
4263

43-
describe('and a name', function () {
44-
it('uses its name as its path', function () {
45-
var route = new Route({ name: 'users', handler: App });
46-
expect(route.path).toEqual('/users');
47-
});
64+
describe('multiple nested Router that match the URL', function () {
65+
it('returns the first one in the subtree, depth-first', function () {
66+
var route = Route({ path: '/', handler: App },
67+
Route({ path: '/a', handler: App },
68+
Route({ path: '/a/b', name: 'expected', handler: App })
69+
),
70+
Route({ path: '/a/b', handler: App })
71+
);
72+
73+
var matches = route.match('/a/b');
74+
assert(matches);
75+
expect(matches.length).toEqual(3);
76+
77+
var rootMatch = getRootMatch(matches);
78+
expect(rootMatch.route.props.name).toEqual('expected');
4879
});
4980
});
5081

51-
describe('a Route that is missing a parameter that its parent Route needs', function () {
52-
it('throws an Error', function () {
53-
expect(function () {
54-
Route.fromComponent(
55-
RouteComponent({ path: '/users/:userId', handler: App },
56-
RouteComponent({ path: '/users/:id/comments', handler: App })
57-
)
58-
);
59-
}).toThrow(/missing the "userId" parameter/);
82+
describe('a Route with custom props', function() {
83+
it('receives props', function (done) {
84+
var route = Route({ handler: App, customProp: 'prop' });
85+
var component = TestUtils.renderIntoDocument(route);
86+
87+
route.dispatch('/').then(function () {
88+
assert(component.props.customProp);
89+
expect(component.props.customProp).toEqual('prop');
90+
done();
91+
});
6092
});
6193
});
94+
95+
// describe('a Router that renders on the server', function() {
96+
// it('works with async willTransitionTo()', function(done) {
97+
// var dataStore = 'goodbye';
98+
// var Layout = React.createClass({
99+
// render: function() {
100+
// return React.DOM.article(null, this.props.activeRoute);
101+
// }
102+
// });
103+
// var AsyncApp = React.createClass({
104+
// displayName: 'AsyncApp',
105+
// statics: {
106+
// willTransitionTo: function() {
107+
// return new Promise(function(resolve) {
108+
// setTimeout(function() {
109+
// dataStore = 'hello';
110+
// resolve();
111+
// }, 0);
112+
// });
113+
// }
114+
// },
115+
// render: function() {
116+
// return React.DOM.div(null, dataStore);
117+
// }
118+
// });
119+
120+
// var router = Router(
121+
// RouteComponent({ path: '/', handler: Layout},
122+
// RouteComponent({ path: '/a', handler: AsyncApp }))
123+
// );
124+
125+
// router.renderComponentToString('/a').then(function(result) {
126+
// expect(result.indexOf('div') > -1).toBe(true);
127+
// expect(result.indexOf('hello') > -1).toBe(true);
128+
129+
// done();
130+
// });
131+
// });
132+
// });
133+
134+
function getRootMatch(matches) {
135+
return matches[matches.length - 1];
136+
}

specs/RouteStore.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,43 @@ describe('when a route is looked up by name', function () {
3030
expect(RouteStore.getRouteByName('products')).toEqual(route);
3131
});
3232
});
33+
34+
describe('when registering a route', function () {
35+
describe('with no handler', function () {
36+
it('throws an Error', function () {
37+
expect(function () {
38+
RouteStore.registerRoute(Route());
39+
}).toThrow(Error);
40+
});
41+
});
42+
43+
describe('with no path or name', function () {
44+
it('uses / as its path', function () {
45+
var route = Route({ handler: App });
46+
RouteStore.registerRoute(route);
47+
expect(route.props.path).toEqual('/');
48+
RouteStore.unregisterRoute(route);
49+
});
50+
});
51+
52+
describe('with a name but no path', function () {
53+
it('uses its name as its path', function () {
54+
var route = Route({ name: 'users', handler: App });
55+
RouteStore.registerRoute(route);
56+
expect(route.props.path).toEqual('/users');
57+
RouteStore.unregisterRoute(route);
58+
});
59+
});
60+
61+
describe('that is missing a parameter its parent route needs', function () {
62+
it('throws an error', function () {
63+
expect(function () {
64+
var childRoute;
65+
var route = Route({ path: '/users/:userID' },
66+
childRoute = Route({ path: '/users/:id/comments '})
67+
);
68+
RouteStore.registerRoute(childRoute);
69+
}).toThrow(Error);
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)