Skip to content

Commit cb31040

Browse files
Muhtasim Ahsantaion
authored andcommitted
Add test for index links on dynamic routes
1 parent e2a979e commit cb31040

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*eslint-env mocha */
2+
import expect from 'expect'
3+
import React, { Component } from 'react'
4+
import { render, unmountComponentAtNode } from 'react-dom'
5+
import createHistory from 'history/lib/createMemoryHistory'
6+
import IndexLink from '../IndexLink'
7+
import execSteps from './execSteps'
8+
import Router from '../Router'
9+
import Link from '../Link'
10+
11+
describe('Dynamic Routes', function () {
12+
13+
class App extends Component {
14+
render() {
15+
return (
16+
<div>
17+
<ul>
18+
<li><IndexLink id="overviewLink" to="/website" activeClassName="active">overview</IndexLink></li>
19+
<li><Link id="contactLink" to="/website/contact" activeClassName="active">contact</Link></li>
20+
</ul>
21+
{this.props.children}
22+
</div>
23+
)
24+
}
25+
}
26+
27+
class Website extends Component {
28+
render() {
29+
return <div>website wrapper {this.props.children}</div>
30+
}
31+
}
32+
33+
class ContactPage extends Component {
34+
render() {
35+
return <div>contact page</div>
36+
}
37+
}
38+
39+
class IndexPage extends Component {
40+
render() {
41+
return <div>index page</div>
42+
}
43+
}
44+
45+
const routes = {
46+
childRoutes: [ {
47+
path: '/',
48+
component: App,
49+
childRoutes: [
50+
{
51+
path: 'website',
52+
component: Website,
53+
childRoutes: [
54+
{ path: 'contact', component: ContactPage }
55+
],
56+
getIndexRoute(location, callback) {
57+
setTimeout(function () {
58+
callback(null, { component: IndexPage } )
59+
})
60+
}
61+
}
62+
]
63+
} ]
64+
}
65+
66+
let node
67+
beforeEach(function () {
68+
node = document.createElement('div')
69+
})
70+
71+
afterEach(function () {
72+
unmountComponentAtNode(node)
73+
})
74+
75+
describe('when linking to an index link', function () {
76+
it('is active and non-index routes are not', function (done) {
77+
let overviewLink, contactLink
78+
const steps = [
79+
function () {
80+
overviewLink = node.querySelector('#overviewLink')
81+
contactLink = node.querySelector('#contactLink')
82+
expect(overviewLink.className).toEqual('')
83+
expect(contactLink.className).toEqual('active')
84+
this.history.pushState(null, '/website')
85+
},
86+
function () {
87+
expect(overviewLink.className).toEqual('active')
88+
expect(contactLink.className).toEqual('')
89+
}
90+
]
91+
92+
const execNextStep = execSteps(steps, done)
93+
94+
render((
95+
<Router onUpdate={execNextStep} history={createHistory('/website/contact')} routes={routes} />
96+
), node, execNextStep)
97+
})
98+
})
99+
100+
})

0 commit comments

Comments
 (0)