From: Eduardo San Martin Morote Date: Wed, 22 Apr 2020 12:27:13 +0000 (+0200) Subject: test: refactor e2e as integration X-Git-Tag: v4.0.0-alpha.8~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca754ff7f70dc1ff133b862c43493e73e1332793;p=thirdparty%2Fvuejs%2Frouter.git test: refactor e2e as integration --- diff --git a/__tests__/guards/guardsContext.spec.ts b/__tests__/guards/guardsContext.spec.ts index 9592c496..6f25d4d4 100644 --- a/__tests__/guards/guardsContext.spec.ts +++ b/__tests__/guards/guardsContext.spec.ts @@ -43,11 +43,190 @@ describe('beforeRouteLeave', () => { await router.isReady() await router.push('/leave') - await router.push('/').catch(() => {}) + await router.push('/') 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') + it('invokes with the component context with named views', async () => { + expect.assertions(2) + const WithLeaveOne = 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 }), + beforeRouteLeave: jest + .fn() + .mockImplementationOnce(function(this: any, to, from, next) { + expect(typeof this.counter).toBe('number') + next() + }), + }) + const WithLeaveTwo = 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 }), + beforeRouteLeave: jest + .fn() + .mockImplementationOnce(function(this: any, to, from, next) { + expect(typeof this.counter).toBe('number') + next() + }), + }) + + const router = createRouter({ + history: createMemoryHistory(), + routes: [ + { path: '/', component }, + { + path: '/leave', + components: { + one: WithLeaveOne as any, + two: WithLeaveTwo 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('/') + }) + + it('invokes with the component context with nested views', async () => { + expect.assertions(2) + const WithLeaveParent = defineComponent({ + template: ``, + // 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 }), + beforeRouteLeave: 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 }), + beforeRouteLeave: jest + .fn() + .mockImplementationOnce(function(this: any, to, from, next) { + expect(typeof this.counter).toBe('number') + next() + }), + }) + + const router = createRouter({ + history: createMemoryHistory(), + routes: [ + { path: '/', component }, + { + path: '/leave', + component: WithLeaveParent as any, + children: [ + { + path: '', + 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('/') + }) + + it('invokes with the component context with nested named views', async () => { + expect.assertions(3) + const WithLeaveParent = defineComponent({ + template: ` + + + `, + // 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 }), + beforeRouteLeave: jest + .fn() + .mockImplementationOnce(function(this: any, to, from, next) { + expect(typeof this.counter).toBe('number') + next() + }), + }) + const WithLeaveOne = 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 }), + beforeRouteLeave: jest + .fn() + .mockImplementationOnce(function(this: any, to, from, next) { + expect(typeof this.counter).toBe('number') + next() + }), + }) + const WithLeaveTwo = 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 }), + beforeRouteLeave: jest + .fn() + .mockImplementationOnce(function(this: any, to, from, next) { + expect(typeof this.counter).toBe('number') + next() + }), + }) + + const router = createRouter({ + history: createMemoryHistory(), + routes: [ + { path: '/', component }, + { + path: '/leave', + component: WithLeaveParent as any, + children: [ + { + path: '', + components: { + one: WithLeaveOne as any, + two: WithLeaveTwo 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('/') + }) }) diff --git a/e2e/navigation-guards/index.html b/e2e/navigation-guards/index.html deleted file mode 100644 index b4911039..00000000 --- a/e2e/navigation-guards/index.html +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - Vue Router e2e tests - Navigation Guards - - - - - << Back to Homepage -
- -
- - diff --git a/e2e/navigation-guards/index.ts b/e2e/navigation-guards/index.ts deleted file mode 100644 index 8cb0b9e6..00000000 --- a/e2e/navigation-guards/index.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { createRouter, createWebHistory, onBeforeRouteLeave } from '../../src' -import { RouteComponent } from '../../src/types' -import { createApp, ref } from 'vue' - -const Home: RouteComponent = { - template: `
Home
`, -} - -const GuardedWithLeave: RouteComponent = { - name: 'GuardedWithLeave', - - template: ` -
-

try to leave

-

So far, you tried {{ tries }} times

-
- `, - - setup() { - console.log('setup in cant leave') - const tries = ref(0) - - onBeforeRouteLeave(function(to, from, next) { - if (window.confirm()) next() - else { - tries.value++ - next(false) - } - }) - return { tries } - }, -} - -const router = createRouter({ - history: createWebHistory('/' + __dirname), - routes: [ - { path: '/', component: Home, name: 'home' }, - { path: '/cant-leave', component: GuardedWithLeave }, - ], -}) - -const app = createApp({ - template: ` -
-
    -
  • - / -
  • -
  • - /cant-leave -
  • -
- - -
- `, -}) -app.use(router) - -window.vm = app.mount('#app') -window.r = router diff --git a/e2e/specs/navigation-guards.js b/e2e/specs/navigation-guards.js deleted file mode 100644 index fd1bc346..00000000 --- a/e2e/specs/navigation-guards.js +++ /dev/null @@ -1,34 +0,0 @@ -const bsStatus = require('../browserstack-send-status') - -const baseURL = 'http://localhost:8080/navigation-guards' - -module.exports = { - ...bsStatus(), - - /** @type {import('nightwatch').NightwatchTest} */ - 'blocks leaving navigation with onBeforeRouteLeave': function(browser) { - browser - .url(baseURL) - .assert.urlEquals(baseURL + '/') - .waitForElementPresent('#app > *', 1000) - .click('li:nth-child(2) a') - .assert.urlEquals(baseURL + '/cant-leave') - .assert.containsText('#tries', '0 times') - .click('li:nth-child(1) a') - .dismissAlert() - .waitFor(100) - .assert.urlEquals(baseURL + '/cant-leave') - .assert.containsText('#tries', '1 times') - .click('li:nth-child(1) a') - .dismissAlert() - .waitFor(100) - .assert.urlEquals(baseURL + '/cant-leave') - .assert.containsText('#tries', '2 times') - .click('li:nth-child(1) a') - .acceptAlert() - .waitFor(100) - .assert.urlEquals(baseURL + '/') - - .end() - }, -}