From: Austin Akers Date: Wed, 22 Apr 2020 11:56:29 +0000 (-0500) Subject: test: refactor guards context test (#177) X-Git-Tag: v4.0.0-alpha.8~59 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4711c8017118b64096271aad81b40ee85ef4842d;p=thirdparty%2Fvuejs%2Frouter.git test: refactor guards context test (#177) --- diff --git a/__tests__/guards/component-beforeRouteLeave.spec.ts b/__tests__/guards/component-beforeRouteLeave.spec.ts index 7e22e727..12f5c69c 100644 --- a/__tests__/guards/component-beforeRouteLeave.spec.ts +++ b/__tests__/guards/component-beforeRouteLeave.spec.ts @@ -165,9 +165,4 @@ describe('beforeRouteLeave', () => { await p.catch(err => {}) // catch the navigation abortion expect(currentRoute.fullPath).toBe('/guard') }) - - 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 new file mode 100644 index 00000000..f58ba03f --- /dev/null +++ b/__tests__/guards/guardsContext.spec.ts @@ -0,0 +1,54 @@ +/** + * @jest-environment jsdom + */ +import { createRouter, createMemoryHistory } from '../../src' +import { createApp, defineComponent } from 'vue' + +const component = { + template: '
Generic
', +} + +describe('beforeRouteLeave', () => { + it('invokes with the component context', async () => { + let componentInstance: any + const spy = jest + .fn() + .mockImplementationOnce(function(this: any, to, from, next) { + expect(this).toBe(componentInstance) + next() + }) + const WithLeave = defineComponent({ + template: `text`, + created() { + componentInstance = this + }, + beforeRouteLeave: 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('/').catch(() => {}) + expect(spy).toHaveBeenCalledTimes(1) + }) + + 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') +})