]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor(types): improve matcher.spec
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 1 Jul 2019 16:20:08 +0000 (18:20 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 1 Jul 2019 16:20:08 +0000 (18:20 +0200)
__tests__/matcher.spec.js
src/history/html5.ts
src/types/index.ts

index 7dae8e822fab31b5f0bcbff2c176cc98c605522e..3fb513592990310ff3eaf2560bb927cc0a2f65c8 100644 (file)
@@ -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<MatcherLocationNormalized & { component: any }>} resolved Expected resolved location given by the matcher
-     * @param {MatcherLocationNormalized | (MatcherLocationNormalized & { matched: Array<MatchedRouteRecord | Exclude<RouteRecord, { redirect: any}>> })} [start] Optional currentLocation used when resolving
+     * @param {Partial<import('../src/types').Override<MatcherLocationNormalized, { matched: Array<Exclude<RouteRecord, { redirect: any}>> }>>} resolved Expected resolved location given by the matcher
+     * @param {import('../src/types').Override<MatcherLocationNormalized, { matched: Array<Exclude<RouteRecord, { redirect: any}>> }>} [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),
+            ],
           }
         )
       })
index 4f5ffce73b5acbca3b0edd555a1808115f29d444..6933bad6624e425e2570e4e6f214cafd41027aff 100644 (file)
@@ -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
index 1f74f46e7bdc1f0163d08997b2ebe3eab2bd082c..0193e990cdfc1530c620c65e2364b27fce84824e 100644 (file)
@@ -4,6 +4,7 @@ import { HistoryQuery, RawHistoryQuery } from '../history/base'
 // type Component = ComponentOptions<Vue> | typeof Vue | AsyncComponent
 
 export type Lazy<T> = () => Promise<T>
+export type Override<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
 
 export type TODO = any