]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(router): throw NavigationCancelled when it should
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 11 Jun 2019 15:17:50 +0000 (17:17 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 11 Jun 2019 15:17:50 +0000 (17:17 +0200)
__tests__/router.spec.js
src/router.ts

index da7f2256048c711329e6e301689ecbca22a22bf4..02832445ea4e3b5a82e6062862267377bb5f8e87 100644 (file)
@@ -5,6 +5,7 @@ const fakePromise = require('faked-promise')
 const { HTML5History } = require('../src/history/html5')
 const { AbstractHistory } = require('../src/history/abstract')
 const { Router } = require('../src/router')
+const { NavigationCancelled } = require('../src/errors')
 const { createDom, components, tick } = require('./utils')
 
 function mockHistory() {
@@ -77,7 +78,7 @@ describe('Router', () => {
   })
 
   describe('navigation', () => {
-    async function checkNavigationCancelledOnPush(target, expectation) {
+    async function checkNavigationCancelledOnPush(target) {
       const [p1, r1] = fakePromise()
       const [p2, r2] = fakePromise()
       const history = mockHistory()
@@ -99,26 +100,26 @@ describe('Router', () => {
       // the second one will have already been resolved
       r2()
       await pB
-      expect(router.currentRoute.fullPath).toBe(expectation)
+      expect(router.currentRoute.fullPath).toBe('/p/b')
       r1()
       try {
         await pA
       } catch (err) {
-        // TODO: expect error
+        expect(err).toBeInstanceOf(NavigationCancelled)
       }
-      expect(router.currentRoute.fullPath).toBe(expectation)
+      expect(router.currentRoute.fullPath).toBe('/p/b')
     }
 
     it('cancels navigation abort if a newer one is finished on push', async () => {
-      await checkNavigationCancelledOnPush(false, '/p/b')
+      await checkNavigationCancelledOnPush(false)
     })
 
     it('cancels pending in-guard navigations if a newer one is finished on push', async () => {
-      await checkNavigationCancelledOnPush('/foo', '/p/b')
+      await checkNavigationCancelledOnPush('/foo')
     })
 
     it('cancels pending navigations if a newer one is finished on push', async () => {
-      await checkNavigationCancelledOnPush(undefined, '/p/b')
+      await checkNavigationCancelledOnPush(undefined)
     })
 
     async function checkNavigationCancelledOnPopstate(target) {
index f04fadc520e1112951229f310e618e9865fe047d..d603553a1ef68527a4e555bbd2ac2fcbd0253c45 100644 (file)
@@ -236,6 +236,11 @@ export class Router {
       } else {
         // TODO: write tests
         // triggerError as well
+        if (this.pendingLocation !== toLocation) {
+          // TODO: trigger onError as well
+          throw new NavigationCancelled(toLocation, this.currentRoute)
+        }
+
         throw error
       }
     }