Skip to content

Commit 896ea16

Browse files
committed
feat: wip vue plugin version
1 parent 6f9f41d commit 896ea16

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
> Easily mock routing interactions in your Vue 3 apps
44
5-
**⚠️ This library intends to be a collaboration of people writing tests to create a better experience writing tests that involve the use of routing with Vue.** Your feedback and experience is welcomed in issues and discussions to give the API shape and create a library that eases unit testing components that deal with the router.
6-
75
## Installation
86

97
```sh
10-
yarn add vue-router-mock
8+
pnpm i -D vue-router-mock
9+
# or
10+
yarn add -D vue-router-mock
1111
# or
12-
npm install vue-router-mock
12+
npm install -D vue-router-mock
1313
```
1414

1515
## Requirements
1616

1717
This library
1818

19-
- vue test utils next
19+
- `@vue/test-utils` >= 2.4.0
2020
- vue 3 and vue router 4
2121

2222
## Goal

src/injections.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ import {
66
RouterLink,
77
RouterView,
88
routerViewLocationKey,
9-
// @ts-ignore: for api-extractor
10-
RouteLocationMatched,
119
} from 'vue-router'
1210
import { config } from '@vue/test-utils'
1311
import { createReactiveRouteLocation } from './routeLocation'
1412
import { createRouterMock, RouterMock } from './router'
15-
// @ts-ignore: for api-extractor
16-
import { computed, Ref, ComputedRef } from 'vue'
13+
import { computed, Plugin } from 'vue'
1714

1815
/**
1916
* Inject global variables, overriding any previously inject router mock
@@ -37,19 +34,28 @@ export function injectRouterMock(router?: RouterMock) {
3734
config.global.components.RouterView = RouterView
3835
config.global.components.RouterLink = RouterLink
3936

40-
// TODO: remove if https://github.com/vuejs/vue-test-utils-next/issues/1023
41-
// gets merged
42-
if (Array.isArray(config.global.stubs)) {
43-
config.global.stubs.push('RouterLink')
44-
config.global.stubs.push('RouterView')
45-
} else {
46-
config.global.stubs.RouterLink = true
47-
config.global.stubs.RouterView = true
48-
}
37+
config.global.stubs.RouterLink = true
38+
config.global.stubs.RouterView = true
4939

5040
return { router, route }
5141
}
5242

43+
// TODO: explore this idea rather than having the weird inject function
44+
export function createPlugin(router: RouterMock): Plugin {
45+
return (app) => {
46+
const provides = createProvide(router)
47+
const route = provides[
48+
routeLocationKey as any
49+
] as RouteLocationNormalizedLoaded
50+
51+
for (const key in provides) {
52+
app.provide(key, provides[key])
53+
}
54+
app.config.globalProperties.$router = router
55+
app.config.globalProperties.$route = route
56+
}
57+
}
58+
5359
/**
5460
* Creates an object of properties to be provided at your application level to
5561
* mock what is injected by vue-router

0 commit comments

Comments
 (0)