@@ -4,9 +4,10 @@ import expect from 'expect'
4
4
import React from 'react'
5
5
import createHistory from 'history/lib/createMemoryHistory'
6
6
import IndexRoute from '../IndexRoute'
7
+ import IndexLink from '../IndexLink'
7
8
import Router from '../Router'
8
9
import Route from '../Route'
9
- import IndexLink from '../IndexLink '
10
+ import Link from '../Link '
10
11
11
12
describe ( 'An <IndexLink>' , function ( ) {
12
13
@@ -15,34 +16,64 @@ describe('An <IndexLink>', function () {
15
16
return (
16
17
< div >
17
18
< 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 >
20
23
</ ul >
21
24
{ this . props . children }
22
25
</ div >
23
26
)
24
27
}
25
28
}
26
29
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 {
28
55
render ( ) {
29
- return < div > parent { this . props . children } </ div >
56
+ return < div > specific product { this . props . params . productId } </ div >
30
57
}
31
58
}
32
59
33
- class Child extends React . Component {
60
+ class WebsiteProductsIndex extends React . Component {
34
61
render ( ) {
35
- return < div > child </ div >
62
+ return < div > list of products </ div >
36
63
}
37
64
}
38
65
39
66
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 } />
44
75
</ Route >
45
- </ Route >
76
+ </ Route >
46
77
)
47
78
48
79
let node
@@ -54,25 +85,57 @@ describe('An <IndexLink>', function () {
54
85
React . unmountComponentAtNode ( node )
55
86
} )
56
87
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 ) {
59
118
React . render ( (
60
- < Router history = { createHistory ( '/' ) } routes = { routes } />
119
+ < Router history = { createHistory ( '/website/products ' ) } routes = { routes } />
61
120
) , 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 ( '' )
64
125
done ( )
65
126
} )
66
127
} )
67
128
} )
68
129
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 ) {
71
132
React . render ( (
72
- < Router history = { createHistory ( '/deep ' ) } routes = { routes } />
133
+ < Router history = { createHistory ( '/website/products/15 ' ) } routes = { routes } />
73
134
) , 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' )
76
139
done ( )
77
140
} )
78
141
} )
0 commit comments