Skip to content

Commit

Permalink
Fix exact routes, support routes without patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish committed Dec 19, 2016
1 parent 34271b8 commit 7b81783
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
35 changes: 23 additions & 12 deletions modules/__tests__/matchRoutesToLocation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import matchRoutesToLocation from '../matchRoutesToLocation'

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

test('extracts matching params', () => {
Expand All @@ -43,9 +46,17 @@ test('extracts matching params', () => {
expect(params).toEqual({ bar: 'bar', baz: 'baz' })
})

test('respects a nested "exactly" route', () => {
const location = { pathname: '/qux/corge' }
test('respects "exactly" routes', () => {
const location = { pathname: '/' }
const { matchedRoutes } = matchRoutesToLocation(routes, location)
expect(matchedRoutes.length).toEqual(2)
expect(matchedRoutes[0]).toEqual(routes[0])
expect(matchedRoutes[1]).toEqual(routes[0].routes[0])
})

test('ignores unreachable routes nested within "exactly" routes', () => {
const location = { pathname: '/qux/unreachable' }
const { matchedRoutes } = matchRoutesToLocation(routes, location)
expect(matchedRoutes.length).toEqual(1)
expect(matchedRoutes[0]).toEqual(routes[0].routes[1].routes[0])
expect(matchedRoutes[0]).toEqual(routes[0])
})
11 changes: 6 additions & 5 deletions modules/matchRoutesToLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ const mergePatterns = (a, b) => {

const matchRoutesToLocation = (routes, location, matchedRoutes=[], params={}, parentPattern='') => {
routes.forEach((route) => {
const nestedPattern = mergePatterns(parentPattern, route.pattern)
const match = matchPattern(nestedPattern, location)
const { exactly = false } = route

const nestedPattern = mergePatterns(parentPattern, route.pattern || '')
const match = !route.pattern ? true : matchPattern(nestedPattern, location, exactly)

if (match) {
if (route.exactly ? match.isExact : true) {
matchedRoutes.push(route)
}
matchedRoutes.push(route)

if (match.params) {
Object.keys(match.params).forEach(key => params[key] = match.params[key])
}
Expand Down

0 comments on commit 7b81783

Please sign in to comment.