From: Eduardo San Martin Morote Date: Tue, 16 Apr 2019 16:04:34 +0000 (+0200) Subject: test: test errors in matcher X-Git-Tag: v4.0.0-alpha.0~438 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4eb990bf7773ae75e42baeb9e714928fab21f1d5;p=thirdparty%2Fvuejs%2Frouter.git test: test errors in matcher --- diff --git a/__tests__/matcher.spec.js b/__tests__/matcher.spec.js index 19bfa9a5..dbd0b49d 100644 --- a/__tests__/matcher.spec.js +++ b/__tests__/matcher.spec.js @@ -47,6 +47,25 @@ describe('Router Matcher', () => { expect(result).toEqual(resolved) } + /** + * + * @param {import('../src/types').RouteRecord} record + * @param {import('../src/types').MatcherLocation} location + * @param {import('../src/types').MatcherLocationNormalized} start + * @returns {any} error + */ + function assertErrorMatch( + record, + location, + start = START_LOCATION_NORMALIZED + ) { + try { + assertRecordMatch(record, location, {}, start) + } catch (error) { + return error + } + } + describe('LocationAsPath', () => { it('resolves a normal path', () => { assertRecordMatch( @@ -87,6 +106,14 @@ describe('Router Matcher', () => { { name: undefined, params: { id: 'posva', other: 'hey' } } ) }) + + it('throws if the path does not exists', () => { + expect( + assertErrorMatch({ path: '/', component }, { path: '/foo' }) + ).toMatchInlineSnapshot( + `[Error: No match for {"path":"/foo","params":{},"query":{},"hash":"","fullPath":"/"}]` + ) + }) }) describe('LocationAsName', () => { @@ -105,6 +132,14 @@ describe('Router Matcher', () => { { name: 'UserEdit', path: '/users/posva/m/admin' } ) }) + + it('throws if the named route does not exists', () => { + expect( + assertErrorMatch({ path: '/', component }, { name: 'Home' }) + ).toMatchInlineSnapshot( + `[Error: No match for {"path":"/","name":"Home","params":{},"query":{},"hash":"","fullPath":"/"}]` + ) + }) }) describe('LocationAsRelative', () => { @@ -176,6 +211,18 @@ describe('Router Matcher', () => { } ) }) + + it('throws if the current named route does not exists', () => { + expect( + assertErrorMatch( + { path: '/', component }, + {}, + { name: 'home', params: {}, path: '/' } + ) + ).toMatchInlineSnapshot( + `[Error: No match for {"name":"home","params":{},"path":"/"}]` + ) + }) }) }) }) diff --git a/src/errors.ts b/src/errors.ts index b3b096f2..ef6d4b91 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,6 +1,6 @@ export class NoRouteMatchError extends Error { constructor(currentLocation: any, location: any) { - super('No match for' + JSON.stringify({ ...currentLocation, ...location })) + super('No match for ' + JSON.stringify({ ...currentLocation, ...location })) Object.setPrototypeOf(this, new.target.prototype) } }