]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: refactor guards context test (#177)
authorAustin Akers <austin.akers5@gmail.com>
Wed, 22 Apr 2020 11:56:29 +0000 (06:56 -0500)
committerGitHub <noreply@github.com>
Wed, 22 Apr 2020 11:56:29 +0000 (13:56 +0200)
__tests__/guards/component-beforeRouteLeave.spec.ts
__tests__/guards/guardsContext.spec.ts [new file with mode: 0644]

index 7e22e7273f785710611d95494df63706a87455dd..12f5c69c53498fcdb38603b9983225855f67613f 100644 (file)
@@ -165,9 +165,4 @@ describe('beforeRouteLeave', () => {
     await p.catch(err => {}) // catch the navigation abortion
     expect(currentRoute.fullPath).toBe('/guard')
   })
-
-  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')
 })
diff --git a/__tests__/guards/guardsContext.spec.ts b/__tests__/guards/guardsContext.spec.ts
new file mode 100644 (file)
index 0000000..f58ba03
--- /dev/null
@@ -0,0 +1,54 @@
+/**
+ * @jest-environment jsdom
+ */
+import { createRouter, createMemoryHistory } from '../../src'
+import { createApp, defineComponent } from 'vue'
+
+const component = {
+  template: '<div>Generic</div>',
+}
+
+describe('beforeRouteLeave', () => {
+  it('invokes with the component context', async () => {
+    let componentInstance: any
+    const spy = jest
+      .fn()
+      .mockImplementationOnce(function(this: any, to, from, next) {
+        expect(this).toBe(componentInstance)
+        next()
+      })
+    const WithLeave = defineComponent({
+      template: `text`,
+      created() {
+        componentInstance = this
+      },
+      beforeRouteLeave: 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('/').catch(() => {})
+    expect(spy).toHaveBeenCalledTimes(1)
+  })
+
+  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')
+})