RouteLocation,
} from '../src/types'
import { createMemoryHistory } from '../src'
-import { mount } from './mount'
-import { ref, markNonReactive, nextTick } from 'vue'
+import { mount, createMockedRoute } from './mount'
+import { nextTick } from 'vue'
import { RouteRecordNormalized } from '../src/matcher/types'
import { routerKey } from '../src/utils/injectionSymbols'
resolvedLocation: RouteLocation,
slotTemplate: string = ''
) {
- // const route = createMockedRoute(initialRoute)
+ const route = createMockedRoute(currentLocation)
const router = {
history: createMemoryHistory(),
createHref(to: RouteLocationNormalized): string {
},
resolve: jest.fn(),
push: jest.fn().mockResolvedValue(resolvedLocation),
- currentRoute: ref(markNonReactive(currentLocation)),
}
router.resolve.mockReturnValueOnce(resolvedLocation)
propsData,
provide: {
[routerKey as any]: router,
+ ...route.provides,
},
slots: { default: slotTemplate },
})
- return { router, wrapper }
+ return { router, wrapper, route }
}
describe('RouterLink', () => {
})
it('can change the value', async () => {
- const to = ref(locations.basic.string)
const { wrapper, router } = await factory(
START_LOCATION_NORMALIZED,
- { to },
+ { to: locations.basic.string },
locations.basic.normalized
)
expect(wrapper.rootEl.querySelector('a')!.getAttribute('href')).toBe(
'/home'
)
router.resolve.mockReturnValueOnce(locations.foo.normalized)
- to.value = locations.foo.string
- await nextTick()
+ await wrapper.setProps({ to: locations.foo.string })
expect(wrapper.rootEl.querySelector('a')!.getAttribute('href')).toBe('/foo')
})
_RouteRecordBase,
RouteComponent,
RouteRecordRaw,
+ RouteRecordName,
} from '../src/types'
import { h, resolveComponent, ComponentOptions } from 'vue'
import { RouterOptions, createWebHistory, createRouter } from '../src'
// @ts-ignore we are intentionally overriding the type
export interface RouteLocationNormalizedLoose extends RouteLocationNormalized {
- name: string | undefined
+ name: RouteRecordName | null | undefined
path: string
// record?
params: any
} from 'vue'
import { RouteLocationRaw, VueUseOptions, RouteLocation } from '../types'
import { isSameLocationObject, isSameRouteRecord } from '../utils'
-import { routerKey } from '../utils/injectionSymbols'
+import { routerKey, routeLocationKey } from '../utils/injectionSymbols'
import { RouteRecord } from '../matcher/types'
interface LinkProps {
// `isExactActive` behavior should go through an RFC
export function useLink(props: UseLinkOptions) {
const router = inject(routerKey)!
- const currentRoute = router.currentRoute
+ const currentRoute = inject(routeLocationKey)!
const route = computed(() => router.resolve(unref(props.to)))
const href = computed(() => router.createHref(route.value))
const currentMatched: RouteRecord | undefined =
route.value.matched[route.value.matched.length - 1]
if (!currentMatched) return -1
- return currentRoute.value.matched.findIndex(
+ return currentRoute.matched.findIndex(
isSameRouteRecord.bind(null, currentMatched)
)
})
const isActive = computed<boolean>(
() =>
activeRecordIndex.value > -1 &&
- includesParams(currentRoute.value.params, route.value.params)
+ includesParams(currentRoute.params, route.value.params)
)
const isExactActive = computed<boolean>(
() =>
activeRecordIndex.value > -1 &&
- activeRecordIndex.value === currentRoute.value.matched.length - 1 &&
- isSameLocationObject(currentRoute.value.params, route.value.params)
+ activeRecordIndex.value === currentRoute.matched.length - 1 &&
+ isSameLocationObject(currentRoute.params, route.value.params)
)
// TODO: handle replace prop