From 7b817831cd57c1ea2f152779eb4cfb8b518c5028 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Mon, 19 Dec 2016 16:49:49 +1100 Subject: [PATCH] Fix exact routes, support routes without patterns --- .../__tests__/matchRoutesToLocation-test.js | 35 ++++++++++++------- modules/matchRoutesToLocation.js | 11 +++--- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/modules/__tests__/matchRoutesToLocation-test.js b/modules/__tests__/matchRoutesToLocation-test.js index cd04747..40298f2 100644 --- a/modules/__tests__/matchRoutesToLocation-test.js +++ b/modules/__tests__/matchRoutesToLocation-test.js @@ -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: [ @@ -18,11 +20,11 @@ const routes = [ ] } ] - }, { - pattern: '/qux', + }, + { pattern: '/qux', exactly: true, routes: [ - { pattern: '/corge' } + { pattern: '/unreachable' } ] } ] @@ -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', () => { @@ -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]) }) diff --git a/modules/matchRoutesToLocation.js b/modules/matchRoutesToLocation.js index 8339c25..a052a48 100644 --- a/modules/matchRoutesToLocation.js +++ b/modules/matchRoutesToLocation.js @@ -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]) }