@@ -7,20 +7,28 @@ import Route from '../Route'
7
7
8
8
describe ( 'matchRoutes' , function ( ) {
9
9
10
- let routes , RootRoute , UsersRoute , UsersIndexRoute , UserRoute , PostRoute , FilesRoute , AboutRoute , TeamRoute , ProfileRoute , GreedyRoute , CatchAllRoute
10
+ let routes
11
+ let
12
+ RootRoute , UsersRoute , UsersIndexRoute , UserRoute , PostRoute , FilesRoute ,
13
+ AboutRoute , TeamRoute , ProfileRoute , GreedyRoute , OptionalRoute ,
14
+ OptionalRouteChild , CatchAllRoute
11
15
let createLocation = createMemoryHistory ( ) . createLocation
16
+
12
17
beforeEach ( function ( ) {
13
18
/*
14
19
<Route>
15
20
<Route path="users">
16
- <Index />
21
+ <IndexRoute />
17
22
<Route path=":userID">
18
23
<Route path="/profile" />
19
24
</Route>
20
25
<Route path="/team" />
21
26
</Route>
22
27
</Route>
23
28
<Route path="/about" />
29
+ <Route path="/(optional)">
30
+ <Route path="child" />
31
+ </Route>
24
32
<Route path="*" />
25
33
*/
26
34
routes = [
@@ -57,6 +65,14 @@ describe('matchRoutes', function () {
57
65
GreedyRoute = {
58
66
path : '/**/f'
59
67
} ,
68
+ OptionalRoute = {
69
+ path : '/(optional)' ,
70
+ childRoutes : [
71
+ OptionalRouteChild = {
72
+ path : 'child'
73
+ }
74
+ ]
75
+ } ,
60
76
CatchAllRoute = {
61
77
path : '*'
62
78
}
@@ -149,6 +165,42 @@ describe('matchRoutes', function () {
149
165
} )
150
166
} )
151
167
168
+ describe ( 'when the location matches an optional route' , function ( ) {
169
+ it ( 'matches when the optional pattern is missing' , function ( done ) {
170
+ matchRoutes ( routes , createLocation ( '/' ) , function ( error , match ) {
171
+ expect ( match ) . toExist ( )
172
+ expect ( match . routes ) . toEqual ( [ OptionalRoute ] )
173
+ done ( )
174
+ } )
175
+ } )
176
+
177
+ it ( 'matches when the optional pattern is present' , function ( done ) {
178
+ matchRoutes ( routes , createLocation ( '/optional' ) , function ( error , match ) {
179
+ expect ( match ) . toExist ( )
180
+ expect ( match . routes ) . toEqual ( [ OptionalRoute ] )
181
+ done ( )
182
+ } )
183
+ } )
184
+ } )
185
+
186
+ describe ( 'when the location matches the child of an optional route' , function ( ) {
187
+ it ( 'matches when the optional pattern is missing' , function ( done ) {
188
+ matchRoutes ( routes , createLocation ( '/child' ) , function ( error , match ) {
189
+ expect ( match ) . toExist ( )
190
+ expect ( match . routes ) . toEqual ( [ OptionalRoute , OptionalRouteChild ] )
191
+ done ( )
192
+ } )
193
+ } )
194
+
195
+ it ( 'matches when the optional pattern is present' , function ( done ) {
196
+ matchRoutes ( routes , createLocation ( '/optional/child' ) , function ( error , match ) {
197
+ expect ( match ) . toExist ( )
198
+ expect ( match . routes ) . toEqual ( [ OptionalRoute , OptionalRouteChild ] )
199
+ done ( )
200
+ } )
201
+ } )
202
+ } )
203
+
152
204
describe ( 'when the location does not match any routes' , function ( ) {
153
205
it ( 'matches the "catch-all" route' , function ( done ) {
154
206
matchRoutes ( routes , createLocation ( '/not-found' ) , function ( error , match ) {
@@ -157,6 +209,22 @@ describe('matchRoutes', function () {
157
209
done ( )
158
210
} )
159
211
} )
212
+
213
+ it ( 'matches the "catch-all" route on a deep miss' , function ( done ) {
214
+ matchRoutes ( routes , createLocation ( '/not-found/foo' ) , function ( error , match ) {
215
+ expect ( match ) . toExist ( )
216
+ expect ( match . routes ) . toEqual ( [ CatchAllRoute ] )
217
+ done ( )
218
+ } )
219
+ } )
220
+
221
+ it ( 'matches the "catch-all" route on missing path separators' , function ( done ) {
222
+ matchRoutes ( routes , createLocation ( '/optionalchild' ) , function ( error , match ) {
223
+ expect ( match ) . toExist ( )
224
+ expect ( match . routes ) . toEqual ( [ CatchAllRoute ] )
225
+ done ( )
226
+ } )
227
+ } )
160
228
} )
161
229
}
162
230
0 commit comments