From: Eduardo San Martin Morote Date: Tue, 28 Apr 2020 08:58:37 +0000 (+0200) Subject: test: test onBeforeRouteLeave X-Git-Tag: v4.0.0-alpha.8~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=feae2dc46b06031cc52d59240e2fc365d80611f0;p=thirdparty%2Fvuejs%2Frouter.git test: test onBeforeRouteLeave --- diff --git a/__tests__/guards/onBeforeRouteLeave.spec.ts b/__tests__/guards/onBeforeRouteLeave.spec.ts new file mode 100644 index 00000000..6ec22579 --- /dev/null +++ b/__tests__/guards/onBeforeRouteLeave.spec.ts @@ -0,0 +1,55 @@ +/** + * @jest-environment jsdom + */ +import { + createRouter, + createMemoryHistory, + onBeforeRouteLeave, +} from '../../src' +import { createApp, defineComponent } from 'vue' + +const component = { + template: '
Generic
', +} + +describe('onBeforeRouteLeave', () => { + 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 WithLeave = 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 }), + setup() { + onBeforeRouteLeave(spy) + }, + }) + + const router = createRouter({ + history: createMemoryHistory(), + routes: [ + { path: '/', component }, + { path: '/leave', component: WithLeave as any }, + ], + }) + 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('/leave') + await router.push('/') + expect(spy).toHaveBeenCalledTimes(1) + }) +})