Skip to content

Commit ed04db8

Browse files
posvayyx990803
authored andcommitted
Prevent unnecessary Exception with non-existent links (#1387)
* Prevent unecessary Exception with non-existent links Closes #1386 * Remove semis * Keep same behaviour in dev and pro with non-exsistent links
1 parent f5db5b8 commit ed04db8

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/create-matcher.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function createMatcher (
3636
if (process.env.NODE_ENV !== 'production') {
3737
warn(record, `Route with name '${name}' does not exist`)
3838
}
39+
if (!record) return _createRoute(null, location)
3940
const paramNames = record.regex.keys
4041
.filter(key => !key.optional)
4142
.map(key => key.name)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import { createMatcher } from '../../../src/create-matcher'
33

44
const routes = [
5-
{ path: '/', name: 'home', component: { name: 'home' } },
6-
{ path: '/foo', name: 'foo', component: { name: 'foo' } },
5+
{ path: '/', name: 'home', component: { name: 'home' }},
6+
{ path: '/foo', name: 'foo', component: { name: 'foo' }},
77
]
88

99
describe('Creating Matcher', function () {
@@ -21,15 +21,15 @@ describe('Creating Matcher', function () {
2121

2222
it('in development, has logged a warning if a named route does not exist', function () {
2323
process.env.NODE_ENV = 'development'
24-
expect(() => {
25-
match({ name: 'bar' }, routes[0]);
26-
}).toThrow(new TypeError('Cannot read property \'regex\' of undefined'));
24+
const { name, matched } = match({ name: 'bar' }, routes[0])
25+
expect(matched.length).toBe(0)
26+
expect(name).toBe('bar')
2727
expect(console.warn).toHaveBeenCalled()
28-
expect(console.warn.calls.argsFor(0)[0]).toMatch('Route with name \'bar\' does not exist');
28+
expect(console.warn.calls.argsFor(0)[0]).toMatch('Route with name \'bar\' does not exist')
2929
})
3030

3131
it('in production, it has not logged this warning', function () {
32-
match({ name: 'foo' }, routes[0]);
32+
match({ name: 'foo' }, routes[0])
3333
expect(console.warn).not.toHaveBeenCalled()
3434
})
3535
})

0 commit comments

Comments
 (0)