From 54ecb5e7d0f54364eca61d00ea04f29a6f61343b Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 16 Mar 2020 16:18:11 +0100 Subject: [PATCH] test: refactor --- __tests__/extractComponentsGuards.spec.ts | 49 ----------------------- __tests__/lazyLoading.spec.ts | 16 ++++++++ 2 files changed, 16 insertions(+), 49 deletions(-) diff --git a/__tests__/extractComponentsGuards.spec.ts b/__tests__/extractComponentsGuards.spec.ts index b3fb90fc..82485066 100644 --- a/__tests__/extractComponentsGuards.spec.ts +++ b/__tests__/extractComponentsGuards.spec.ts @@ -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 -): 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 - ) - }) }) diff --git a/__tests__/lazyLoading.spec.ts b/__tests__/lazyLoading.spec.ts index a55ae823..86684a4f 100644 --- a/__tests__/lazyLoading.spec.ts +++ b/__tests__/lazyLoading.spec.ts @@ -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({ -- 2.39.5