createDom()
})
- it('can be instantiated', () => {
+ it('starts at START_LOCATION', () => {
const history = createMemoryHistory()
const router = createRouter({ history, routes })
expect(router.currentRoute.value).toEqual(START_LOCATION_NORMALIZED)
)
})
+ it('calls history.replace with router.replace', async () => {
+ const history = createMemoryHistory()
+ const { router } = await newRouter({ history })
+ jest.spyOn(history, 'replace')
+ await router.replace('/foo')
+ expect(history.replace).toHaveBeenCalledTimes(1)
+ expect(history.replace).toHaveBeenCalledWith(
+ expect.objectContaining({
+ fullPath: '/foo',
+ path: '/foo',
+ query: {},
+ hash: '',
+ }),
+ undefined
+ )
+ })
+
it('allows to customize parseQuery', async () => {
const parseQuery = jest.fn()
const { router } = await newRouter({ parseQuery })
expect(router.currentRoute.value).not.toBe(START_LOCATION_NORMALIZED)
})
- it('calls history.replace with router.replace', async () => {
- const history = createMemoryHistory()
- const { router } = await newRouter({ history })
- jest.spyOn(history, 'replace')
- await router.replace('/foo')
- expect(history.replace).toHaveBeenCalledTimes(1)
- expect(history.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- fullPath: '/foo',
- path: '/foo',
- query: {},
- hash: '',
- }),
- undefined
- )
- })
-
it('can pass replace option to push', async () => {
const { router, history } = await newRouter()
jest.spyOn(history, 'replace')
const spy = jest.fn((to, from, next) => next())
router.beforeEach(spy)
await router.push('/idontexist')
- expect(spy).toHaveBeenCalled()
+ expect(spy).toHaveBeenCalledTimes(1)
expect(router.currentRoute.value).toMatchObject({ matched: [] })
spy.mockClear()
await router.push('/me-neither')
expect(router.currentRoute.value).toMatchObject({ matched: [] })
- expect(spy).toHaveBeenCalled()
+ expect(spy).toHaveBeenCalledTimes(1)
})
it('navigates to same route record but different query', async () => {
})
})
- describe('navigation', () => {
+ describe('navigation cancelled', () => {
async function checkNavigationCancelledOnPush(
target?: RouteLocationRaw | false | ((vm: any) => void)
) {
import { createRouter as newRouter } from '../src/router'
-import { createDom, components } from './utils'
+import { components } from './utils'
import { RouteRecordRaw } from '../src/types'
import { createMemoryHistory } from '../src'
import * as encoding from '../src/utils/encoding'
{ path: '/p/:p+', component: components.Bar, name: 'repeat' },
]
-// this function is meant to easy refactor in the future as Histories are going to be
-// function-based
-function createWebHistory() {
- const routerHistory = createMemoryHistory()
- return routerHistory
-}
-
function createRouter() {
- const history = createWebHistory()
+ const history = createMemoryHistory()
const router = newRouter({ history, routes })
return router
}
-// TODO: test by spying on encode functions since things are already tested by encoding.spec.ts
describe('URL Encoding', () => {
- beforeAll(() => {
- createDom()
- })
-
beforeEach(() => {
// mock all encoding functions
for (const key in encoding) {