Skip to content

Commit

Permalink
fix(): '/:id' is not supposed to be matched by route
Browse files Browse the repository at this point in the history
* test(): test bug where a :key param is appended to an existing route

* fix(): fix bug where '/:id' is not supposed to be matched by route

When navigating to `/` when `/` and `/:id` is defined it still tries to match
`/:id` while id will be undefined, causing null to be the route matched.
  • Loading branch information
TylorS authored and staltz committed Apr 18, 2016
1 parent 18d963a commit cd7a363
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -82,7 +82,7 @@ function switchPath(sourcePath, routes) {
matchedValue = routes[pattern]
}

const params = matchesWithParams(sourcePath, pattern)
const params = matchesWithParams(sourcePath, pattern).filter(Boolean)

if (params.length > 0 && betterMatch(sourcePath, matchedPath)) {
matchedPath = extractPartial(sourcePath, pattern)
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Expand Up @@ -213,4 +213,14 @@ describe('switchPath corner cases', () => {
expect(path).to.be.equal('/home/123/456')
expect(value).to.be.equal('123:456')
})

it('should not match a :key type route if no params are given', () => {
const {path, value} = switchPath('/', {
'/': 'root',
'/:id': (id) => `$id`
})

expect(path).to.be.equal('/')
expect(value).to.be.equal('root')
})
});

0 comments on commit cd7a363

Please sign in to comment.