]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: fix beforeEach tests
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 May 2019 10:05:15 +0000 (12:05 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 May 2019 10:05:15 +0000 (12:05 +0200)
__tests__/guards/global-beforeEach.spec.js

index 83da878db858bea99dfd46debee7ee886827db30..a22c5893f3b7888be04b648ed0d96e5c50ea742c 100644 (file)
@@ -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')