]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: test global beforeEach on children
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 17:02:41 +0000 (19:02 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 17:02:41 +0000 (19:02 +0200)
__tests__/guards/global-beforeEach.spec.js

index 55c17205c67d2e470a457c41aff1d8388669b8b7..4aec5309227a8605958c7283d0233a1e33911c5c 100644 (file)
@@ -6,8 +6,11 @@ const { Router } = require('../../src/router')
 const fakePromise = require('faked-promise')
 const { NAVIGATION_TYPES, createDom, tick, noGuard } = require('../utils')
 
+/** @typedef {import('../../src/types').RouteRecord} RouteRecord */
+/** @typedef {import('../../src/router').RouterOptions} RouterOptions */
+
 /**
- * @param {Partial<import('../../src/router').RouterOptions> & { routes: import('../../src/types').RouteRecord[]}} options
+ * @param {Partial<RouterOptions> & { routes: RouteRecord[]}} options
  */
 function createRouter(options) {
   return new Router({
@@ -17,14 +20,23 @@ function createRouter(options) {
 }
 
 const Home = { template: `<div>Home</div>` }
+const Nested = { template: `<div>Nested<router-view/></div>` }
 const Foo = { template: `<div>Foo</div>` }
 
-/** @type {import('../../src/types').RouteRecord[]} */
+/** @type {RouteRecord[]} */
 const routes = [
   { path: '/', component: Home },
   { path: '/foo', component: Foo },
   { path: '/other', component: Foo },
   { path: '/n/:i', name: 'n', component: Home },
+  {
+    path: '/nested',
+    component: Nested,
+    children: [
+      { path: '', name: 'nested-default', component: Foo },
+      { path: 'home', name: 'nested-home', component: Home },
+    ],
+  },
 ]
 
 describe('router.beforeEach', () => {
@@ -63,6 +75,18 @@ describe('router.beforeEach', () => {
         expect(spy).not.toHaveBeenCalled()
       })
 
+      it('calls beforeEach guards on navigation between children routes', async () => {
+        const spy = jest.fn()
+        const router = createRouter({ routes })
+        await router.push('/nested')
+        router.beforeEach(spy)
+        spy.mockImplementation(noGuard)
+        await router[navigationMethod]('/nested/home')
+        expect(spy).toHaveBeenCalledTimes(1)
+        await router[navigationMethod]('/nested')
+        expect(spy).toHaveBeenCalledTimes(2)
+      })
+
       it('can redirect to a different location', async () => {
         const spy = jest.fn()
         const router = createRouter({ routes })