From: Eduardo San Martin Morote Date: Thu, 2 May 2019 10:05:15 +0000 (+0200) Subject: test: fix beforeEach tests X-Git-Tag: v4.0.0-alpha.0~416 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09cb5f5eccdb7ab0c13f46a38a9ae5b0419c696f;p=thirdparty%2Fvuejs%2Frouter.git test: fix beforeEach tests --- diff --git a/__tests__/guards/global-beforeEach.spec.js b/__tests__/guards/global-beforeEach.spec.js index 83da878d..a22c5893 100644 --- a/__tests__/guards/global-beforeEach.spec.js +++ b/__tests__/guards/global-beforeEach.spec.js @@ -32,11 +32,14 @@ describe('router.beforeEach', () => { NAVIGATION_TYPES.forEach(navigationMethod => { describe(navigationMethod, () => { - it('calls beforeEach guards on navigation', () => { + it('calls beforeEach guards on navigation', async () => { const spy = jest.fn() const router = createRouter({ routes }) router.beforeEach(spy) - router[navigationMethod]('/foo') + spy.mockImplementationOnce((to, from, next) => { + next() + }) + await router[navigationMethod]('/foo') expect(spy).toHaveBeenCalledTimes(1) }) @@ -60,27 +63,31 @@ describe('router.beforeEach', () => { const [p1, r1] = fakePromise() const [p2, r2] = fakePromise() const router = createRouter({ routes }) - const guard1 = jest.fn(async (to, from, next) => { + const guard1 = jest.fn() + let order = 0 + guard1.mockImplementationOnce(async (to, from, next) => { + expect(order++).toBe(0) await p1 next() }) router.beforeEach(guard1) - const guard2 = jest.fn(async (to, from, next) => { + const guard2 = jest.fn() + guard2.mockImplementationOnce(async (to, from, next) => { + expect(order++).toBe(1) await p2 next() }) router.beforeEach(guard2) let navigation = router[navigationMethod]('/foo') expect(router.currentRoute.fullPath).toBe('/') - expect(guard1).toHaveBeenCalled() + expect(guard1).not.toHaveBeenCalled() expect(guard2).not.toHaveBeenCalled() - r1() - // wait until the guard is called - await tick() - await tick() - expect(guard2).toHaveBeenCalled() - r2() + r1() // resolve the first guard + await tick() // wait a tick + expect(guard1).toHaveBeenCalled() + // we haven't resolved the second gurad yet expect(router.currentRoute.fullPath).toBe('/') + r2() await navigation expect(guard2).toHaveBeenCalled() expect(router.currentRoute.fullPath).toBe('/foo')