From: Eduardo San Martin Morote Date: Mon, 16 Mar 2020 15:25:14 +0000 (+0100) Subject: test: add tests for in component guards X-Git-Tag: v4.0.0-alpha.4~47 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6c2b6f349dea716a4f56728b0799088879b4fd5;p=thirdparty%2Fvuejs%2Frouter.git test: add tests for in component guards --- diff --git a/__tests__/guards/component-beforeRouteEnter.spec.ts b/__tests__/guards/component-beforeRouteEnter.spec.ts index cf129633..f807b67b 100644 --- a/__tests__/guards/component-beforeRouteEnter.spec.ts +++ b/__tests__/guards/component-beforeRouteEnter.spec.ts @@ -116,7 +116,7 @@ beforeEach(() => { resetMocks() }) -describe.skip('beforeRouteEnter', () => { +describe('beforeRouteEnter', () => { beforeAll(() => { createDom() }) @@ -192,25 +192,6 @@ describe.skip('beforeRouteEnter', () => { expect(router.currentRoute.value.fullPath).not.toBe('/named') }) - // TODO: async components - it.skip('resolves async components before guarding', async () => { - const spy = jest.fn(noGuard) - const component = { - template: `
`, - beforeRouteEnter: spy, - } - const [promise, resolve] = fakePromise() - const router = createRouter({ - routes: [...routes, { path: '/async', component: () => promise }], - }) - const pushPromise = router[navigationMethod]('/async') - expect(spy).not.toHaveBeenCalled() - resolve(component) - await pushPromise - - expect(spy).toHaveBeenCalledTimes(1) - }) - it('does not call beforeRouteEnter if we were already on the page', async () => { const router = createRouter({ routes }) beforeRouteEnter.mockImplementation(noGuard) diff --git a/__tests__/guards/component-beforeRouteLeave.spec.ts b/__tests__/guards/component-beforeRouteLeave.spec.ts index c008f7e4..99d4c913 100644 --- a/__tests__/guards/component-beforeRouteLeave.spec.ts +++ b/__tests__/guards/component-beforeRouteLeave.spec.ts @@ -98,7 +98,7 @@ beforeEach(() => { resetMocks() }) -describe.skip('beforeRouteLeave', () => { +describe('beforeRouteLeave', () => { beforeAll(() => { createDom() }) @@ -167,26 +167,6 @@ describe.skip('beforeRouteLeave', () => { expect(nested.nestedNestedFoo).toHaveBeenCalledTimes(1) }) - // TODO: implem async components - it.skip('works when a lazy loaded component', async () => { - const router = createRouter({ - routes: [ - ...routes, - { - path: '/lazy', - component: () => Promise.resolve({ ...Foo, beforeRouteLeave }), - }, - ], - }) - beforeRouteLeave.mockImplementationOnce((to, from, next) => { - next() - }) - await router.push('/lazy') - expect(beforeRouteLeave).not.toHaveBeenCalled() - await router[navigationMethod]('/foo') - expect(beforeRouteLeave).toHaveBeenCalledTimes(1) - }) - it('can cancel navigation', async () => { const router = createRouter({ routes }) beforeRouteLeave.mockImplementationOnce(async (to, from, next) => { diff --git a/__tests__/guards/component-beforeRouteUpdate.spec.ts b/__tests__/guards/component-beforeRouteUpdate.spec.ts index 6ffc1ba9..211decbf 100644 --- a/__tests__/guards/component-beforeRouteUpdate.spec.ts +++ b/__tests__/guards/component-beforeRouteUpdate.spec.ts @@ -34,7 +34,7 @@ beforeEach(() => { beforeRouteUpdate.mockReset() }) -describe.skip('beforeRouteUpdate', () => { +describe('beforeRouteUpdate', () => { beforeAll(() => { createDom() }) @@ -51,25 +51,6 @@ describe.skip('beforeRouteUpdate', () => { expect(beforeRouteUpdate).toHaveBeenCalledTimes(1) }) - // TODO: add async component - it.skip('resolves async components before guarding', async () => { - const spy = jest.fn((to, from, next) => next()) - const component = { - template: `
`, - beforeRouteUpdate: spy, - } - const router = createRouter({ - routes: [ - ...routes, - { path: '/async/:a', component: () => Promise.resolve(component) }, - ], - }) - await router[navigationMethod]('/async/a') - expect(spy).not.toHaveBeenCalled() - await router[navigationMethod]('/async/b') - expect(spy).toHaveBeenCalledTimes(1) - }) - it('waits before navigating', async () => { const [promise, resolve] = fakePromise() const router = createRouter({ routes }) diff --git a/__tests__/lazyLoading.spec.ts b/__tests__/lazyLoading.spec.ts index 86684a4f..df75c71d 100644 --- a/__tests__/lazyLoading.spec.ts +++ b/__tests__/lazyLoading.spec.ts @@ -192,6 +192,47 @@ describe('Lazy Loading', () => { expect(spy).toHaveBeenCalledTimes(1) }) + it('beforeRouteLeave works on a lazy loaded component', async () => { + const { promise, resolve } = createLazyComponent() + const spy = jest.fn((to, from, next) => next()) + const component = jest.fn(() => + promise.then(() => ({ beforeRouteLeave: spy })) + ) + const { router } = newRouter({ + routes: [ + { path: '/foo', component }, + { path: '/', component: {} }, + ], + }) + + resolve() + await router.push('/foo') + expect(component).toHaveBeenCalledTimes(1) + expect(spy).toHaveBeenCalledTimes(0) + + await router.push('/') + expect(spy).toHaveBeenCalledTimes(1) + }) + + it('beforeRouteUpdate works on a lazy loaded component', async () => { + const { promise, resolve } = createLazyComponent() + const spy = jest.fn((to, from, next) => next()) + const component = jest.fn(() => + promise.then(() => ({ beforeRouteUpdate: spy })) + ) + const { router } = newRouter({ + routes: [{ path: '/:id', component }], + }) + + resolve() + await router.push('/foo') + expect(component).toHaveBeenCalledTimes(1) + expect(spy).toHaveBeenCalledTimes(0) + + await router.push('/bar') + expect(spy).toHaveBeenCalledTimes(1) + }) + it('aborts the navigation if async fails', async () => { const { component, reject } = createLazyComponent() const { router } = newRouter({