Skip to content

Commit d86ac40

Browse files
committed
Add tests for ActiveDelegate
1 parent c456d8d commit d86ac40

File tree

3 files changed

+118
-3
lines changed

3 files changed

+118
-3
lines changed

modules/mixins/ActiveDelegate.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,23 @@ var ActiveDelegate = {
4141
};
4242
},
4343

44+
propTypes: {
45+
initialState: React.PropTypes.object
46+
},
47+
48+
getDefaultProps: function () {
49+
return {
50+
initialState: {}
51+
};
52+
},
53+
4454
getInitialState: function () {
55+
var initialState = this.props.initialState;
56+
4557
return {
46-
activeRoutes: [],
47-
activeParams: {},
48-
activeQuery: {}
58+
activeRoutes: initialState.activeRoutes || [],
59+
activeParams: initialState.activeParams || {},
60+
activeQuery: initialState.activeQuery || {}
4961
};
5062
},
5163

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var assert = require('assert');
2+
var React = require('react/addons');
3+
var ReactTestUtils = React.addons.TestUtils;
4+
var Route = require('../../components/Route');
5+
var ActiveDelegate = require('../ActiveDelegate');
6+
7+
describe('ActiveDelegate', function () {
8+
var App = React.createClass({
9+
mixins: [ ActiveDelegate ],
10+
render: function () {
11+
return React.DOM.div();
12+
}
13+
});
14+
15+
describe('when a route is active', function () {
16+
var route;
17+
beforeEach(function () {
18+
route = Route({ name: 'products', handler: App });
19+
});
20+
21+
describe('and it has no params', function () {
22+
var component;
23+
beforeEach(function () {
24+
component = ReactTestUtils.renderIntoDocument(
25+
App({
26+
initialState: {
27+
activeRoutes: [ route ]
28+
}
29+
})
30+
);
31+
});
32+
33+
afterEach(function () {
34+
React.unmountComponentAtNode(component.getDOMNode());
35+
});
36+
37+
it('is active', function () {
38+
assert(component.isActive('products'));
39+
});
40+
});
41+
42+
describe('and the right params are given', function () {
43+
var component;
44+
beforeEach(function () {
45+
component = ReactTestUtils.renderIntoDocument(
46+
App({
47+
initialState: {
48+
activeRoutes: [ route ],
49+
activeParams: { id: '123', show: 'true', variant: 456 },
50+
activeQuery: { search: 'abc', limit: 789 }
51+
}
52+
})
53+
);
54+
});
55+
56+
afterEach(function () {
57+
React.unmountComponentAtNode(component.getDOMNode());
58+
});
59+
60+
describe('and no query is used', function () {
61+
it('is active', function () {
62+
assert(component.isActive('products', { id: 123, variant: '456' }));
63+
});
64+
});
65+
66+
describe('and a matching query is used', function () {
67+
it('is active', function () {
68+
assert(component.isActive('products', { id: 123 }, { search: 'abc', limit: '789' }));
69+
});
70+
});
71+
72+
describe('but the query does not match', function () {
73+
it('is not active', function () {
74+
assert(component.isActive('products', { id: 123 }, { search: 'def', limit: '123' }) === false);
75+
});
76+
});
77+
});
78+
79+
describe('and the wrong params are given', function () {
80+
var component;
81+
beforeEach(function () {
82+
component = ReactTestUtils.renderIntoDocument(
83+
App({
84+
initialState: {
85+
activeRoutes: [ route ],
86+
activeParams: { id: 123 }
87+
}
88+
})
89+
);
90+
});
91+
92+
afterEach(function () {
93+
React.unmountComponentAtNode(component.getDOMNode());
94+
});
95+
96+
it('is not active', function () {
97+
assert(component.isActive('products', { id: 345 }) === false);
98+
});
99+
});
100+
});
101+
102+
});

tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require('./modules/components/__tests__/DefaultRoute-test');
22
require('./modules/components/__tests__/NotFoundRoute-test');
3+
require('./modules/mixins/__tests__/ActiveDelegate-test');
34
require('./modules/mixins/__tests__/PathDelegate-test');
45
require('./modules/mixins/__tests__/PathState-test');
56
require('./modules/mixins/__tests__/RouteContainer-test');

0 commit comments

Comments
 (0)