]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: refactor using noGuard
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 May 2019 20:40:53 +0000 (22:40 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 May 2019 20:40:53 +0000 (22:40 +0200)
__tests__/guards/component-beforeRouteUpdate.spec.js
__tests__/guards/global-beforeEach.spec.js
__tests__/guards/route-beforeEnter.spec.js

index c3c15e37e20090a7a9faa7a4dffa27a00284749d..2ea0dc1f429006d58fb75a638acc5e4fc16bf446 100644 (file)
@@ -4,7 +4,7 @@ const expect = require('expect')
 const { HTML5History } = require('../../src/history/html5')
 const { Router } = require('../../src/router')
 const fakePromise = require('faked-promise')
-const { NAVIGATION_TYPES, createDom } = require('../utils')
+const { NAVIGATION_TYPES, createDom, noGuard } = require('../utils')
 
 /**
  * @param {Partial<import('../../src/router').RouterOptions> & { routes: import('../../src/types').RouteRecord[]}} options
@@ -46,9 +46,7 @@ describe('beforeRouteUpdate', () => {
     describe(navigationMethod, () => {
       it('calls beforeRouteUpdate guards when changing params', async () => {
         const router = createRouter({ routes })
-        beforeRouteUpdate.mockImplementationOnce((to, from, next) => {
-          next()
-        })
+        beforeRouteUpdate.mockImplementationOnce(noGuard)
         await router[navigationMethod]('/guard/valid')
         // not called on initial navigation
         expect(beforeRouteUpdate).not.toHaveBeenCalled()
index fa833071f7a1750617b4ca9241980bcfb7f347fc..af51e4d02fdcb904277a6cdf3247b35c15cf9e18 100644 (file)
@@ -4,7 +4,7 @@ const expect = require('expect')
 const { HTML5History } = require('../../src/history/html5')
 const { Router } = require('../../src/router')
 const fakePromise = require('faked-promise')
-const { NAVIGATION_TYPES, createDom, tick } = require('../utils')
+const { NAVIGATION_TYPES, createDom, tick, noGuard } = require('../utils')
 
 /**
  * @param {Partial<import('../../src/router').RouterOptions> & { routes: import('../../src/types').RouteRecord[]}} options
@@ -36,9 +36,7 @@ describe('router.beforeEach', () => {
         const spy = jest.fn()
         const router = createRouter({ routes })
         router.beforeEach(spy)
-        spy.mockImplementationOnce((to, from, next) => {
-          next()
-        })
+        spy.mockImplementationOnce(noGuard)
         await router[navigationMethod]('/foo')
         expect(spy).toHaveBeenCalledTimes(1)
       })
@@ -48,9 +46,7 @@ describe('router.beforeEach', () => {
         const router = createRouter({ routes })
         await router.push('/foo')
         router.beforeEach(spy)
-        spy.mockImplementationOnce((to, from, next) => {
-          next()
-        })
+        spy.mockImplementationOnce(noGuard)
         await router[navigationMethod]('/foo')
         expect(spy).not.toHaveBeenCalled()
       })
index 5086e6dd45cbf04949059648b978164d786e11bb..76532b0a9a617d79a912defcc3877c6e82dba3e2 100644 (file)
@@ -4,7 +4,7 @@ const expect = require('expect')
 const { HTML5History } = require('../../src/history/html5')
 const { Router } = require('../../src/router')
 const fakePromise = require('faked-promise')
-const { NAVIGATION_TYPES, createDom } = require('../utils')
+const { NAVIGATION_TYPES, createDom, noGuard } = require('../utils')
 
 /**
  * @param {Partial<import('../../src/router').RouterOptions> & { routes: import('../../src/types').RouteRecord[]}} options
@@ -44,19 +44,14 @@ describe('beforeEnter', () => {
     describe(navigationMethod, () => {
       it('calls beforeEnter guards on navigation', async () => {
         const router = createRouter({ routes })
-        beforeEnter.mockImplementationOnce((to, from, next) => {
-          if (to.params.n !== 'valid') return next(false)
-          next()
-        })
+        beforeEnter.mockImplementationOnce(noGuard)
         await router[navigationMethod]('/guard/valid')
         expect(beforeEnter).toHaveBeenCalledTimes(1)
       })
 
       it('does not call beforeEnter guard if we were already on the page', async () => {
         const router = createRouter({ routes })
-        beforeEnter.mockImplementation((to, from, next) => {
-          next()
-        })
+        beforeEnter.mockImplementation(noGuard)
         await router.push('/guard/one')
         expect(beforeEnter).toHaveBeenCalledTimes(1)
         await router[navigationMethod]('/guard/one')