]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: beforeEnter nested routes
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 20:14:51 +0000 (22:14 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 20:14:51 +0000 (22:14 +0200)
__tests__/guards/route-beforeEnter.spec.js

index ed48bcce66ef564def21c9043552f213ff44a839..e3720cfacf20ce8637b786d41f34c1162c1df599 100644 (file)
@@ -24,6 +24,16 @@ const Foo = { template: `<div>Foo</div>` }
 
 const beforeEnter = jest.fn()
 const beforeEnters = [jest.fn(), jest.fn()]
+const nested = {
+  parent: jest.fn(),
+  nestedEmpty: jest.fn(),
+  nestedA: jest.fn(),
+  nestedAbs: jest.fn(),
+  nestedNested: jest.fn(),
+  nestedNestedFoo: jest.fn(),
+  nestedNestedParam: jest.fn(),
+}
+
 /** @type {RouteRecord[]} */
 const routes = [
   { path: '/', component: Home },
@@ -39,6 +49,47 @@ const routes = [
     beforeEnter: beforeEnters,
     component: Foo,
   },
+  {
+    path: '/nested',
+    component: {
+      ...Home,
+      beforeRouteEnter: nested.parent,
+    },
+    children: [
+      {
+        path: '',
+        name: 'nested-empty-path',
+        component: { ...Home, beforeRouteEnter: nested.nestedEmpty },
+      },
+      {
+        path: 'a',
+        name: 'nested-path',
+        component: { ...Home, beforeRouteEnter: nested.nestedA },
+      },
+      {
+        path: '/abs-nested',
+        name: 'absolute-nested',
+        component: { ...Home, beforeRouteEnter: nested.nestedAbs },
+      },
+      {
+        path: 'nested',
+        name: 'nested-nested',
+        component: { ...Home, beforeRouteEnter: nested.nestedNested },
+        children: [
+          {
+            path: 'foo',
+            name: 'nested-nested-foo',
+            component: { ...Home, beforeRouteEnter: nested.nestedNestedFoo },
+          },
+          {
+            path: 'param/:p',
+            name: 'nested-nested-param',
+            component: { ...Home, beforeRouteEnter: nested.nestedNestedParam },
+          },
+        ],
+      },
+    ],
+  },
 ]
 
 function resetMocks() {
@@ -47,6 +98,10 @@ function resetMocks() {
     spy.mockReset()
     spy.mockImplementationOnce(noGuard)
   })
+  for (const key in nested) {
+    nested[key].mockReset()
+    nested[key].mockImplementation(noGuard)
+  }
 }
 
 beforeEach(() => {
@@ -79,6 +134,27 @@ describe('beforeEnter', () => {
         )
       })
 
+      it('call beforeEnter in nested views', async () => {
+        const router = createRouter({ routes })
+        await router.push('/nested/a')
+        resetMocks()
+        await router[navigationMethod]('/nested/nested/foo')
+        expect(nested.parent).not.toHaveBeenCalled()
+        expect(nested.nestedA).not.toHaveBeenCalled()
+        expect(nested.nestedNested).toHaveBeenCalledTimes(1)
+        expect(nested.nestedNestedFoo).toHaveBeenCalledTimes(1)
+        expect(nested.nestedNested).toHaveBeenCalledWith(
+          expect.objectContaining({ path: '/nested/nested/foo' }),
+          expect.objectContaining({ path: '/nested/a' }),
+          expect.any(Function)
+        )
+        expect(nested.nestedNestedFoo).toHaveBeenCalledWith(
+          expect.objectContaining({ path: '/nested/nested/foo' }),
+          expect.objectContaining({ path: '/nested/a' }),
+          expect.any(Function)
+        )
+      })
+
       it('calls beforeEnter different records, same component', async () => {
         const router = createRouter({ routes })
         beforeEnter.mockImplementationOnce(noGuard)