From: Eduardo San Martin Morote Date: Wed, 9 Oct 2019 13:15:09 +0000 (+0200) Subject: test: use new histories X-Git-Tag: v4.0.0-alpha.0~210 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=52ef74169d501636227f958720dc50afb76089c6;p=thirdparty%2Fvuejs%2Frouter.git test: use new histories --- diff --git a/__tests__/errors.spec.ts b/__tests__/errors.spec.ts index 0bd4ed63..61b94575 100644 --- a/__tests__/errors.spec.ts +++ b/__tests__/errors.spec.ts @@ -1,5 +1,4 @@ -import { AbstractHistory } from '../src/history/abstract' -import { Router } from '../src/router' +import { Router, createMemoryHistory } from '../src' import { NavigationAborted, NavigationGuardRedirect } from '../src/errors' import { components, tick } from './utils' import { RouteRecord } from '../src/types' @@ -24,7 +23,7 @@ const routes: RouteRecord[] = [ const onError = jest.fn() function createRouter() { - const history = new AbstractHistory() + const history = createMemoryHistory() const router = new Router({ history, routes, diff --git a/__tests__/guards/component-beforeRouteEnter.spec.ts b/__tests__/guards/component-beforeRouteEnter.spec.ts index f2aaa52f..3c4ecda4 100644 --- a/__tests__/guards/component-beforeRouteEnter.spec.ts +++ b/__tests__/guards/component-beforeRouteEnter.spec.ts @@ -1,14 +1,14 @@ -import { HTML5History } from '../../src/history/html5' -import { Router, RouterOptions } from '../../src/router' +import { RouterOptions } from '../../src/router' import fakePromise from 'faked-promise' import { NAVIGATION_TYPES, createDom, noGuard } from '../utils' import { RouteRecord, NavigationGuard } from '../../src/types' +import { Router, createHistory } from '../../src' function createRouter( options: Partial & { routes: RouteRecord[] } ) { return new Router({ - history: new HTML5History(), + history: createHistory(), ...options, }) } diff --git a/__tests__/guards/component-beforeRouteLeave.spec.ts b/__tests__/guards/component-beforeRouteLeave.spec.ts index aee7cca7..204ddf4d 100644 --- a/__tests__/guards/component-beforeRouteLeave.spec.ts +++ b/__tests__/guards/component-beforeRouteLeave.spec.ts @@ -1,14 +1,14 @@ -import { HTML5History } from '../../src/history/html5' -import { Router, RouterOptions } from '../../src/router' +import { RouterOptions, Router } from '../../src/router' import { NAVIGATION_TYPES, createDom, noGuard } from '../utils' import { RouteRecord } from '../../src/types' +import { createHistory } from '../../src' // TODO: refactor in utils function createRouter( options: Partial & { routes: RouteRecord[] } ) { return new Router({ - history: new HTML5History(), + history: createHistory(), ...options, }) } diff --git a/__tests__/guards/component-beforeRouteUpdate.spec.ts b/__tests__/guards/component-beforeRouteUpdate.spec.ts index 46172ba8..63625ca2 100644 --- a/__tests__/guards/component-beforeRouteUpdate.spec.ts +++ b/__tests__/guards/component-beforeRouteUpdate.spec.ts @@ -1,7 +1,7 @@ -import { HTML5History } from '../../src/history/html5' -import { Router } from '../../src/router' import fakePromise from 'faked-promise' import { NAVIGATION_TYPES, createDom, noGuard } from '../utils' +import { Router, createHistory } from '../../src' +import { RouteRecord } from '../../src/types' function createRouter( options: Partial & { @@ -9,7 +9,7 @@ function createRouter( } ) { return new Router({ - history: new HTML5History(), + history: createHistory(), ...options, }) } @@ -18,8 +18,7 @@ const Home = { template: `
Home
` } const Foo = { template: `
Foo
` } const beforeRouteUpdate = jest.fn() -/** @type {import('../../src/types').RouteRecord[]} */ -const routes = [ +const routes: RouteRecord[] = [ { path: '/', component: Home }, { path: '/foo', component: Foo }, { diff --git a/__tests__/guards/global-after.spec.ts b/__tests__/guards/global-after.spec.ts index 2a589401..5cd56e59 100644 --- a/__tests__/guards/global-after.spec.ts +++ b/__tests__/guards/global-after.spec.ts @@ -1,6 +1,5 @@ -import { HTML5History } from '../../src/history/html5' -import { Router } from '../../src/router' import { NAVIGATION_TYPES, createDom } from '../utils' +import { createHistory, Router } from '../../src' function createRouter( options: Partial & { @@ -8,7 +7,7 @@ function createRouter( } ) { return new Router({ - history: new HTML5History(), + history: createHistory(), ...options, }) } diff --git a/__tests__/guards/global-beforeEach.spec.ts b/__tests__/guards/global-beforeEach.spec.ts index c0e1b32c..eb35ae3f 100644 --- a/__tests__/guards/global-beforeEach.spec.ts +++ b/__tests__/guards/global-beforeEach.spec.ts @@ -1,14 +1,14 @@ -import { HTML5History } from '../../src/history/html5' -import { Router, RouterOptions } from '../../src/router' +import { RouterOptions } from '../../src/router' import fakePromise from 'faked-promise' import { NAVIGATION_TYPES, createDom, tick, noGuard } from '../utils' import { RouteRecord, RouteLocation } from '../../src/types' +import { createHistory, Router } from '../../src' function createRouter( options: Partial & { routes: RouteRecord[] } ) { return new Router({ - history: new HTML5History(), + history: createHistory(), ...options, }) } @@ -17,8 +17,7 @@ const Home = { template: `
Home
` } const Foo = { template: `
Foo
` } const Nested = { template: `
Nested
` } -/** @type {RouteRecord[]} */ -const routes = [ +const routes: RouteRecord[] = [ { path: '/', component: Home }, { path: '/foo', component: Foo }, { path: '/other', component: Foo }, diff --git a/__tests__/guards/route-beforeEnter.spec.ts b/__tests__/guards/route-beforeEnter.spec.ts index cee5dbf7..51e908ce 100644 --- a/__tests__/guards/route-beforeEnter.spec.ts +++ b/__tests__/guards/route-beforeEnter.spec.ts @@ -1,14 +1,14 @@ -import { HTML5History } from '../../src/history/html5' -import { Router, RouterOptions } from '../../src/router' +import { RouterOptions, Router } from '../../src/router' import fakePromise from 'faked-promise' import { NAVIGATION_TYPES, createDom, noGuard, tick } from '../utils' import { RouteRecord } from '../../src/types' +import { createHistory } from '../../src' function createRouter( options: Partial & { routes: RouteRecord[] } ) { return new Router({ - history: new HTML5History(), + history: createHistory(), ...options, }) } @@ -28,8 +28,7 @@ const nested = { nestedNestedParam: jest.fn(), } -/** @type {RouteRecord[]} */ -const routes = [ +const routes: RouteRecord[] = [ { path: '/', component: Home }, { path: '/home', component: Home, beforeEnter }, { path: '/foo', component: Foo }, diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index 619861df..60d326a3 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -1,8 +1,7 @@ import fakePromise from 'faked-promise' -import { AbstractHistory } from '../src/history/abstract' -import { Router } from '../src/router' +import { Router, createMemoryHistory } from '../src' import { NavigationCancelled } from '../src/errors' -import { createDom, components, tick, HistoryMock } from './utils' +import { createDom, components, tick } from './utils' import { RouteRecord, RouteLocation } from '../src/types' const routes: RouteRecord[] = [ @@ -30,7 +29,7 @@ describe('Router', () => { }) it('can be instantiated', () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) expect(router.currentRoute).toEqual({ name: undefined, @@ -45,7 +44,8 @@ describe('Router', () => { // TODO: should do other checks not based on history implem it.skip('takes browser location', () => { - const history = new HistoryMock('/search?q=dog#footer') + const history = createMemoryHistory() + history.replace('/search?q=dog#footer') const router = new Router({ history, routes }) expect(router.currentRoute).toEqual({ fullPath: '/search?q=dog#footer', @@ -57,7 +57,7 @@ describe('Router', () => { }) it('calls history.push with router.push', async () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) jest.spyOn(history, 'push') await router.push('/foo') @@ -71,7 +71,7 @@ describe('Router', () => { }) it('calls history.replace with router.replace', async () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) jest.spyOn(history, 'replace') await router.replace('/foo') @@ -85,7 +85,7 @@ describe('Router', () => { }) it('can pass replace option to push', async () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) jest.spyOn(history, 'replace') await router.push({ path: '/foo', replace: true }) @@ -104,7 +104,7 @@ describe('Router', () => { ) { const [p1, r1] = fakePromise() const [p2, r2] = fakePromise() - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) router.beforeEach(async (to, from, next) => { if (to.name !== 'Param') return next() @@ -151,7 +151,7 @@ describe('Router', () => { ) { const [p1, r1] = fakePromise() const [p2, r2] = fakePromise() - const history = new AbstractHistory() + const history = createMemoryHistory() const router = new Router({ history, routes }) // navigate first to add entries to the history stack await router.push('/foo') @@ -203,7 +203,7 @@ describe('Router', () => { describe('matcher', () => { it('handles one redirect from route record', async () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) const loc = await router.push('/to-foo') expect(loc.name).toBe('Foo') @@ -213,7 +213,7 @@ describe('Router', () => { }) it('drops query and params on redirect if not provided', async () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) const loc = await router.push('/to-foo?hey=foo#fa') expect(loc.name).toBe('Foo') @@ -225,7 +225,7 @@ describe('Router', () => { }) it('allows object in redirect', async () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) const loc = await router.push('/to-foo-named') expect(loc.name).toBe('Foo') @@ -235,7 +235,7 @@ describe('Router', () => { }) it('can pass on query and hash when redirecting', async () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) const loc = await router.push('/inc-query-hash?n=3#fa') expect(loc).toMatchObject({ @@ -252,7 +252,7 @@ describe('Router', () => { }) it('handles multiple redirect fields in route record', async () => { - const history = new HistoryMock() + const history = createMemoryHistory() const router = new Router({ history, routes }) const loc = await router.push('/to-foo2') expect(loc.name).toBe('Foo') diff --git a/__tests__/ssr/shared.ts b/__tests__/ssr/shared.ts index b23b16b5..d0d80b83 100644 --- a/__tests__/ssr/shared.ts +++ b/__tests__/ssr/shared.ts @@ -1,18 +1,18 @@ import Vue, { ComponentOptions } from 'vue' -import Router from '../../src' +import { Router, createMemoryHistory, plugin } from '../../src' import { components } from '../utils' import { createRenderer } from 'vue-server-renderer' import { RouterOptions } from '../../src/router' -Vue.use(Router) +Vue.use(plugin) export const renderer = createRenderer() export function createRouter(options?: Partial) { // TODO: a more complex routing that can be used for most tests return new Router({ - mode: 'history', + history: createMemoryHistory(), routes: [ { path: '/', @@ -57,7 +57,9 @@ export function renderApp( const { app, router } = createApp(routerOptions, vueOptions) // set server-side router's location - router.push(context.url).catch(err => {}) + router.push(context.url).catch(err => { + console.error('ssr push failed', err) + }) // wait until router has resolved possible async components and hooks // TODO: rename the promise one to isReady diff --git a/__tests__/url-encoding.spec.ts b/__tests__/url-encoding.spec.ts index 4556010a..ca39a2a8 100644 --- a/__tests__/url-encoding.spec.ts +++ b/__tests__/url-encoding.spec.ts @@ -1,6 +1,7 @@ import { Router } from '../src/router' -import { createDom, components, HistoryMock } from './utils' +import { createDom, components } from './utils' import { RouteRecord } from '../src/types' +import { createMemoryHistory } from '../src' const routes: RouteRecord[] = [ { path: '/', name: 'home', component: components.Home }, @@ -11,8 +12,9 @@ const routes: RouteRecord[] = [ // this function is meant to easy refactor in the future as Histories are going to be // function-based -function createHistory(...args: ConstructorParameters) { - return new HistoryMock(...args) +function createHistory() { + const routerHistory = createMemoryHistory() + return routerHistory } describe('URL Encoding', () => { @@ -22,9 +24,9 @@ describe('URL Encoding', () => { describe('initial navigation', () => { it('decodes path', async () => { - const history = createHistory('/%25') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.replace('/%25') expect(router.currentRoute).toEqual( expect.objectContaining({ name: 'percent', @@ -36,9 +38,9 @@ describe('URL Encoding', () => { it('decodes params in path', async () => { // /p/€ - const history = createHistory('/p/%E2%82%AC') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.push('/p/%E2%82%AC') expect(router.currentRoute).toEqual( expect.objectContaining({ name: 'params', @@ -50,9 +52,9 @@ describe('URL Encoding', () => { }) it('allows navigating to valid unencoded params (IE and Edge)', async () => { - const history = createHistory('/p/€') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.push('/p/€') expect(router.currentRoute).toEqual( expect.objectContaining({ name: 'params', @@ -67,9 +69,9 @@ describe('URL Encoding', () => { it('allows navigating to invalid unencoded params (IE and Edge)', async () => { const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}) - const history = createHistory('/p/%notvalid') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.push('/p/%notvalid') expect(spy).toHaveBeenCalledTimes(1) spy.mockRestore() expect(router.currentRoute).toEqual( @@ -85,9 +87,9 @@ describe('URL Encoding', () => { }) it('decodes params in query', async () => { - const history = createHistory('/?q=%25%E2%82%AC') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.push('/?q=%25%E2%82%AC') expect(router.currentRoute).toEqual( expect.objectContaining({ name: 'home', @@ -101,9 +103,9 @@ describe('URL Encoding', () => { }) it('decodes params keys in query', async () => { - const history = createHistory('/?%E2%82%AC=euro') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.push('/?%E2%82%AC=euro') expect(router.currentRoute).toEqual( expect.objectContaining({ name: 'home', @@ -118,9 +120,9 @@ describe('URL Encoding', () => { it('allow unencoded params in query (IE Edge)', async () => { const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}) - const history = createHistory('/?q=€%notvalid') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.push('/?q=€%notvalid') expect(spy).toHaveBeenCalledTimes(1) spy.mockRestore() expect(router.currentRoute).toEqual( @@ -139,9 +141,9 @@ describe('URL Encoding', () => { // should we do it? it seems to be a bit different as it allows using % without // encoding it. To be safe we would have to encode everything it.skip('decodes hash', async () => { - const history = createHistory('/#%25%E2%82%AC') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.push('/#%25%E2%82%AC') expect(router.currentRoute).toEqual( expect.objectContaining({ name: 'home', @@ -154,9 +156,9 @@ describe('URL Encoding', () => { it('allow unencoded params in query (IE Edge)', async () => { const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}) - const history = createHistory('/?q=€%notvalid') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() + await router.push('/?q=€%notvalid') expect(spy).toHaveBeenCalledTimes(1) spy.mockRestore() expect(router.currentRoute).toEqual( @@ -174,9 +176,8 @@ describe('URL Encoding', () => { describe('resolving locations', () => { it('encodes params when resolving', async () => { - const history = createHistory('/') + const history = createHistory() const router = new Router({ history, routes }) - await router.doInitialNavigation() await router.push({ name: 'params', params: { p: '%€' } }) expect(router.currentRoute).toEqual( expect.objectContaining({