function factory(currentLocation, propsData, resolvedLocation) {
const router = {
history: new HistoryMock(),
- resolveLocation: jest.fn(),
+ resolve: jest.fn(),
push: jest.fn(),
}
- router.resolveLocation.mockReturnValueOnce(resolvedLocation)
+ router.resolve.mockReturnValueOnce(resolvedLocation)
// @ts-ignore TODO: Some information are missing on RouterLink
const wrapper = mount(RouterLink, {
propsData,
{ to: locations.basic.string },
locations.basic.normalized
)
- expect(router.resolveLocation).toHaveBeenCalledTimes(1)
- expect(router.resolveLocation).toHaveBeenCalledWith(
- expect.objectContaining({ path: locations.basic.string }),
- START_LOCATION_NORMALIZED
- )
+ expect(router.resolve).toHaveBeenCalledTimes(1)
+ expect(router.resolve).toHaveBeenCalledWith(locations.basic.string)
})
it('calls router.push when clicked', () => {
const { router } = factory(
START_LOCATION_NORMALIZED,
{ to: locations.withQuery.string },
- locations.withQuery.normalized // it doesn't matter as we want to check what resolveLocation is called with
- )
- expect(router.resolveLocation).toHaveBeenCalledWith(
- expect.objectContaining({ query: locations.withQuery.normalized.query }),
- START_LOCATION_NORMALIZED
+ locations.withQuery.normalized // it doesn't matter as we want to check what resolve is called with
)
+ expect(router.resolve).toHaveBeenCalledWith(locations.withQuery.string)
})
})
import { Component } from 'vue'
import { Router } from '../router'
import { RouteLocationNormalized, RouteLocation } from '../types'
-import { HistoryLocationNormalized } from '../history/base'
const Link: Component = {
name: 'RouterLink',
// @ts-ignore can't get `this`
const to = this.to as RouteLocation
- // @ts-ignore can't get `this`
- const history = router.history
- let url: HistoryLocationNormalized
- let location: RouteLocationNormalized
- // TODO: refactor router code and use its function istead of having a copied version here
- if (typeof to === 'string' || ('path' in to && !('name' in to))) {
- url = history.utils.normalizeLocation(to)
- // TODO: should allow a non matching url to allow dynamic routing to work
- location = router.resolveLocation(url, from)
- } else {
- // named or relative route
- const query = history.utils.normalizeQuery(to.query ? to.query : {})
- const hash = to.hash || ''
- // we need to resolve first
- location = router.resolveLocation({ ...to, query, hash }, from)
- // intentionally drop current query and hash
- url = history.utils.normalizeLocation({
- query,
- hash,
- ...location,
- })
- }
- const route = location
+ const route = router.resolve(to)
// TODO: active classes
// TODO: handle replace prop
})
}
- resolveLocation(
+ private resolveLocation(
location: MatcherLocation & Required<RouteQueryAndHash>,
currentLocation?: RouteLocationNormalized,
redirectedFrom?: RouteLocationNormalized