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 = [
})
it('can be instantiated', () => {
- const history = mockHistory()
+ const history = new HistoryMock()
const router = new Router({ history, routes })
expect(router.currentRoute).toEqual({
fullPath: '/',
// 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',
})
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')
})
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')
})
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 })
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()
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')
})
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')
})
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')
})
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({
})
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')