Skip to content

Commit e224637

Browse files
authored
fix: removes warning resolving asterisk routes
* fix: removes warning resolving asterisk routes Closes #2505 * refactor: add test for #2505 * test: fix bad route name in test * fix(flow): enuser params is an object
1 parent 32ad6da commit e224637

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/util/params.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ export function fillParams (
1313
params: ?Object,
1414
routeMsg: string
1515
): string {
16+
params = params || {}
1617
try {
1718
const filler =
1819
regexpCompileCache[path] ||
1920
(regexpCompileCache[path] = Regexp.compile(path))
20-
return filler(params || {}, { pretty: true })
21+
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 })
2126
} catch (e) {
2227
if (process.env.NODE_ENV !== 'production') {
2328
warn(false, `missing param for ${routeMsg}: ${e.message}`)
2429
}
2530
return ''
31+
} finally {
32+
// delete the 0 if it was added
33+
delete params[0]
2634
}
2735
}

test/unit/specs/create-matcher.spec.js

+39-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ 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: '/error/*', name: 'error', component: { name: 'error' }},
9+
{ path: '*', props: true, name: 'notFound', component: { name: 'notFound' }}
810
]
911

1012
describe('Creating Matcher', function () {
@@ -26,14 +28,49 @@ describe('Creating Matcher', function () {
2628
expect(matched.length).toBe(0)
2729
expect(name).toBe('bar')
2830
expect(console.warn).toHaveBeenCalled()
29-
expect(console.warn.calls.argsFor(0)[0]).toMatch('Route with name \'bar\' does not exist')
31+
expect(console.warn.calls.argsFor(0)[0]).toMatch(
32+
"Route with name 'bar' does not exist"
33+
)
3034
})
3135

3236
it('in production, it has not logged this warning', function () {
3337
match({ name: 'foo' }, routes[0])
3438
expect(console.warn).not.toHaveBeenCalled()
3539
})
3640

41+
it('matches named route with params without warning', function () {
42+
process.env.NODE_ENV = 'development'
43+
const { name, path, params } = match({
44+
name: 'baz',
45+
params: { testparam: 'testvalue' }
46+
})
47+
expect(console.warn).not.toHaveBeenCalled()
48+
expect(name).toEqual('baz')
49+
expect(path).toEqual('/baz/testvalue')
50+
expect(params).toEqual({ testparam: 'testvalue' })
51+
})
52+
53+
it('matches asterisk routes with a default param name without warning', function () {
54+
process.env.NODE_ENV = 'development'
55+
const { params } = match(
56+
{ name: 'notFound', params: { pathMatch: '/not-found' }},
57+
routes[0]
58+
)
59+
expect(console.warn).not.toHaveBeenCalled()
60+
expect(params).toEqual({ pathMatch: '/not-found' })
61+
})
62+
63+
it('matches partial asterisk routes with a default param name without warning', function () {
64+
process.env.NODE_ENV = 'development'
65+
const { params, path } = match(
66+
{ name: 'error', params: { pathMatch: 'some' }},
67+
routes[0]
68+
)
69+
expect(console.warn).not.toHaveBeenCalled()
70+
expect(params).toEqual({ pathMatch: 'some' })
71+
expect(path).toEqual('/error/some')
72+
})
73+
3774
it('matches asterisk routes with a default param name', function () {
3875
const { params } = match({ path: '/not-found' }, routes[0])
3976
expect(params).toEqual({ pathMatch: '/not-found' })

0 commit comments

Comments
 (0)