]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: refactor
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 16 Mar 2020 15:18:11 +0000 (16:18 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 16 Mar 2020 15:18:11 +0000 (16:18 +0100)
__tests__/extractComponentsGuards.spec.ts
__tests__/lazyLoading.spec.ts

index b3fb90fc1e8025bab110f7e1b10799e180bc0fd5..824850668e2466fdf76a2bf1b35e95f60a4ff953 100644 (file)
@@ -1,7 +1,6 @@
 import { extractComponentsGuards } from '../src/utils'
 import { START_LOCATION_NORMALIZED, RouteRecord } from '../src/types'
 import { components } from './utils'
-import { RouteRecordNormalized } from '../src/matcher/types'
 import { normalizeRouteRecord } from '../src/matcher'
 
 const beforeRouteEnter = jest.fn()
@@ -23,37 +22,6 @@ const SingleGuardNamed: RouteRecord = {
   },
 }
 
-function makeAsync(
-  record: Exclude<RouteRecord, { redirect: any }>
-): RouteRecordNormalized {
-  if ('components' in record) {
-    const copy = { ...record }
-    copy.components = Object.keys(record.components).reduce(
-      (components, name) => {
-        // @ts-ignore
-        components[name] = () => Promise.resolve(record.components[name])
-        return components
-      },
-      {} as typeof record['components']
-    )
-    return normalizeRouteRecord(copy)
-  } else {
-    const { component, ...copy } = record
-    if (typeof component === 'function')
-      return normalizeRouteRecord({
-        ...copy,
-        components: { default: component },
-      })
-
-    return normalizeRouteRecord({
-      ...copy,
-      components: {
-        default: () => Promise.resolve(component),
-      },
-    })
-  }
-}
-
 beforeEach(() => {
   beforeRouteEnter.mockReset()
   beforeRouteEnter.mockImplementation((to, from, next) => {
@@ -100,21 +68,4 @@ describe('extractComponentsGuards', () => {
     await checkGuards([SingleGuard, SingleGuard], 2)
     await checkGuards([SingleGuardNamed, SingleGuardNamed], 4)
   })
-
-  it('works with async components', async () => {
-    await checkGuards([makeAsync(NoGuard)], 0, 1)
-    await checkGuards([makeAsync(NoGuard), makeAsync(NoGuard)], 0, 2)
-    await checkGuards([makeAsync(SingleGuard)], 1, 1)
-    await checkGuards([makeAsync(SingleGuard), makeAsync(NoGuard)], 1, 2)
-    await checkGuards(
-      [makeAsync(SingleGuard), makeAsync(SingleGuardNamed)],
-      3,
-      3
-    )
-    await checkGuards([makeAsync(SingleGuard), makeAsync(SingleGuard)], 2)
-    await checkGuards(
-      [makeAsync(SingleGuardNamed), makeAsync(SingleGuardNamed)],
-      4
-    )
-  })
 })
index a55ae8235442bb8c95d3c3b3e24d879478735e58..86684a4fe235ab797bce564c512a534b16cd1424 100644 (file)
@@ -176,6 +176,22 @@ describe('Lazy Loading', () => {
     expect(component).toHaveBeenCalledTimes(0)
   })
 
+  it('invokes beforeRouteEnter after lazy loading the component', async () => {
+    const { promise, resolve } = createLazyComponent()
+    const spy = jest.fn((to, from, next) => next())
+    const component = jest.fn(() =>
+      promise.then(() => ({ beforeRouteEnter: spy }))
+    )
+    const { router } = newRouter({
+      routes: [{ path: '/foo', component }],
+    })
+
+    resolve()
+    await router.push('/foo')
+    expect(component).toHaveBeenCalledTimes(1)
+    expect(spy).toHaveBeenCalledTimes(1)
+  })
+
   it('aborts the navigation if async fails', async () => {
     const { component, reject } = createLazyComponent()
     const { router } = newRouter({