]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: test errors in matcher
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 16 Apr 2019 16:04:34 +0000 (18:04 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 16 Apr 2019 16:04:34 +0000 (18:04 +0200)
__tests__/matcher.spec.js
src/errors.ts

index 19bfa9a5fb1cadf80b9d6c3458f019d3b265693d..dbd0b49d7413989e9d2162b1fb390eb7514a8209 100644 (file)
@@ -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":"/"}]`
+        )
+      })
     })
   })
 })
index b3b096f209328a2ec0ac1e50807b9c5410c2fcb5..ef6d4b9103b6410831d07da0da113c764e2eea3a 100644 (file)
@@ -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)
   }
 }