Skip to content

Commit 37326ca

Browse files
Stanislav Karpovposva
Stanislav Karpov
authored andcommitted
Add warning unknown route (#1022)
* Show warning message if route does exist * Remove semicolon * Add test to warning of not existing route * Use text interpolation * fix no-space-before-comma eslint error * Fix typo
1 parent 1c3942f commit 37326ca

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/create-matcher.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
2020

2121
if (name) {
2222
const record = nameMap[name]
23+
if (process.env.NODE_ENV !== 'production') {
24+
warn(record, `Route with name '${name}' does not exist`)
25+
}
2326
const paramNames = getRouteRegex(record.path).keys
2427
.filter(key => !key.optional)
2528
.map(key => key.name)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*eslint-disable no-undef*/
2+
import { createMatcher } from '../../../src/create-matcher'
3+
4+
const routes = [
5+
{ path: '/', name: 'home', component: { name: 'home' } },
6+
{ path: '/foo', name: 'foo', component: { name: 'foo' } },
7+
]
8+
9+
describe('Creating Matcher', function () {
10+
beforeAll(function () {
11+
spyOn(console, 'warn')
12+
this.match = createMatcher(routes)
13+
})
14+
beforeEach(function () {
15+
console.warn.calls.reset()
16+
process.env.NODE_ENV = 'production'
17+
})
18+
19+
it('in development, has logged a warning if a named route does not exist', function () {
20+
process.env.NODE_ENV = 'development'
21+
expect(() => {
22+
this.match({ name: 'bar' }, routes[0]);
23+
}).toThrow(new TypeError('Cannot read property \'path\' of undefined'));
24+
expect(console.warn).toHaveBeenCalled()
25+
expect(console.warn.calls.argsFor(0)[0]).toMatch('Route with name \'bar\' does not exist');
26+
})
27+
it('in production, it has not logged this warning', function () {
28+
this.match({ name: 'foo' }, routes[0]);
29+
expect(console.warn).not.toHaveBeenCalled()
30+
})
31+
})

0 commit comments

Comments
 (0)