]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test(guards): test onBeforeRouteLeave are removed
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 21 Jul 2020 11:49:11 +0000 (13:49 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 21 Jul 2020 11:49:11 +0000 (13:49 +0200)
__tests__/guards/onBeforeRouteLeave.spec.ts

index 6ec2257948b2ed8aba30570f64ec222ea2bcc36b..17165eb9909705e4ab678e06414ef75eaa6c054c 100644 (file)
@@ -52,4 +52,46 @@ describe('onBeforeRouteLeave', () => {
     await router.push('/')
     expect(spy).toHaveBeenCalledTimes(1)
   })
+
+  it('removes guards when leaving the route', async () => {
+    expect.assertions(3)
+    const spy = jest
+      .fn()
+      .mockImplementation(function (this: any, to, from, next) {
+        expect(typeof this.counter).toBe('number')
+        next()
+      })
+    const WithLeave = 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 }),
+      setup() {
+        onBeforeRouteLeave(spy)
+      },
+    })
+
+    const router = createRouter({
+      history: createMemoryHistory(),
+      routes: [
+        { path: '/', component },
+        { path: '/leave', component: WithLeave as any },
+      ],
+    })
+    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('/leave')
+    await router.push('/')
+    await router.push('/leave')
+    await router.push('/')
+    expect(spy).toHaveBeenCalledTimes(2)
+  })
 })