Skip to content

Commit fb21b9a

Browse files
committed
feat: wip
1 parent 6137c49 commit fb21b9a

File tree

5 files changed

+99
-20
lines changed

5 files changed

+99
-20
lines changed

__tests__/fixtures/Test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineComponent, h, inject } from 'vue'
2+
import { routeLocationKey, useRoute, useRouter } from 'vue-router'
3+
4+
export default defineComponent({
5+
name: 'Test',
6+
7+
setup() {
8+
const foo = inject('foo')!
9+
const route = useRoute()
10+
const router = useRouter()
11+
12+
return { route, router, foo }
13+
},
14+
15+
render() {
16+
return h('p', this.route.fullPath)
17+
// return h('p', this.$r.fullPath)
18+
},
19+
})

__tests__/index.spec.ts

+48-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
1-
import { mylib, Component } from '../src'
2-
import { mount } from '@vue/test-utils'
1+
import { routeLocationKey, routerKey } from '../src'
2+
import { config, mount } from '@vue/test-utils'
3+
import Test from './fixtures/Test'
4+
import { RouterLink } from 'vue-router'
5+
// import { createRouter, createMemoryHistory } from "vue-router";
36

4-
describe('mylib', () => {
5-
it('works', () => {
6-
expect(mylib()).toBe(true)
7-
})
8-
})
7+
// const router = createRouter({
8+
// history: createMemoryHistory(),
9+
// routes: [{
10+
// path: '/:pathMatch(.*)*',
11+
// component: { render: () => null }
12+
// }]
13+
// })
14+
15+
config.global.components = config.global.components || {}
16+
config.global.components.RouterLink = RouterLink
917

1018
describe('Component', () => {
19+
const routerMock = {
20+
push: jest.fn(),
21+
}
22+
const routeMock = {
23+
fullPath: 'fullPath',
24+
}
25+
26+
beforeAll(() => {
27+
// @ts-ignore
28+
config.global.provide = { foo: 'foo' }
29+
// shouldn't this be always present for convenience?
30+
// record string is not enough
31+
config.global.provide![routerKey as any] = routerMock
32+
config.global.provide![routeLocationKey as any] = routeMock
33+
})
34+
1135
it('works', async () => {
12-
const wrapper = mount(Component, {
13-
props: { data: { title: 'hi', summary: 'summary' } },
36+
const wrapper = mount(Test, {
37+
// initial location
38+
// route: '/',
39+
// router: false,
1440
})
15-
expect(wrapper.html()).toMatchInlineSnapshot(
16-
`"<p>Custom: false. hi - summary.</p>"`
17-
)
41+
42+
// await wrapper.pendingNavigation()
43+
44+
expect(wrapper.vm.foo).toBe('foo')
45+
46+
expect(wrapper.text()).toBe(`fullPath`)
47+
expect(wrapper.vm.router).toBe(routerMock)
48+
expect(wrapper.vm.$router).toBe(routerMock)
49+
50+
// sugar
51+
// expect('/url').toBeCurrentRoute()
52+
// expect('/url').toBeCurrentRoute()
53+
// expect('/url').toHaveBeenRedirectedTo()
1854
})
1955
})

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@size-limit/preset-small-lib": "^4.7.0",
4242
"@types/jest": "^26.0.15",
4343
"@types/jsdom": "^16.2.5",
44-
"@vue/test-utils": "^2.0.0-beta.6",
44+
"@vue/test-utils": "^2.0.0-beta.8",
4545
"codecov": "^3.8.1",
4646
"conventional-changelog-cli": "^2.1.1",
4747
"jest": "^26.5.3",
@@ -55,6 +55,7 @@
5555
"ts-jest": "^26.4.1",
5656
"typescript": "^4.0.5",
5757
"vue": "^3.0.2",
58+
"vue-router": "^4.0.0-rc.1",
5859
"yorkie": "^2.0.0"
5960
},
6061
"gitHooks": {
@@ -75,7 +76,8 @@
7576
}
7677
],
7778
"peerDependencies": {
78-
"vue": "^3.0.0-beta.20"
79+
"vue": "^3.0.0",
80+
"vue-router": "^4.0.0-rc.1"
7981
},
8082
"repository": {
8183
"type": "git",

src/index.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import { defineComponent, PropType, h } from 'vue'
2+
import { createRouter, createWebHashHistory } from 'vue-router'
3+
export { routeLocationKey, routerKey } from 'vue-router'
24

3-
/**
4-
* Returns true.
5-
*/
6-
export function mylib() {
7-
return true
5+
export function createMockedRouter() {
6+
const router = createRouter({
7+
history: createWebHashHistory(),
8+
routes: [
9+
{
10+
path: '/:pathMatch(.*)*',
11+
component: { render: () => null },
12+
},
13+
],
14+
})
15+
16+
router.push = jest.fn((to) => {
17+
router.currentRoute.value = router.resolve(to)
18+
// resolve pending navigation failure
19+
return Promise.resolve()
20+
})
21+
22+
// TODO: replace
23+
24+
return router
825
}
926

1027
export const Component = defineComponent({

yarn.lock

+6-1
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@
857857
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.2.tgz#419bd85a2ebdbd4f42963e98c5a1b103452176d9"
858858
integrity sha512-Zx869zlNoujFOclKIoYmkh8ES2RcS/+Jn546yOiPyZ+3+Ejivnr+fb8l+DdXUEFjo+iVDNR3KyLzg03aBFfZ4Q==
859859

860-
"@vue/test-utils@^2.0.0-beta.6":
860+
"@vue/test-utils@^2.0.0-beta.8":
861861
version "2.0.0-beta.8"
862862
resolved "https://registry.yarnpkg.com/@vue/test-utils/-/test-utils-2.0.0-beta.8.tgz#13be1a31f4d6d396e2a9d595d4e0f236421eba98"
863863
integrity sha512-ksQYCzBINZy9iSb9jCo3AbfTEQ2Xw7NEyGA07frqNZ3tHG4pBnnRrTi+zoS7wi89f41eyx2zVhAF/mvzDKNG2A==
@@ -8092,6 +8092,11 @@ vm-browserify@^1.0.1:
80928092
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"
80938093
integrity sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==
80948094

8095+
vue-router@^4.0.0-rc.1:
8096+
version "4.0.0-rc.2"
8097+
resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-4.0.0-rc.2.tgz#8545cab76a05ca4f6dffbe6c6a671a4dbf585ab2"
8098+
integrity sha512-51mBp39rzBFpk1nyU9SkhPcwR67gBzWIH8p3pyeDmtNYgWzGF3q8MneD/xbMwsfTQkw2H1qBk6uwRaVy3M8Nxw==
8099+
80958100
vue@^3.0.2:
80968101
version "3.0.2"
80978102
resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.2.tgz#9d5b7b2983f35e64a34d13c7c9d6831239feca3c"

0 commit comments

Comments
 (0)