From: Eduardo San Martin Morote Date: Thu, 18 Apr 2019 16:54:07 +0000 (+0200) Subject: test: test beforeEach hook X-Git-Tag: v4.0.0-alpha.0~431 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fcfe0cdb5283040557f22f970a8f4efcb811988;p=thirdparty%2Fvuejs%2Frouter.git test: test beforeEach hook --- diff --git a/__tests__/guards.spec.js b/__tests__/guards.spec.js new file mode 100644 index 00000000..185182be --- /dev/null +++ b/__tests__/guards.spec.js @@ -0,0 +1,65 @@ +// @ts-check +require('./helper') +const expect = require('expect') +const { HTML5History } = require('../src/history/html5') +const { Router } = require('../src/router') +const { JSDOM } = require('jsdom') +const fakePromise = require('faked-promise') + +/** + * @param {Partial & { routes: import('../src/types').RouteRecord[]}} options + */ +function createRouter(options) { + return new Router({ + history: new HTML5History(), + ...options, + }) +} + +const Home = { template: `
Home
` } +const Foo = { template: `
Foo
` } + +/** @type {import('../src/types').RouteRecord[]} */ +const routes = [ + { path: '/', component: Home }, + { path: '/foo', component: Foo }, +] + +describe('navigation guards', () => { + beforeAll(() => { + // TODO: move to utils for tests that need DOM + const dom = new JSDOM( + ``, + { + url: 'https://example.org/', + referrer: 'https://example.com/', + contentType: 'text/html', + } + ) + + // @ts-ignore + global.window = dom.window + }) + + it('calls beforeEach guards on push', () => { + const spy = jest.fn() + const router = createRouter({ routes }) + router.beforeEach(spy) + router.push('/foo') + expect(spy).toHaveBeenCalledTimes(1) + }) + + it('waits before navigating', async () => { + const [promise, resolve] = fakePromise() + const router = createRouter({ routes }) + router.beforeEach(async (to, from, next) => { + await promise + next() + }) + const p = router.push('/foo') + expect(router.currentRoute.fullPath).toBe('/') + resolve() + await p + expect(router.currentRoute.fullPath).toBe('/foo') + }) +}) diff --git a/__tests__/helper.js b/__tests__/helper.js index 293187ab..855127f8 100644 --- a/__tests__/helper.js +++ b/__tests__/helper.js @@ -2,6 +2,9 @@ if (typeof global !== 'undefined' && typeof global.beforeAll === 'undefined') { global.beforeAll = global.before global.afterAll = global.after + const mocks = require('jest-mock') + global.jest = mocks + const expect = require('expect') // monkey patch jest snapshots expect.extend({ diff --git a/package.json b/package.json index 9e566bab..854f695b 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@types/mocha": "^5.2.6", "consola": "^2.5.8", "expect": "^24.7.1", + "faked-promise": "^2.1.0", "html-webpack-plugin": "^3.2.0", "jest": "^24.7.1", "jsdom": "^14.0.0", diff --git a/yarn.lock b/yarn.lock index 80ae8006..ad8e287f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2180,6 +2180,11 @@ extsprintf@^1.2.0: resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f" integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8= +faked-promise@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/faked-promise/-/faked-promise-2.1.0.tgz#988dd6f7e1c30638be2cd0d67e944b51d5dbbaeb" + integrity sha512-p5QCxMj8OsHjBs0aIEbg1WFW019Tib3LIWpz6Fyl5uVf4A3FRJzcMrJgiTgwVB5k99k3M566U6NUa/rEi473fA== + fast-deep-equal@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz#7b05218ddf9667bf7f370bf7fdb2cb15fdd0aa49"