Skip to content

Commit c8c522f

Browse files
committed
Add more route matching tests
1 parent b57580e commit c8c522f

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

modules/__tests__/matchRoutes-test.js

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,28 @@ import Route from '../Route'
77

88
describe('matchRoutes', function () {
99

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
1115
let createLocation = createMemoryHistory().createLocation
16+
1217
beforeEach(function () {
1318
/*
1419
<Route>
1520
<Route path="users">
16-
<Index />
21+
<IndexRoute />
1722
<Route path=":userID">
1823
<Route path="/profile" />
1924
</Route>
2025
<Route path="/team" />
2126
</Route>
2227
</Route>
2328
<Route path="/about" />
29+
<Route path="/(optional)">
30+
<Route path="child" />
31+
</Route>
2432
<Route path="*" />
2533
*/
2634
routes = [
@@ -57,6 +65,14 @@ describe('matchRoutes', function () {
5765
GreedyRoute = {
5866
path: '/**/f'
5967
},
68+
OptionalRoute = {
69+
path: '/(optional)',
70+
childRoutes: [
71+
OptionalRouteChild = {
72+
path: 'child'
73+
}
74+
]
75+
},
6076
CatchAllRoute = {
6177
path: '*'
6278
}
@@ -149,6 +165,42 @@ describe('matchRoutes', function () {
149165
})
150166
})
151167

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+
152204
describe('when the location does not match any routes', function () {
153205
it('matches the "catch-all" route', function (done) {
154206
matchRoutes(routes, createLocation('/not-found'), function (error, match) {
@@ -157,6 +209,22 @@ describe('matchRoutes', function () {
157209
done()
158210
})
159211
})
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+
})
160228
})
161229
}
162230

0 commit comments

Comments
 (0)