Skip to content

Commit b73bb1c

Browse files
committed
Merge pull request #2237 from taion/IndexLink-empty-path
[fixed] Don't match empty routes for isActive
2 parents 3a0bd60 + 751ca25 commit b73bb1c

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

modules/__tests__/IndexLink-test.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('An <IndexLink>', function () {
1717
return (
1818
<div>
1919
<ul>
20+
<li><IndexLink id="rootLink" to="/" activeClassName="active">root</IndexLink></li>
2021
<li><IndexLink id="overviewLink" to="/website" activeClassName="active">overview</IndexLink></li>
2122
<li><Link id="contactLink" to="/website/contact" activeClassName="active">contact</Link></li>
2223
<li><Link id="productsLink" to="/website/products" activeClassName="active">products</Link></li>
@@ -29,6 +30,18 @@ describe('An <IndexLink>', function () {
2930
}
3031
}
3132

33+
class RootWrapper extends Component {
34+
render() {
35+
return <div>root wrapper {this.props.children}</div>
36+
}
37+
}
38+
39+
class RootPage extends Component {
40+
render() {
41+
return <div>website root</div>
42+
}
43+
}
44+
3245
class Wrapper extends Component {
3346
render() {
3447
return <div>website wrapper {this.props.children}</div>
@@ -67,13 +80,16 @@ describe('An <IndexLink>', function () {
6780

6881
const routes = (
6982
<Route component={App}>
70-
<Route path="/website" component={Wrapper}>
71-
<Route path="products" component={ProductsPage}>
72-
<Route path=":productId" component={ProductPage} />
73-
<IndexRoute component={ProductsIndexPage} />
83+
<Route path="/" component={RootWrapper}>
84+
<IndexRoute component={RootPage} />
85+
<Route path="website" component={Wrapper}>
86+
<Route path="products" component={ProductsPage}>
87+
<Route path=":productId" component={ProductPage} />
88+
<IndexRoute component={ProductsIndexPage} />
89+
</Route>
90+
<Route path="contact" component={ContactPage} />
91+
<IndexRoute component={IndexPage} />
7492
</Route>
75-
<Route path="contact" component={ContactPage} />
76-
<IndexRoute component={IndexPage} />
7793
</Route>
7894
</Route>
7995
)
@@ -87,11 +103,28 @@ describe('An <IndexLink>', function () {
87103
unmountComponentAtNode(node)
88104
})
89105

106+
describe('when linking to the root', function () {
107+
it('is active and other routes are not', function (done) {
108+
render((
109+
<Router history={createHistory('/')} routes={routes} />
110+
), node, function () {
111+
expect(node.querySelector('#rootLink').className).toEqual('active')
112+
expect(node.querySelector('#overviewLink').className).toEqual('')
113+
expect(node.querySelector('#contactLink').className).toEqual('')
114+
expect(node.querySelector('#productsLink').className).toEqual('')
115+
expect(node.querySelector('#productsIndexLink').className).toEqual('')
116+
expect(node.querySelector('#specificProductLink').className).toEqual('')
117+
done()
118+
})
119+
})
120+
})
121+
90122
describe('when linking to the overview', function () {
91123
it('is active and other routes are not', function (done) {
92124
render((
93125
<Router history={createHistory('/website')} routes={routes} />
94126
), node, function () {
127+
expect(node.querySelector('#rootLink').className).toEqual('')
95128
expect(node.querySelector('#overviewLink').className).toEqual('active')
96129
expect(node.querySelector('#contactLink').className).toEqual('')
97130
expect(node.querySelector('#productsLink').className).toEqual('')
@@ -107,6 +140,7 @@ describe('An <IndexLink>', function () {
107140
render((
108141
<Router history={createHistory('/website/contact')} routes={routes} />
109142
), node, function () {
143+
expect(node.querySelector('#rootLink').className).toEqual('')
110144
expect(node.querySelector('#overviewLink').className).toEqual('')
111145
expect(node.querySelector('#contactLink').className).toEqual('active')
112146
expect(node.querySelector('#productsLink').className).toEqual('')
@@ -122,6 +156,7 @@ describe('An <IndexLink>', function () {
122156
render((
123157
<Router history={createHistory('/website/products')} routes={routes} />
124158
), node, function () {
159+
expect(node.querySelector('#rootLink').className).toEqual('')
125160
expect(node.querySelector('#overviewLink').className).toEqual('')
126161
expect(node.querySelector('#contactLink').className).toEqual('')
127162
expect(node.querySelector('#productsLink').className).toEqual('active')
@@ -137,6 +172,7 @@ describe('An <IndexLink>', function () {
137172
render((
138173
<Router history={createHistory('/website/products/15')} routes={routes} />
139174
), node, function () {
175+
expect(node.querySelector('#rootLink').className).toEqual('')
140176
expect(node.querySelector('#overviewLink').className).toEqual('')
141177
expect(node.querySelector('#contactLink').className).toEqual('')
142178
expect(node.querySelector('#productsLink').className).toEqual('active')

modules/isActive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function getMatchingRoute(pathname, activeRoutes, activeParams) {
4141

4242
let { remainingPathname, paramNames, paramValues } = matchPattern(pattern, pathname)
4343

44-
if (remainingPathname === '' && paramsAreActive(paramNames, paramValues, activeParams))
44+
if (remainingPathname === '' && route.path && paramsAreActive(paramNames, paramValues, activeParams))
4545
return route
4646

4747
basename = pattern

0 commit comments

Comments
 (0)