} from '../src/types'
import { createMemoryHistory } from '../src'
import { mount, tick } from './mount'
-import { ref, markNonReactive } from 'vue'
+import { ref, markNonReactive, isRef } from 'vue'
const locations: Record<
string,
name: undefined,
},
},
+ foo: {
+ string: '/foo',
+ // toResolve: { path: '/home', fullPath: '/home', undefined, query: {}, hash: '' },
+ normalized: {
+ fullPath: '/foo',
+ path: '/foo',
+ params: {},
+ meta: {},
+ query: {},
+ hash: '',
+ matched: [],
+ name: undefined,
+ },
+ },
withQuery: {
string: '/home?foo=a&bar=b',
// toResolve: { path: '/home', fullPath: '/home', undefined, query: {}, hash: '' },
template: `<RouterLink :to="to">a link</RouterLink>`,
components: { RouterLink } as any,
setup() {
- const to = ref(propsData.to)
+ const to = isRef(propsData.to) ? propsData.to : ref(propsData.to)
return { to }
},
expect(el.innerHTML).toBe('<a class="" href="/home">a link</a>')
})
+ it('can change the value', async () => {
+ const to = ref(locations.basic.string)
+ const { el, router } = factory(
+ START_LOCATION_NORMALIZED,
+ { to },
+ locations.basic.normalized
+ )
+ expect(el.innerHTML).toBe('<a class="" href="/home">a link</a>')
+ router.resolve.mockReturnValueOnce(locations.foo.normalized)
+ to.value = locations.foo.string
+ await tick()
+ expect(el.innerHTML).toBe('<a class="" href="/foo">a link</a>')
+ })
+
it('displays a link with an object with path prop', () => {
const { el } = factory(
START_LOCATION_NORMALIZED,
import { computed, reactive, isRef, Ref } from '@vue/reactivity'
import { RouteLocation } from '../types'
-export function useLink(to: Ref<RouteLocation> | RouteLocation) {
+interface UseLinkProps {
+ to: Ref<RouteLocation> | RouteLocation
+ replace?: Ref<boolean> | boolean
+}
+
+export function useLink(props: UseLinkProps) {
const router = inject('router')
- const route = computed(() => router.resolve(isRef(to) ? to.value : to))
+ const route = computed(() =>
+ router.resolve(isRef(props.to) ? props.to.value : props.to)
+ )
const href = computed(() => router.createHref(route.value))
const isActive = computed<boolean>(
() => router.currentRoute.value.path.indexOf(route.value.path) === 0
// TODO: handle replace prop
- function navigate(e: MouseEvent) {
+ function navigate(e: MouseEvent = {} as MouseEvent) {
// TODO: handle navigate with empty parameters for scoped slot and composition api
if (guardEvent(e)) router.push(route.value)
}
},
setup(props, { slots, attrs }) {
- const { route, isActive, href, navigate } = useLink(props.to)
+ const { route, isActive, href, navigate } = useLink(props)
const elClass = computed(() => ({
'router-link-active': isActive.value,