From e224637bddf25c98d541e9e62f10c31ba11c8bcf Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 23 Dec 2018 13:51:28 +0100 Subject: [PATCH] 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 --- src/util/params.js | 10 ++++++- test/unit/specs/create-matcher.spec.js | 41 ++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/util/params.js b/src/util/params.js index 4daf13cec..cac6a683d 100644 --- a/src/util/params.js +++ b/src/util/params.js @@ -13,15 +13,23 @@ export function fillParams ( params: ?Object, routeMsg: string ): string { + params = params || {} try { const filler = regexpCompileCache[path] || (regexpCompileCache[path] = Regexp.compile(path)) - return filler(params || {}, { pretty: true }) + + // Fix #2505 resolving asterisk routes { name: 'not-found', params: { pathMatch: '/not-found' }} + if (params.pathMatch) params[0] = params.pathMatch + + return filler(params, { pretty: true }) } catch (e) { if (process.env.NODE_ENV !== 'production') { warn(false, `missing param for ${routeMsg}: ${e.message}`) } return '' + } finally { + // delete the 0 if it was added + delete params[0] } } diff --git a/test/unit/specs/create-matcher.spec.js b/test/unit/specs/create-matcher.spec.js index 5028957e5..88fb475a1 100644 --- a/test/unit/specs/create-matcher.spec.js +++ b/test/unit/specs/create-matcher.spec.js @@ -4,7 +4,9 @@ import { createMatcher } from '../../../src/create-matcher' const routes = [ { path: '/', name: 'home', component: { name: 'home' }}, { path: '/foo', name: 'foo', component: { name: 'foo' }}, - { path: '*', props: true, component: { name: 'notFound' }} + { path: '/baz/:testparam', name: 'baz', component: { name: 'baz' }}, + { path: '/error/*', name: 'error', component: { name: 'error' }}, + { path: '*', props: true, name: 'notFound', component: { name: 'notFound' }} ] describe('Creating Matcher', function () { @@ -26,7 +28,9 @@ describe('Creating Matcher', function () { expect(matched.length).toBe(0) expect(name).toBe('bar') expect(console.warn).toHaveBeenCalled() - expect(console.warn.calls.argsFor(0)[0]).toMatch('Route with name \'bar\' does not exist') + expect(console.warn.calls.argsFor(0)[0]).toMatch( + "Route with name 'bar' does not exist" + ) }) it('in production, it has not logged this warning', function () { @@ -34,6 +38,39 @@ describe('Creating Matcher', function () { expect(console.warn).not.toHaveBeenCalled() }) + it('matches named route with params without warning', function () { + process.env.NODE_ENV = 'development' + const { name, path, params } = match({ + name: 'baz', + params: { testparam: 'testvalue' } + }) + expect(console.warn).not.toHaveBeenCalled() + expect(name).toEqual('baz') + expect(path).toEqual('/baz/testvalue') + expect(params).toEqual({ testparam: 'testvalue' }) + }) + + it('matches asterisk routes with a default param name without warning', function () { + process.env.NODE_ENV = 'development' + const { params } = match( + { name: 'notFound', params: { pathMatch: '/not-found' }}, + routes[0] + ) + expect(console.warn).not.toHaveBeenCalled() + expect(params).toEqual({ pathMatch: '/not-found' }) + }) + + it('matches partial asterisk routes with a default param name without warning', function () { + process.env.NODE_ENV = 'development' + const { params, path } = match( + { name: 'error', params: { pathMatch: 'some' }}, + routes[0] + ) + expect(console.warn).not.toHaveBeenCalled() + expect(params).toEqual({ pathMatch: 'some' }) + expect(path).toEqual('/error/some') + }) + it('matches asterisk routes with a default param name', function () { const { params } = match({ path: '/not-found' }, routes[0]) expect(params).toEqual({ pathMatch: '/not-found' })