Skip to content

Commit 2408606

Browse files
committed
Update tests
1 parent d2aa7cb commit 2408606

File tree

7 files changed

+656
-2
lines changed

7 files changed

+656
-2
lines changed

karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ module.exports = function(config) {
66
frameworks: ['mocha', 'browserify'],
77

88
files: [
9-
'specs/main.js'
9+
'tests.js'
1010
],
1111

1212
exclude: [],
1313

1414
preprocessors: {
15-
'specs/main.js': ['browserify']
15+
'tests.js': ['browserify']
1616
},
1717

1818
browserify: {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
var expect = require('expect');
2+
var React = require('react/addons');
3+
var ReactTestUtils = React.addons.TestUtils;
4+
var Route = require('../../components/Route');
5+
var PathDelegate = require('../PathDelegate');
6+
7+
describe('PathDelegate', function () {
8+
9+
var App = React.createClass({
10+
mixins: [ PathDelegate ],
11+
render: function () {
12+
return React.DOM.div();
13+
}
14+
});
15+
16+
describe('makePath', function () {
17+
describe('when there is a route with the given name', function () {
18+
var component;
19+
beforeEach(function () {
20+
component = ReactTestUtils.renderIntoDocument(
21+
App(null,
22+
Route({ name: 'home', path: '/:username/home', handler: App })
23+
)
24+
);
25+
});
26+
27+
afterEach(function () {
28+
React.unmountComponentAtNode(component.getDOMNode());
29+
});
30+
31+
it('creates the correct path', function () {
32+
expect(component.makePath('home', { username: 'mjackson' })).toEqual('/mjackson/home');
33+
});
34+
});
35+
36+
describe('when there is no route with the given name', function () {
37+
var component;
38+
beforeEach(function () {
39+
component = ReactTestUtils.renderIntoDocument(
40+
App()
41+
);
42+
});
43+
44+
afterEach(function () {
45+
React.unmountComponentAtNode(component.getDOMNode());
46+
});
47+
48+
it('creates the correct path', function () {
49+
expect(function () {
50+
component.makePath('home');
51+
}).toThrow('Unable to find a route named "home". Make sure you have a <Route name="home"> defined somewhere in your <Routes>');
52+
});
53+
});
54+
});
55+
56+
describe('makeHref', function () {
57+
describe('when using "hash" location', function () {
58+
var component;
59+
beforeEach(function () {
60+
component = ReactTestUtils.renderIntoDocument(
61+
App({ location: 'hash' },
62+
Route({ name: 'home', handler: App })
63+
)
64+
);
65+
});
66+
67+
afterEach(function () {
68+
React.unmountComponentAtNode(component.getDOMNode());
69+
});
70+
71+
it('puts a # in front of the URL', function () {
72+
expect(component.makeHref('home')).toEqual('#/home');
73+
});
74+
});
75+
76+
describe('when using "history" location', function () {
77+
var component;
78+
beforeEach(function () {
79+
component = ReactTestUtils.renderIntoDocument(
80+
App({ location: 'history' },
81+
Route({ name: 'home', handler: App })
82+
)
83+
);
84+
});
85+
86+
afterEach(function () {
87+
React.unmountComponentAtNode(component.getDOMNode());
88+
});
89+
90+
it('returns the correct URL', function () {
91+
expect(component.makeHref('home')).toEqual('/home');
92+
});
93+
});
94+
});
95+
96+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var expect = require('expect');
2+
var React = require('react/addons');
3+
var ReactTestUtils = React.addons.TestUtils;
4+
var HashLocation = require('../../locations/HashLocation');
5+
var HistoryLocation = require('../../locations/HistoryLocation');
6+
var PathState = require('../PathState');
7+
8+
describe('PathState', function () {
9+
var App = React.createClass({
10+
mixins: [ PathState ],
11+
render: function () {
12+
return React.DOM.div();
13+
}
14+
});
15+
16+
describe('when using location="none"', function () {
17+
var component;
18+
beforeEach(function () {
19+
component = ReactTestUtils.renderIntoDocument(
20+
App({ location: 'none' })
21+
);
22+
});
23+
24+
afterEach(function () {
25+
React.unmountComponentAtNode(component.getDOMNode());
26+
});
27+
28+
it('has a null location', function () {
29+
expect(component.getLocation()).toBe(null);
30+
});
31+
});
32+
33+
describe('when using location="hash"', function () {
34+
var component;
35+
beforeEach(function () {
36+
component = ReactTestUtils.renderIntoDocument(
37+
App({ location: 'hash' })
38+
);
39+
});
40+
41+
afterEach(function () {
42+
React.unmountComponentAtNode(component.getDOMNode());
43+
});
44+
45+
it('has a null location', function () {
46+
expect(component.getLocation()).toBe(HashLocation);
47+
});
48+
});
49+
50+
describe('when using location="history"', function () {
51+
var component;
52+
beforeEach(function () {
53+
component = ReactTestUtils.renderIntoDocument(
54+
App({ location: 'history' })
55+
);
56+
});
57+
58+
afterEach(function () {
59+
React.unmountComponentAtNode(component.getDOMNode());
60+
});
61+
62+
it('has a null location', function () {
63+
expect(component.getLocation()).toBe(HistoryLocation);
64+
});
65+
});
66+
});
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
var expect = require('expect');
2+
var React = require('react/addons');
3+
var ReactTestUtils = React.addons.TestUtils;
4+
var Route = require('../../components/Route');
5+
var RouteContainer = require('../RouteContainer');
6+
7+
describe('RouteContainer', function () {
8+
var App = React.createClass({
9+
mixins: [ RouteContainer ],
10+
render: function () {
11+
return React.DOM.div();
12+
}
13+
});
14+
15+
describe('getRouteByName', function () {
16+
describe('when the route exists', function () {
17+
var component, route;
18+
beforeEach(function () {
19+
component = ReactTestUtils.renderIntoDocument(
20+
App(null, route = Route({ name: 'home', handler: App }))
21+
)
22+
});
23+
24+
afterEach(function () {
25+
React.unmountComponentAtNode(component.getDOMNode());
26+
});
27+
28+
it('returns that route', function () {
29+
expect(component.getRouteByName('home')).toBe(route);
30+
});
31+
});
32+
33+
describe('when no such route exists', function () {
34+
var component;
35+
beforeEach(function () {
36+
component = ReactTestUtils.renderIntoDocument(
37+
App(null, Route({ name: 'home', handler: App }))
38+
)
39+
});
40+
41+
afterEach(function () {
42+
React.unmountComponentAtNode(component.getDOMNode());
43+
});
44+
45+
it('returns null', function () {
46+
expect(component.getRouteByName('about')).toBe(null);
47+
});
48+
});
49+
});
50+
51+
describe('when a <Route> has no name or path', function () {
52+
var component, route;
53+
beforeEach(function () {
54+
component = ReactTestUtils.renderIntoDocument(
55+
App(null,
56+
route = Route({ handler: App })
57+
)
58+
);
59+
});
60+
61+
afterEach(function () {
62+
React.unmountComponentAtNode(component.getDOMNode());
63+
});
64+
65+
it('uses / as its path', function () {
66+
expect(route.props.path).toEqual('/');
67+
});
68+
});
69+
70+
describe('when a <Route> has a name but no path', function () {
71+
var component, route;
72+
beforeEach(function () {
73+
component = ReactTestUtils.renderIntoDocument(
74+
App(null,
75+
route = Route({ name: 'home', handler: App })
76+
)
77+
);
78+
});
79+
80+
afterEach(function () {
81+
React.unmountComponentAtNode(component.getDOMNode());
82+
});
83+
84+
it('uses /:name as its path', function () {
85+
expect(route.props.path).toEqual('/home');
86+
});
87+
});
88+
89+
describe('when a nested <Route>\'s path does not begin with a /', function () {
90+
var component, childRoute;
91+
beforeEach(function () {
92+
component = ReactTestUtils.renderIntoDocument(
93+
App(null,
94+
Route({ name: 'home', handler: App },
95+
childRoute = Route({ path: 'sub', handler: App })
96+
)
97+
)
98+
);
99+
});
100+
101+
afterEach(function () {
102+
React.unmountComponentAtNode(component.getDOMNode());
103+
});
104+
105+
it('extends the parent path', function () {
106+
expect(childRoute.props.path).toEqual('/home/sub');
107+
});
108+
});
109+
110+
describe('when a nested <Route>\'s path begins with a /', function () {
111+
var component, childRoute;
112+
beforeEach(function () {
113+
component = ReactTestUtils.renderIntoDocument(
114+
App(null,
115+
Route({ name: 'home', handler: App },
116+
childRoute = Route({ path: '/sub', handler: App })
117+
)
118+
)
119+
);
120+
});
121+
122+
afterEach(function () {
123+
React.unmountComponentAtNode(component.getDOMNode());
124+
});
125+
126+
it('does not extend the parent path', function () {
127+
expect(childRoute.props.path).toEqual('/sub');
128+
});
129+
});
130+
});

0 commit comments

Comments
 (0)