]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: context with beforeRouteUpdate
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 29 Apr 2020 13:38:23 +0000 (15:38 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 29 Apr 2020 13:38:23 +0000 (15:38 +0200)
__tests__/guards/beforeRouteUpdate.spec.ts
__tests__/guards/guardsContext.spec.ts

index 4c6a724a5750ad9ace38eb9234d89544cfb4fd11..8911ee47fb9ca852d90cf061fc2b63ba4279429c 100644 (file)
@@ -51,9 +51,4 @@ describe('beforeRouteUpdate', () => {
     await p
     expect(router.currentRoute.value.fullPath).toBe('/guard/foo')
   })
-
-  it.todo('invokes with the component context')
-  it.todo('invokes with the component context with named views')
-  it.todo('invokes with the component context with nested views')
-  it.todo('invokes with the component context with nested named views')
 })
index a2ddc09df6d4834974c97161caff3f544a20e882..7900f9f3b5c7bfeaf2a9df2085a2e73aae24b8dd 100644 (file)
@@ -230,3 +230,43 @@ describe('beforeRouteLeave', () => {
     await router.push('/')
   })
 })
+
+describe('beforeRouteUpdate', () => {
+  it('invokes with the component context', async () => {
+    expect.assertions(2)
+    const spy = jest
+      .fn()
+      .mockImplementationOnce(function (this: any, to, from, next) {
+        expect(typeof this.counter).toBe('number')
+        next()
+      })
+    const WithParam = defineComponent({
+      template: `text`,
+      // we use data to check if the context is the right one because saving `this` in a variable logs a few warnings
+      data: () => ({ counter: 0 }),
+      beforeRouteUpdate: spy,
+    })
+
+    const router = createRouter({
+      history: createMemoryHistory(),
+      routes: [
+        { path: '/', component },
+        { path: '/:id', component: WithParam },
+      ],
+    })
+    const app = createApp({
+      template: `
+      <router-view />
+      `,
+    })
+    app.use(router)
+    const rootEl = document.createElement('div')
+    document.body.appendChild(rootEl)
+    app.mount(rootEl)
+
+    await router.isReady()
+    await router.push('/one')
+    await router.push('/foo')
+    expect(spy).toHaveBeenCalledTimes(1)
+  })
+})