-
Notifications
You must be signed in to change notification settings - Fork 233
/
mocks.spec.ts
129 lines (117 loc) · 3.11 KB
/
mocks.spec.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { describe, expect, it, vi } from 'vitest'
import { mount, RouterLinkStub } from '../../src'
import { defineComponent } from 'vue'
import ScriptSetupWithI18n from '../components/ScriptSetupWithI18n.vue'
import ComponentWithI18n from '../components/ComponentWithI18n.vue'
describe('mocks', () => {
it('mocks a vuex store', async () => {
const Foo = {
template: `
<div>
count: {{ $store.state.count }}
<button @click="$store.dispatch('inc')" />
</div>
`
}
const $store = {
state: {
count: 1
},
dispatch: vi.fn()
}
const wrapper = mount(Foo, {
global: {
mocks: { $store }
}
})
expect(wrapper.html()).toContain('count: 1')
await wrapper.find('button').trigger('click')
expect($store.dispatch).toHaveBeenCalledWith('inc')
})
it('mocks vue-router', async () => {
const Foo = defineComponent({
template: `
<div>
<RouterLink :to="url">Go to post: {{ id }}</RouterLink>
<button @click="submit">Go</button>
</div>
`,
computed: {
url(): string {
return `/posts/${this.$route.params.id}`
},
id(): string | string[] {
return this.$route.params.id
}
},
methods: {
submit() {
this.$router.push(`/posts/${this.id}`)
}
}
})
const $router = {
push: vi.fn()
}
const $route = {
params: {
id: 1
}
}
const wrapper = mount(Foo, {
global: {
components: {
RouterLink: RouterLinkStub
},
mocks: { $route, $router }
}
})
expect(wrapper.html()).toContain('Go to post: 1')
await wrapper.find('button').trigger('click')
expect($router.push).toHaveBeenCalledWith('/posts/1')
})
it('mocks a global function in a script setup component', () => {
const wrapper = mount(ScriptSetupWithI18n, {
global: {
mocks: {
$t: () => 'mocked'
}
}
})
expect(wrapper.text()).toContain('hello')
expect(wrapper.text()).toContain('mocked')
})
it('mocks a global function in an option component', () => {
const wrapper = mount(ComponentWithI18n, {
global: {
mocks: {
$t: () => 'mocked'
}
}
})
expect(wrapper.text()).toContain('hello')
expect(wrapper.text()).toContain('mocked')
})
it('mocks a global function in an option component which includes the setup() option', () => {
const ComponentWithI18nAndSetupOption = defineComponent({
setup: () => ({
hello: 'hello'
}),
template: `
<div>{{ hello }}</div>
<!-- this emulates components that use a global function like $t for i18n -->
<!-- this function can be mocked using global.mocks -->
<div>{{ $t('world') }}</div>
`
})
const wrapper = mount(ComponentWithI18nAndSetupOption, {
global: {
mocks: {
$t: () => 'mocked'
}
}
})
expect(wrapper.text()).toContain('hello')
expect(wrapper.text()).toContain('mocked')
})
})