]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: test beforeEach hook
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 18 Apr 2019 16:54:07 +0000 (18:54 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 18 Apr 2019 16:54:07 +0000 (18:54 +0200)
__tests__/guards.spec.js [new file with mode: 0644]
__tests__/helper.js
package.json
yarn.lock

diff --git a/__tests__/guards.spec.js b/__tests__/guards.spec.js
new file mode 100644 (file)
index 0000000..185182b
--- /dev/null
@@ -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<import('../src/router').RouterOptions> & { routes: import('../src/types').RouteRecord[]}} options
+ */
+function createRouter(options) {
+  return new Router({
+    history: new HTML5History(),
+    ...options,
+  })
+}
+
+const Home = { template: `<div>Home</div>` }
+const Foo = { template: `<div>Foo</div>` }
+
+/** @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(
+      `<!DOCTYPE html><html><head></head><body></body></html>`,
+      {
+        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')
+  })
+})
index 293187ab2cd99e34018a622e534f0757785e2445..855127f8606d92ccb54a7ade91322d49bb25b4bf 100644 (file)
@@ -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({
index 9e566babcc92f37deafd3381a836537d8de1003d..854f695b5bdccf7d67e76e5cd40c71d04c862310 100644 (file)
@@ -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",
index 80ae8006d8bf8bbcab893bcb7691c924bef2ec25..ad8e287f17aab54976bdc4e8c704e7cf8b9fd85b 100644 (file)
--- 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"