From: Eduardo San Martin Morote Date: Fri, 28 Jun 2019 15:53:54 +0000 (+0200) Subject: test: use History mock for router tests X-Git-Tag: v4.0.0-alpha.0~327 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5511d6f4190a3958f739739f8c7f8787f978f0f1;p=thirdparty%2Fvuejs%2Frouter.git test: use History mock for router tests --- diff --git a/__tests__/HistoryMock.ts b/__tests__/HistoryMock.ts new file mode 100644 index 00000000..357022f2 --- /dev/null +++ b/__tests__/HistoryMock.ts @@ -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] + } +} diff --git a/__tests__/router.spec.js b/__tests__/router.spec.js index a748511b..1990e9ab 100644 --- a/__tests__/router.spec.js +++ b/__tests__/router.spec.js @@ -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') diff --git a/__tests__/utils.ts b/__tests__/utils.ts index 05e0bc63..5220564d 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -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'] diff --git a/src/history/abstract.ts b/src/history/abstract.ts index 38485562..9a50ba1a 100644 --- a/src/history/abstract.ts +++ b/src/history/abstract.ts @@ -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 }