]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: add afterEach test for nested routes
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 17:08:22 +0000 (19:08 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 17:08:22 +0000 (19:08 +0200)
__tests__/guards/global-after.spec.js
__tests__/guards/global-beforeEach.spec.js

index 0f55274bf3db92c6403454d2409bb1cf03929fd5..7a4368714229515ab52c3dffcf8a19897a7ae62c 100644 (file)
@@ -17,11 +17,20 @@ function createRouter(options) {
 
 const Home = { template: `<div>Home</div>` }
 const Foo = { template: `<div>Foo</div>` }
+const Nested = { template: `<div>Nested<router-view/></div>` }
 
 /** @type {import('../../src/types').RouteRecord[]} */
 const routes = [
   { path: '/', component: Home },
   { path: '/foo', component: Foo },
+  {
+    path: '/nested',
+    component: Nested,
+    children: [
+      { path: '', name: 'nested-default', component: Foo },
+      { path: 'home', name: 'nested-home', component: Home },
+    ],
+  },
 ]
 
 describe('router.afterEach', () => {
@@ -52,6 +61,25 @@ describe('router.afterEach', () => {
         expect(spy).not.toHaveBeenCalled()
       })
 
+      it('calls afterEach guards on push', async () => {
+        const spy = jest.fn()
+        const router = createRouter({ routes })
+        await router.push('/nested')
+        router.afterEach(spy)
+        await router[navigationMethod]('/nested/home')
+        expect(spy).toHaveBeenCalledTimes(1)
+        expect(spy).toHaveBeenLastCalledWith(
+          expect.objectContaining({ name: 'nested-home' }),
+          expect.objectContaining({ name: 'nested-default' })
+        )
+        await router[navigationMethod]('/nested')
+        expect(spy).toHaveBeenLastCalledWith(
+          expect.objectContaining({ name: 'nested-default' }),
+          expect.objectContaining({ name: 'nested-home' })
+        )
+        expect(spy).toHaveBeenCalledTimes(2)
+      })
+
       it('does not call afterEach if navigation is cancelled', async () => {
         const spy = jest.fn()
         const router = createRouter({ routes })
index 91fde0d580770903e36c70f9149e53ef44866670..a2d288cb1e00708ff466d95940c9632602d939e8 100644 (file)
@@ -20,8 +20,8 @@ function createRouter(options) {
 }
 
 const Home = { template: `<div>Home</div>` }
-const Nested = { template: `<div>Nested<router-view/></div>` }
 const Foo = { template: `<div>Foo</div>` }
+const Nested = { template: `<div>Nested<router-view/></div>` }
 
 /** @type {RouteRecord[]} */
 const routes = [