From: Eduardo San Martin Morote Date: Tue, 11 Jun 2019 15:17:50 +0000 (+0200) Subject: fix(router): throw NavigationCancelled when it should X-Git-Tag: v4.0.0-alpha.0~339 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=085b75580bc952aed55048e0a6cb01bdc38c3630;p=thirdparty%2Fvuejs%2Frouter.git fix(router): throw NavigationCancelled when it should --- diff --git a/__tests__/router.spec.js b/__tests__/router.spec.js index da7f2256..02832445 100644 --- a/__tests__/router.spec.js +++ b/__tests__/router.spec.js @@ -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) { diff --git a/src/router.ts b/src/router.ts index f04fadc5..d603553a 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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 } }