Skip to content

Commit 67b46a2

Browse files
committed
refactor: add test for #2505
1 parent bbfb889 commit 67b46a2

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

Diff for: src/util/params.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ export function fillParams (
1717
const filler =
1818
regexpCompileCache[path] ||
1919
(regexpCompileCache[path] = Regexp.compile(path))
20-
if (params && params.pathMatch) {
21-
params[0] = params.pathMatch
22-
}
23-
return filler(params || {}, { pretty: true })
20+
21+
params = params || {}
22+
// Fix #2505 resolving asterisk routes { name: 'not-found', params: { pathMatch: '/not-found' }}
23+
if (params.pathMatch) params[0] = params.pathMatch
24+
25+
return filler(params, { pretty: true })
2426
} catch (e) {
2527
if (process.env.NODE_ENV !== 'production') {
2628
warn(false, `missing param for ${routeMsg}: ${e.message}`)
2729
}
2830
return ''
2931
} finally {
30-
if (params && params[0]) {
31-
delete params[0]
32-
}
32+
// delete the 0 if it was added
33+
delete params[0]
3334
}
3435
}

Diff for: test/unit/specs/create-matcher.spec.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { createMatcher } from '../../../src/create-matcher'
44
const routes = [
55
{ path: '/', name: 'home', component: { name: 'home' }},
66
{ path: '/foo', name: 'foo', component: { name: 'foo' }},
7-
{ path: '*', props: true, component: { name: 'notFound' }}
7+
{ path: '/baz/:testparam', name: 'baz', component: { name: 'baz' }},
8+
{ path: '*', props: true, name: 'notFound', component: { name: 'notFound' }}
89
]
910

1011
describe('Creating Matcher', function () {
@@ -26,14 +27,29 @@ describe('Creating Matcher', function () {
2627
expect(matched.length).toBe(0)
2728
expect(name).toBe('bar')
2829
expect(console.warn).toHaveBeenCalled()
29-
expect(console.warn.calls.argsFor(0)[0]).toMatch('Route with name \'bar\' does not exist')
30+
expect(console.warn.calls.argsFor(0)[0]).toMatch("Route with name 'bar' does not exist")
3031
})
3132

3233
it('in production, it has not logged this warning', function () {
3334
match({ name: 'foo' }, routes[0])
3435
expect(console.warn).not.toHaveBeenCalled()
3536
})
3637

38+
it('matches named route with params without warning', function () {
39+
process.env.NODE_ENV = 'development'
40+
const { name, path } = match({ name: 'baz', params: { testparam: 'testvalue' }})
41+
expect(console.warn).not.toHaveBeenCalled()
42+
expect(name).toEqual('baz')
43+
expect(path).toEqual('/baz/testvalue')
44+
})
45+
46+
it('matches asterisk routes with a default param name without warning', function () {
47+
process.env.NODE_ENV = 'development'
48+
const { params } = match({ name: 'notfound', params: { pathMatch: '/not-found' }}, routes[0])
49+
expect(console.warn).not.toHaveBeenCalled()
50+
expect(params).toEqual({ pathMatch: '/not-found' })
51+
})
52+
3753
it('matches asterisk routes with a default param name', function () {
3854
const { params } = match({ path: '/not-found' }, routes[0])
3955
expect(params).toEqual({ pathMatch: '/not-found' })

0 commit comments

Comments
 (0)