generated from posva/vue-ts-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
injections.ts
72 lines (62 loc) · 2.03 KB
/
injections.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {
matchedRouteKey,
routeLocationKey,
RouteLocationNormalizedLoaded,
routerKey,
RouterLink,
RouterView,
routerViewLocationKey,
// @ts-ignore: for api-extractor
RouteLocationMatched,
} from 'vue-router'
import { config } from '@vue/test-utils'
import { createReactiveRouteLocation } from './routeLocation'
import { createRouterMock, RouterMock } from './router'
// @ts-ignore: for api-extractor
import { computed, Ref, ComputedRef } from 'vue'
/**
* Inject global variables, overriding any previously inject router mock
*
* @param router - router mock to inject
*/
export function injectRouterMock(router?: RouterMock) {
router = router || createRouterMock()
const provides = createProvide(router)
const route = provides[
routeLocationKey as any
] as RouteLocationNormalizedLoaded
Object.assign(config.global.provide, provides)
config.global.mocks.$router = router
config.global.mocks.$route = route
// TODO: stub that provides the prop route or the current route with matchedRouteKey
config.global.components.RouterView = RouterView
config.global.components.RouterLink = RouterLink
// TODO: remove if https://github.com/vuejs/vue-test-utils-next/issues/1023
// gets merged
if (Array.isArray(config.global.stubs)) {
config.global.stubs.push('RouterLink')
config.global.stubs.push('RouterView')
} else {
config.global.stubs.RouterLink = true
config.global.stubs.RouterView = true
}
return { router, route }
}
/**
* Creates an object of properties to be provided at your application level to
* mock what is injected by vue-router
*
* @param router - router mock instance
*/
export function createProvide(router: RouterMock) {
const route = createReactiveRouteLocation(router.currentRoute)
const matchedRouteRef = computed(
() => router.currentRoute.value.matched[router.depth.value]
)
return {
[routerKey as any]: router,
[routeLocationKey as any]: route,
[routerViewLocationKey as any]: router.currentRoute,
[matchedRouteKey as any]: matchedRouteRef,
}
}