-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'
const onError = jest.fn()
function createRouter() {
- const history = new AbstractHistory()
+ const history = createMemoryHistory()
const router = new Router({
history,
routes,
-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<RouterOptions> & { routes: RouteRecord[] }
) {
return new Router({
- history: new HTML5History(),
+ history: createHistory(),
...options,
})
}
-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<RouterOptions> & { routes: RouteRecord[] }
) {
return new Router({
- history: new HTML5History(),
+ history: createHistory(),
...options,
})
}
-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<import('../../src/router').RouterOptions> & {
}
) {
return new Router({
- history: new HTML5History(),
+ history: createHistory(),
...options,
})
}
const Foo = { template: `<div>Foo</div>` }
const beforeRouteUpdate = jest.fn()
-/** @type {import('../../src/types').RouteRecord[]} */
-const routes = [
+const routes: RouteRecord[] = [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{
-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<import('../../src/router').RouterOptions> & {
}
) {
return new Router({
- history: new HTML5History(),
+ history: createHistory(),
...options,
})
}
-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<RouterOptions> & { routes: RouteRecord[] }
) {
return new Router({
- history: new HTML5History(),
+ history: createHistory(),
...options,
})
}
const Foo = { template: `<div>Foo</div>` }
const Nested = { template: `<div>Nested<router-view/></div>` }
-/** @type {RouteRecord[]} */
-const routes = [
+const routes: RouteRecord[] = [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{ path: '/other', component: Foo },
-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<RouterOptions> & { routes: RouteRecord[] }
) {
return new Router({
- history: new HTML5History(),
+ history: createHistory(),
...options,
})
}
nestedNestedParam: jest.fn(),
}
-/** @type {RouteRecord[]} */
-const routes = [
+const routes: RouteRecord[] = [
{ path: '/', component: Home },
{ path: '/home', component: Home, beforeEnter },
{ path: '/foo', component: Foo },
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[] = [
})
it('can be instantiated', () => {
- const history = new HistoryMock()
+ const history = createMemoryHistory()
const router = new Router({ history, routes })
expect(router.currentRoute).toEqual({
name: undefined,
// 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',
})
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')
})
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')
})
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 })
) {
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()
) {
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')
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')
})
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')
})
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')
})
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({
})
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')
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<RouterOptions>) {
// TODO: a more complex routing that can be used for most tests
return new Router({
- mode: 'history',
+ history: createMemoryHistory(),
routes: [
{
path: '/',
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
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 },
// this function is meant to easy refactor in the future as Histories are going to be
// function-based
-function createHistory(...args: ConstructorParameters<typeof HistoryMock>) {
- return new HistoryMock(...args)
+function createHistory() {
+ const routerHistory = createMemoryHistory()
+ return routerHistory
}
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',
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',
})
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',
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(
})
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',
})
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',
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(
// 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',
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(
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({