]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: use History mock for router tests
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 28 Jun 2019 15:53:54 +0000 (17:53 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 28 Jun 2019 15:53:54 +0000 (17:53 +0200)
__tests__/HistoryMock.ts [new file with mode: 0644]
__tests__/router.spec.js
__tests__/utils.ts
src/history/abstract.ts

diff --git a/__tests__/HistoryMock.ts b/__tests__/HistoryMock.ts
new file mode 100644 (file)
index 0000000..357022f
--- /dev/null
@@ -0,0 +1,11 @@
+import { HistoryLocationNormalized, START } from '../src/history/base'
+import { AbstractHistory } from '../src/history/abstract'
+
+export class HistoryMock extends AbstractHistory {
+  constructor(start: string | HistoryLocationNormalized = START) {
+    super()
+    this.location =
+      typeof start === 'string' ? this.utils.normalizeLocation(start) : start
+    this.queue = [this.location]
+  }
+}
index a748511b1c5294ce07abd7f980e02b6b21a3cf28..1990e9abefc124101410fbd9952002d55620b2c5 100644 (file)
@@ -6,18 +6,7 @@ const { HTML5History } = require('../src/history/html5')
 const { AbstractHistory } = require('../src/history/abstract')
 const { Router } = require('../src/router')
 const { NavigationCancelled } = require('../src/errors')
-const { createDom, components, tick } = require('./utils')
-
-/**
- * Created a mocked version of the history
- * @param {string} [start] starting locationh
- */
-function mockHistory(start) {
-  // @ts-ignore
-  if (start) window.location = start
-  // TODO: actually do a mock
-  return new HTML5History()
-}
+const { createDom, components, tick, HistoryMock } = require('./utils')
 
 /** @type {import('../src/types').RouteRecord[]} */
 const routes = [
@@ -45,7 +34,7 @@ describe('Router', () => {
   })
 
   it('can be instantiated', () => {
-    const history = mockHistory()
+    const history = new HistoryMock()
     const router = new Router({ history, routes })
     expect(router.currentRoute).toEqual({
       fullPath: '/',
@@ -58,7 +47,7 @@ describe('Router', () => {
 
   // TODO: should do other checks not based on history implem
   it.skip('takes browser location', () => {
-    const history = mockHistory('/search?q=dog#footer')
+    const history = new HistoryMock('/search?q=dog#footer')
     const router = new Router({ history, routes })
     expect(router.currentRoute).toEqual({
       fullPath: '/search?q=dog#footer',
@@ -70,7 +59,7 @@ describe('Router', () => {
   })
 
   it('calls history.push with router.push', async () => {
-    const history = mockHistory()
+    const history = new HistoryMock()
     const router = new Router({ history, routes })
     jest.spyOn(history, 'push')
     await router.push('/foo')
@@ -84,7 +73,7 @@ describe('Router', () => {
   })
 
   it('calls history.replace with router.replace', async () => {
-    const history = mockHistory()
+    const history = new HistoryMock()
     const router = new Router({ history, routes })
     jest.spyOn(history, 'replace')
     await router.replace('/foo')
@@ -98,7 +87,7 @@ describe('Router', () => {
   })
 
   it('can pass replace option to push', async () => {
-    const history = mockHistory()
+    const history = new HistoryMock()
     const router = new Router({ history, routes })
     jest.spyOn(history, 'replace')
     await router.push({ path: '/foo', replace: true })
@@ -115,7 +104,7 @@ describe('Router', () => {
     async function checkNavigationCancelledOnPush(target) {
       const [p1, r1] = fakePromise()
       const [p2, r2] = fakePromise()
-      const history = mockHistory()
+      const history = new HistoryMock()
       const router = new Router({ history, routes })
       router.beforeEach(async (to, from, next) => {
         if (to.name !== 'Param') return next()
@@ -210,7 +199,7 @@ describe('Router', () => {
 
   describe('matcher', () => {
     it('handles one redirect from route record', async () => {
-      const history = mockHistory()
+      const history = new HistoryMock()
       const router = new Router({ history, routes })
       const loc = await router.push('/to-foo')
       expect(loc.name).toBe('Foo')
@@ -220,7 +209,7 @@ describe('Router', () => {
     })
 
     it('drops query and params on redirect if not provided', async () => {
-      const history = mockHistory()
+      const history = new HistoryMock()
       const router = new Router({ history, routes })
       const loc = await router.push('/to-foo?hey=foo#fa')
       expect(loc.name).toBe('Foo')
@@ -232,7 +221,7 @@ describe('Router', () => {
     })
 
     it('allows object in redirect', async () => {
-      const history = mockHistory()
+      const history = new HistoryMock()
       const router = new Router({ history, routes })
       const loc = await router.push('/to-foo-named')
       expect(loc.name).toBe('Foo')
@@ -242,7 +231,7 @@ describe('Router', () => {
     })
 
     it('can pass on query and hash when redirecting', async () => {
-      const history = mockHistory()
+      const history = new HistoryMock()
       const router = new Router({ history, routes })
       const loc = await router.push('/inc-query-hash?n=3#fa')
       expect(loc).toMatchObject({
@@ -259,7 +248,7 @@ describe('Router', () => {
     })
 
     it('handles multiple redirect fields in route record', async () => {
-      const history = mockHistory()
+      const history = new HistoryMock()
       const router = new Router({ history, routes })
       const loc = await router.push('/to-foo2')
       expect(loc.name).toBe('Foo')
index 05e0bc63fa6ddd92b03c8b8973aea3e71594f86f..5220564d8703e187662fe5efe9ee32a14d3b645e 100644 (file)
@@ -1,6 +1,8 @@
 import { JSDOM, ConstructorOptions } from 'jsdom'
 import { NavigationGuard } from '../src/types'
 
+export { HistoryMock } from './HistoryMock'
+
 export const tick = () => new Promise(resolve => process.nextTick(resolve))
 
 export const NAVIGATION_TYPES = ['push', 'replace']
index 38485562182aac871687605532ed90edfc975f5a..9a50ba1ad336ec8b6ed30aacce21a22f688bc459 100644 (file)
@@ -10,7 +10,7 @@ import { NavigationCallback, HistoryState, START } from './base'
 // const cs = consola.withTag('abstract')
 
 export class AbstractHistory extends BaseHistory {
-  private listeners: NavigationCallback[] = []
+  protected listeners: NavigationCallback[] = []
   public queue: HistoryLocationNormalized[] = [START]
   public position: number = 0
 
@@ -84,7 +84,7 @@ export class AbstractHistory extends BaseHistory {
     this.listeners = []
   }
 
-  private triggerListeners(
+  protected triggerListeners(
     to: HistoryLocationNormalized,
     from: HistoryLocationNormalized,
     { direction }: { direction: NavigationDirection }