From: Eduardo San Martin Morote Date: Mon, 1 Jul 2019 16:20:08 +0000 (+0200) Subject: refactor(types): improve matcher.spec X-Git-Tag: v4.0.0-alpha.0~319 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d53a8438fbfe9e8a0f36733595359f7299018cab;p=thirdparty%2Fvuejs%2Frouter.git refactor(types): improve matcher.spec --- diff --git a/__tests__/matcher.spec.js b/__tests__/matcher.spec.js index 7dae8e82..3fb51359 100644 --- a/__tests__/matcher.spec.js +++ b/__tests__/matcher.spec.js @@ -5,6 +5,7 @@ const { RouterMatcher } = require('../src/matcher') const { START_LOCATION_NORMALIZED } = require('../src/types') const { normalizeRouteRecord } = require('./utils') +/** @type {RouteComponent} */ const component = null /** @typedef {import('../src/types').RouteRecord} RouteRecord */ @@ -20,8 +21,8 @@ describe('Router Matcher', () => { * * @param {RouteRecord | RouteRecord[]} record Record or records we are testing the matcher against * @param {MatcherLocation} location location we want to reolve against - * @param {Partial} resolved Expected resolved location given by the matcher - * @param {MatcherLocationNormalized | (MatcherLocationNormalized & { matched: Array> })} [start] Optional currentLocation used when resolving + * @param {Partial> }>>} resolved Expected resolved location given by the matcher + * @param {import('../src/types').Override> }>} [start] Optional currentLocation used when resolving */ function assertRecordMatch( record, @@ -39,10 +40,12 @@ describe('Router Matcher', () => { } if ('redirect' in record) { + throw new Error('not handled') } else { // use one single record if (!('matched' in resolved)) resolved.matched = record.map(normalizeRouteRecord) + else resolved.matched = resolved.matched.map(normalizeRouteRecord) } // allows not passing params @@ -52,8 +55,6 @@ describe('Router Matcher', () => { resolved.params = resolved.params || {} } - if (!('matched' in resolved)) resolved.matched = [] - const startCopy = { ...start, matched: start.matched.map(normalizeRouteRecord), @@ -179,7 +180,7 @@ describe('Router Matcher', () => { name: 'Home', params: {}, path: '/home', - matched: [normalizeRouteRecord(record)], + matched: [record], } ) }) @@ -194,7 +195,7 @@ describe('Router Matcher', () => { path: '/users/ed/m/user', name: undefined, params: { id: 'ed', role: 'user' }, - matched: [record].map(normalizeRouteRecord), + matched: [record], } ) }) @@ -236,7 +237,7 @@ describe('Router Matcher', () => { path: '/users/ed/m/user', name: 'UserEdit', params: { id: 'ed', role: 'user' }, - matched: [record].map(normalizeRouteRecord), + matched: [record], } ) }) @@ -255,7 +256,7 @@ describe('Router Matcher', () => { path: '/users/ed/m/user', name: undefined, params: { id: 'ed', role: 'user' }, - matched: [record].map(normalizeRouteRecord), + matched: [record], } ) }) @@ -454,11 +455,17 @@ describe('Router Matcher', () => { name: 'home', params: {}, path: '/', - matched: [record].map(normalizeRouteRecord), + matched: [record], } // the property should be non enumerable Object.defineProperty(start, 'matched', { enumerable: false }) - expect(assertErrorMatch(record, {}, start)).toMatchInlineSnapshot( + expect( + assertErrorMatch( + record, + {}, + { ...start, matched: start.matched.map(normalizeRouteRecord) } + ) + ).toMatchInlineSnapshot( `[Error: No match for {"name":"home","params":{},"path":"/"}]` ) }) @@ -503,10 +510,7 @@ describe('Router Matcher', () => { name: 'child-b', path: '/foo/b', params: {}, - matched: [ - Foo, - { ...ChildB, path: `${Foo.path}/${ChildB.path}` }, - ].map(normalizeRouteRecord), + matched: [Foo, { ...ChildB, path: `${Foo.path}/${ChildB.path}` }], } ) }) @@ -558,7 +562,7 @@ describe('Router Matcher', () => { Foo, { ...Nested, path: `${Foo.path}` }, { ...NestedNested, path: `${Foo.path}` }, - ].map(normalizeRouteRecord), + ], } ) }) @@ -584,7 +588,7 @@ describe('Router Matcher', () => { ...NestedChildA, path: `${Foo.path}/${Nested.path}/${NestedChildA.path}`, }, - ].map(normalizeRouteRecord), + ], } ) }) @@ -610,7 +614,7 @@ describe('Router Matcher', () => { ...NestedChildA, path: `${Foo.path}/${Nested.path}/${NestedChildA.path}`, }, - ].map(normalizeRouteRecord), + ], } ) }) @@ -636,7 +640,7 @@ describe('Router Matcher', () => { ...NestedChildA, path: `${Foo.path}/${Nested.path}/${NestedChildA.path}`, }, - ].map(normalizeRouteRecord), + ], }, { name: 'nested-child-a', @@ -671,7 +675,7 @@ describe('Router Matcher', () => { ...NestedChildWithParam, path: `${Foo.path}/${NestedWithParam.path}/${NestedChildWithParam.path}`, }, - ].map(normalizeRouteRecord), + ], } ) }) @@ -700,7 +704,7 @@ describe('Router Matcher', () => { ...NestedChildWithParam, path: `${Foo.path}/${NestedWithParam.path}/${NestedChildWithParam.path}`, }, - ].map(normalizeRouteRecord), + ], } ) }) diff --git a/src/history/html5.ts b/src/history/html5.ts index 4f5ffce7..6933bad6 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -6,6 +6,7 @@ const cs = consola.withTag('html5') // TODO: implement the mock instead /* istanbul ignore next */ +// @ts-ignore otherwise fails after rollup replacement plugin if (process.env.NODE_ENV === 'test') cs.mockTypes(() => jest.fn()) type PopStateListener = (this: Window, ev: PopStateEvent) => any diff --git a/src/types/index.ts b/src/types/index.ts index 1f74f46e..0193e990 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -4,6 +4,7 @@ import { HistoryQuery, RawHistoryQuery } from '../history/base' // type Component = ComponentOptions | typeof Vue | AsyncComponent export type Lazy = () => Promise +export type Override = Pick> & U export type TODO = any