From 37c4ef196b860e4a337d87256a9aedd0e5a62c27 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 29 Apr 2020 15:38:23 +0200 Subject: [PATCH] test: context with beforeRouteUpdate --- __tests__/guards/beforeRouteUpdate.spec.ts | 5 --- __tests__/guards/guardsContext.spec.ts | 40 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/__tests__/guards/beforeRouteUpdate.spec.ts b/__tests__/guards/beforeRouteUpdate.spec.ts index 4c6a724a..8911ee47 100644 --- a/__tests__/guards/beforeRouteUpdate.spec.ts +++ b/__tests__/guards/beforeRouteUpdate.spec.ts @@ -51,9 +51,4 @@ describe('beforeRouteUpdate', () => { await p expect(router.currentRoute.value.fullPath).toBe('/guard/foo') }) - - 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 index a2ddc09d..7900f9f3 100644 --- a/__tests__/guards/guardsContext.spec.ts +++ b/__tests__/guards/guardsContext.spec.ts @@ -230,3 +230,43 @@ describe('beforeRouteLeave', () => { await router.push('/') }) }) + +describe('beforeRouteUpdate', () => { + it('invokes with the component context', async () => { + expect.assertions(2) + const spy = jest + .fn() + .mockImplementationOnce(function (this: any, to, from, next) { + expect(typeof this.counter).toBe('number') + next() + }) + const WithParam = 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 }), + beforeRouteUpdate: spy, + }) + + const router = createRouter({ + history: createMemoryHistory(), + routes: [ + { path: '/', component }, + { path: '/:id', component: WithParam }, + ], + }) + const app = createApp({ + template: ` + + `, + }) + app.use(router) + const rootEl = document.createElement('div') + document.body.appendChild(rootEl) + app.mount(rootEl) + + await router.isReady() + await router.push('/one') + await router.push('/foo') + expect(spy).toHaveBeenCalledTimes(1) + }) +}) -- 2.47.2