Skip to content

Commit 2732586

Browse files
pjukemjackson
authored andcommitted
Add failing test
1 parent aabd195 commit 2732586

File tree

1 file changed

+85
-22
lines changed

1 file changed

+85
-22
lines changed

modules/__tests__/IndexLink-test.js

Lines changed: 85 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import expect from 'expect'
44
import React from 'react'
55
import createHistory from 'history/lib/createMemoryHistory'
66
import IndexRoute from '../IndexRoute'
7+
import IndexLink from '../IndexLink'
78
import Router from '../Router'
89
import Route from '../Route'
9-
import IndexLink from '../IndexLink'
10+
import Link from '../Link'
1011

1112
describe('An <IndexLink>', function () {
1213

@@ -15,34 +16,64 @@ describe('An <IndexLink>', function () {
1516
return (
1617
<div>
1718
<ul>
18-
<li><IndexLink id="appLink" to="/" activeClassName="active">app</IndexLink></li>
19-
<li><IndexLink id="deepLink" to="/deep" activeClassName="active">deep</IndexLink></li>
19+
<li><IndexLink id="overviewLink" to="/website" activeClassName="active">overview</IndexLink></li>
20+
<li><Link id="contactLink" to="/website/contact" activeClassName="active">contact</Link></li>
21+
<li><IndexLink id="productsLink" to="/website/products" activeClassName="active">products</IndexLink></li>
22+
<li><Link id="specificProductLink" to="/website/products/15" activeClassName="active">specific product</Link></li>
2023
</ul>
2124
{this.props.children}
2225
</div>
2326
)
2427
}
2528
}
2629

27-
class Parent extends React.Component {
30+
class Website extends React.Component {
31+
render() {
32+
return <div>website wrapper {this.props.children}</div>
33+
}
34+
}
35+
36+
class WebsiteOverview extends React.Component {
37+
render() {
38+
return <div>website overview</div>
39+
}
40+
}
41+
42+
class WebsiteContact extends React.Component {
43+
render() {
44+
return <div>contact page</div>
45+
}
46+
}
47+
48+
class WebsiteProducts extends React.Component {
49+
render() {
50+
return <div>website products {this.props.children}</div>
51+
}
52+
}
53+
54+
class WebsiteProductsProduct extends React.Component {
2855
render() {
29-
return <div>parent {this.props.children}</div>
56+
return <div>specific product {this.props.params.productId}</div>
3057
}
3158
}
3259

33-
class Child extends React.Component {
60+
class WebsiteProductsIndex extends React.Component {
3461
render() {
35-
return <div>child </div>
62+
return <div>list of products</div>
3663
}
3764
}
3865

3966
const routes = (
40-
<Route path="/" component={App}>
41-
<IndexRoute component={Child} />
42-
<Route path="/deep" component={Parent}>
43-
<IndexRoute component={Child} />
67+
<Route component={App}>
68+
<Route path="/website" component={Website}>
69+
<Route path="products" component={WebsiteProducts}>
70+
<Route path=":productId" component={WebsiteProductsProduct} />
71+
<IndexRoute component={WebsiteProductsIndex} />
72+
</Route>
73+
<Route path="contact" component={WebsiteContact} />
74+
<IndexRoute component={WebsiteOverview} />
4475
</Route>
45-
</Route>
76+
</Route>
4677
)
4778

4879
let node
@@ -54,25 +85,57 @@ describe('An <IndexLink>', function () {
5485
React.unmountComponentAtNode(node)
5586
})
5687

57-
describe('when linking to the root', function () {
58-
it("is active when the parent's route is active", function (done) {
88+
describe('when linking to the overview', function () {
89+
it('is active and other routes are not', function (done) {
90+
React.render((
91+
<Router history={createHistory('/website')} routes={routes} />
92+
), node, function () {
93+
expect(node.querySelector('#overviewLink').className).toEqual('active')
94+
expect(node.querySelector('#contactLink').className).toEqual('')
95+
expect(node.querySelector('#productsLink').className).toEqual('')
96+
expect(node.querySelector('#specificProductLink').className).toEqual('')
97+
done()
98+
})
99+
})
100+
})
101+
102+
describe('when linking to the contact', function () {
103+
it('is active and other routes are not', function (done) {
104+
React.render((
105+
<Router history={createHistory('/website/contact')} routes={routes} />
106+
), node, function () {
107+
expect(node.querySelector('#overviewLink').className).toEqual('')
108+
expect(node.querySelector('#contactLink').className).toEqual('active')
109+
expect(node.querySelector('#productsLink').className).toEqual('')
110+
expect(node.querySelector('#specificProductLink').className).toEqual('')
111+
done()
112+
})
113+
})
114+
})
115+
116+
describe('when linking to the products', function () {
117+
it('is active and other routes are not', function (done) {
59118
React.render((
60-
<Router history={createHistory('/')} routes={routes} />
119+
<Router history={createHistory('/website/products')} routes={routes} />
61120
), node, function () {
62-
expect(node.querySelector('#appLink').className).toEqual('active')
63-
expect(node.querySelector('#deepLink').className).toEqual('')
121+
expect(node.querySelector('#overviewLink').className).toEqual('')
122+
expect(node.querySelector('#contactLink').className).toEqual('')
123+
expect(node.querySelector('#productsLink').className).toEqual('active')
124+
expect(node.querySelector('#specificProductLink').className).toEqual('')
64125
done()
65126
})
66127
})
67128
})
68129

69-
describe('when linking deep into the route hierarchy', function () {
70-
it("is active when the parent's route is active", function (done) {
130+
describe('when linking to a specific product', function () {
131+
it("is active and it's parent is also active", function (done) {
71132
React.render((
72-
<Router history={createHistory('/deep')} routes={routes} />
133+
<Router history={createHistory('/website/products/15')} routes={routes} />
73134
), node, function () {
74-
expect(node.querySelector('#appLink').className).toEqual('')
75-
expect(node.querySelector('#deepLink').className).toEqual('active')
135+
expect(node.querySelector('#overviewLink').className).toEqual('')
136+
expect(node.querySelector('#contactLink').className).toEqual('')
137+
expect(node.querySelector('#productsLink').className).toEqual('active')
138+
expect(node.querySelector('#specificProductLink').className).toEqual('active')
76139
done()
77140
})
78141
})

0 commit comments

Comments
 (0)