forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexLink-test.js
More file actions
184 lines (165 loc) · 6.4 KB
/
IndexLink-test.js
File metadata and controls
184 lines (165 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import expect from 'expect'
import React, { Component } from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import createHistory from '../createMemoryHistory'
import IndexRoute from '../IndexRoute'
import IndexLink from '../IndexLink'
import Router from '../Router'
import Route from '../Route'
import Link from '../Link'
describe('An <IndexLink>', function () {
class App extends Component {
render() {
return (
<div>
<ul>
<li><IndexLink id="rootLink" to="/" activeClassName="active">root</IndexLink></li>
<li><IndexLink id="overviewLink" to="/website" activeClassName="active">overview</IndexLink></li>
<li><Link id="contactLink" to="/website/contact" activeClassName="active">contact</Link></li>
<li><Link id="productsLink" to="/website/products" activeClassName="active">products</Link></li>
<li><IndexLink id="productsIndexLink" to="/website/products" activeClassName="active">products index</IndexLink></li>
<li><Link id="specificProductLink" to="/website/products/15" activeClassName="active">specific product</Link></li>
</ul>
{this.props.children}
</div>
)
}
}
class RootWrapper extends Component {
render() {
return <div>root wrapper {this.props.children}</div>
}
}
class RootPage extends Component {
render() {
return <div>website root</div>
}
}
class Wrapper extends Component {
render() {
return <div>website wrapper {this.props.children}</div>
}
}
class IndexPage extends Component {
render() {
return <div>website overview</div>
}
}
class ContactPage extends Component {
render() {
return <div>contact page</div>
}
}
class ProductsPage extends Component {
render() {
return <div>website products {this.props.children}</div>
}
}
class ProductPage extends Component {
render() {
return <div>specific product {this.props.params.productId}</div>
}
}
class ProductsIndexPage extends Component {
render() {
return <div>list of products</div>
}
}
const routes = (
<Route component={App}>
<Route path="/" component={RootWrapper}>
<IndexRoute component={RootPage} />
<Route path="website" component={Wrapper}>
<Route path="products" component={ProductsPage}>
<Route path=":productId" component={ProductPage} />
<IndexRoute component={ProductsIndexPage} />
</Route>
<Route path="contact" component={ContactPage} />
<IndexRoute component={IndexPage} />
</Route>
</Route>
</Route>
)
let node
beforeEach(function () {
node = document.createElement('div')
})
afterEach(function () {
unmountComponentAtNode(node)
})
describe('when linking to the root', function () {
it('is active and other routes are not', function (done) {
render((
<Router history={createHistory('/')} routes={routes} />
), node, function () {
expect(node.querySelector('#rootLink').className).toEqual('active')
expect(node.querySelector('#overviewLink').className).toEqual('')
expect(node.querySelector('#contactLink').className).toEqual('')
expect(node.querySelector('#productsLink').className).toEqual('')
expect(node.querySelector('#productsIndexLink').className).toEqual('')
expect(node.querySelector('#specificProductLink').className).toEqual('')
done()
})
})
})
describe('when linking to the overview', function () {
it('is active and other routes are not', function (done) {
render((
<Router history={createHistory('/website')} routes={routes} />
), node, function () {
expect(node.querySelector('#rootLink').className).toEqual('')
expect(node.querySelector('#overviewLink').className).toEqual('active')
expect(node.querySelector('#contactLink').className).toEqual('')
expect(node.querySelector('#productsLink').className).toEqual('')
expect(node.querySelector('#productsIndexLink').className).toEqual('')
expect(node.querySelector('#specificProductLink').className).toEqual('')
done()
})
})
})
describe('when linking to the contact', function () {
it('is active and other routes are not', function (done) {
render((
<Router history={createHistory('/website/contact')} routes={routes} />
), node, function () {
expect(node.querySelector('#rootLink').className).toEqual('')
expect(node.querySelector('#overviewLink').className).toEqual('')
expect(node.querySelector('#contactLink').className).toEqual('active')
expect(node.querySelector('#productsLink').className).toEqual('')
expect(node.querySelector('#productsIndexLink').className).toEqual('')
expect(node.querySelector('#specificProductLink').className).toEqual('')
done()
})
})
})
describe('when linking to the products', function () {
it('is active and other routes are not', function (done) {
render((
<Router history={createHistory('/website/products')} routes={routes} />
), node, function () {
expect(node.querySelector('#rootLink').className).toEqual('')
expect(node.querySelector('#overviewLink').className).toEqual('')
expect(node.querySelector('#contactLink').className).toEqual('')
expect(node.querySelector('#productsLink').className).toEqual('active')
expect(node.querySelector('#productsIndexLink').className).toEqual('active')
expect(node.querySelector('#specificProductLink').className).toEqual('')
done()
})
})
})
describe('when linking to a specific product', function () {
it("is active and it's parent is also active", function (done) {
render((
<Router history={createHistory('/website/products/15')} routes={routes} />
), node, function () {
expect(node.querySelector('#rootLink').className).toEqual('')
expect(node.querySelector('#overviewLink').className).toEqual('')
expect(node.querySelector('#contactLink').className).toEqual('')
expect(node.querySelector('#productsLink').className).toEqual('active')
expect(node.querySelector('#productsIndexLink').className).toEqual('')
expect(node.querySelector('#specificProductLink').className).toEqual('active')
done()
})
})
})
})