-
-
Notifications
You must be signed in to change notification settings - Fork 975
/
testing.spec.ts
227 lines (187 loc) 路 5.96 KB
/
testing.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { createTestingPinia, TestingOptions } from './testing'
import { createPinia, defineStore, setActivePinia } from 'pinia'
import { mount } from '@vue/test-utils'
import { defineComponent, ref, computed } from 'vue'
describe('Testing', () => {
const useCounter = defineStore('counter', {
state: () => ({ n: 0 }),
actions: {
increment(amount = 1) {
this.n += amount
},
},
})
const Counter = defineComponent({
setup() {
const counter = useCounter()
return { counter }
},
template: `
<button @click="counter.increment()">+1</button>
<span>{{ counter.n }}</span>
<button @click="counter.increment(10)">+10</button>
`,
})
function factory(options?: TestingOptions) {
const wrapper = mount(Counter, {
global: {
plugins: [createTestingPinia(options)],
},
})
const counter = useCounter()
return { wrapper, counter }
}
it('spies with no config', () => {
const { counter, wrapper } = factory()
counter.increment()
expect(counter.n).toBe(0)
expect(counter.increment).toHaveBeenCalledTimes(1)
expect(counter.increment).toHaveBeenLastCalledWith()
counter.increment(5)
expect(counter.n).toBe(0)
expect(counter.increment).toHaveBeenCalledTimes(2)
expect(counter.increment).toHaveBeenLastCalledWith(5)
wrapper.findAll('button')[0].trigger('click')
expect(counter.n).toBe(0)
expect(counter.increment).toHaveBeenCalledTimes(3)
expect(counter.increment).toHaveBeenLastCalledWith()
wrapper.findAll('button')[1].trigger('click')
expect(counter.n).toBe(0)
expect(counter.increment).toHaveBeenCalledTimes(4)
expect(counter.increment).toHaveBeenLastCalledWith(10)
})
it('can execute actions', () => {
const { counter, wrapper } = factory({ stubActions: false })
counter.increment()
expect(counter.n).toBe(1)
expect(counter.increment).toHaveBeenCalledTimes(1)
expect(counter.increment).toHaveBeenLastCalledWith()
counter.increment(5)
expect(counter.n).toBe(6)
expect(counter.increment).toHaveBeenCalledTimes(2)
expect(counter.increment).toHaveBeenLastCalledWith(5)
wrapper.findAll('button')[0].trigger('click')
expect(counter.n).toBe(7)
expect(counter.increment).toHaveBeenCalledTimes(3)
expect(counter.increment).toHaveBeenLastCalledWith()
wrapper.findAll('button')[1].trigger('click')
expect(counter.n).toBe(17)
expect(counter.increment).toHaveBeenCalledTimes(4)
expect(counter.increment).toHaveBeenLastCalledWith(10)
})
it('spies $patch calls', () => {
const { counter } = factory()
expect(counter.n).toBe(0)
expect(counter.$patch).toHaveBeenCalledTimes(0)
counter.$patch({ n: 1 })
expect(counter.$patch).toHaveBeenCalledTimes(1)
expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
expect(counter.n).toBe(1)
})
it('can stub $patch calls', () => {
const { counter } = factory({ stubPatch: true })
expect(counter.n).toBe(0)
expect(counter.$patch).toHaveBeenCalledTimes(0)
counter.$patch({ n: 1 })
expect(counter.$patch).toHaveBeenCalledTimes(1)
expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
expect(counter.n).toBe(0)
})
it('can stub $patch calls', () => {
const { counter } = factory({ stubPatch: true })
expect(counter.n).toBe(0)
expect(counter.$patch).toHaveBeenCalledTimes(0)
counter.$patch({ n: 1 })
expect(counter.$patch).toHaveBeenCalledTimes(1)
expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
expect(counter.n).toBe(0)
})
it('executes plugins', () => {
const { counter, wrapper } = factory({
plugins: [() => ({ pluginN: 0 })],
})
expect(counter.pluginN).toBe(0)
expect(wrapper.vm.counter.pluginN).toBe(0)
})
it('executes plugins with fakeApp', () => {
const pinia = createTestingPinia({
plugins: [() => ({ pluginN: 0 })],
fakeApp: true,
})
const counter = useCounter(pinia)
expect(counter.pluginN).toBe(0)
expect(pinia.app).toHaveProperty('mount', expect.any(Function))
})
it('bypass useStore(pinia)', () => {
const realPinia = createPinia()
const { counter } = factory()
const counterWithRealPinia = useCounter(realPinia)
expect(counter.n).toBe(0)
expect(counterWithRealPinia.n).toBe(0)
counter.n++
expect(counter.n).toBe(1)
expect(counterWithRealPinia.n).toBe(1)
})
it('works with no actions', () => {
const useEmpty = defineStore('empty', {})
const Empty = defineComponent({
setup() {
const empty = useEmpty()
return { empty }
},
template: `{{ empty.$id }}`,
})
const wrapper = mount(Empty, {
global: {
plugins: [createTestingPinia()],
},
})
expect(wrapper.text()).toBe('empty')
})
it('works with nested stores', () => {
const useA = defineStore('a', () => {
const n = ref(0)
return { n }
})
const useB = defineStore('b', () => {
const a = useA()
const n = ref(0)
const doubleA = computed(() => a.n * 2)
return { n, doubleA }
})
const pinia = createTestingPinia()
setActivePinia(pinia)
const b = useB()
const a = useA()
expect(a.n).toBe(0)
a.n++
expect(b.doubleA).toBe(2)
expect(pinia.state.value).toEqual({
a: { n: 1 },
b: { n: 0 },
})
})
it('allows overriding computed properties', () => {
const useStore = defineStore('lol', {
state: () => ({ n: 0 }),
getters: {
double: (state) => state.n * 2,
},
})
const pinia = createTestingPinia()
const store = useStore(pinia)
store.n++
expect(store.double).toBe(2)
// once the getter is overridden, it stays
store.double = 3
expect(store.double).toBe(3)
store.n++
expect(store.double).toBe(3)
// it can be set to undefined again to reset
// @ts-expect-error
store.double = undefined
expect(store.double).toBe(4)
store.n++
expect(store.double).toBe(6)
})
})