Skip to content

Commit

Permalink
Update matchRoutesToLocation to respect "exactly" flags set on routes…
Browse files Browse the repository at this point in the history
… (#10)
  • Loading branch information
ctrlplusb authored and markdalgleish committed Dec 5, 2016
1 parent 8d24091 commit 31fde91
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 17 additions & 4 deletions modules/__tests__/matchRoutesToLocation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import matchRoutesToLocation from '../matchRoutesToLocation'

const routes = [
{ pattern: '/',
exactly: true,
name: 'root',
routes: [
{ pattern: '/foo',
Expand All @@ -17,6 +18,12 @@ const routes = [
]
}
]
}, {
pattern: '/qux',
exactly: true,
routes: [
{ pattern: '/corge' }
]
}
]
}
Expand All @@ -25,14 +32,20 @@ const routes = [
test('finds matched routes', () => {
const location = { pathname: '/foo/bar' }
const { matchedRoutes } = matchRoutesToLocation(routes, location)
expect(matchedRoutes.length).toEqual(3)
expect(matchedRoutes[0]).toEqual(routes[0])
expect(matchedRoutes[1]).toEqual(routes[0].routes[0])
expect(matchedRoutes[2]).toEqual(routes[0].routes[0].routes[0])
expect(matchedRoutes.length).toEqual(2)
expect(matchedRoutes[0]).toEqual(routes[0].routes[0])
expect(matchedRoutes[1]).toEqual(routes[0].routes[0].routes[0])
})

test('extracts matching params', () => {
const location = { pathname: '/foo/bar/baz' }
const { params } = matchRoutesToLocation(routes, location)
expect(params).toEqual({ bar: 'bar', baz: 'baz' })
})

test('respects a nested "exactly" route', () => {
const location = { pathname: '/qux/corge' }
const { matchedRoutes } = matchRoutesToLocation(routes, location)
expect(matchedRoutes.length).toEqual(1)
expect(matchedRoutes[0]).toEqual(routes[0].routes[1].routes[0])
})
5 changes: 3 additions & 2 deletions modules/matchRoutesToLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const matchRoutesToLocation = (routes, location, matchedRoutes=[], params={}, pa
const match = matchPattern(nestedPattern, location)

if (match) {
matchedRoutes.push(route)

if (route.exactly ? match.isExact : true) {
matchedRoutes.push(route)
}
if (match.params) {
Object.keys(match.params).forEach(key => params[key] = match.params[key])
}
Expand Down

0 comments on commit 31fde91

Please sign in to comment.