forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetData.spec.js
313 lines (293 loc) · 8 KB
/
setData.spec.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { compileToFunctions } from 'vue-template-compiler'
import ComponentWithVIf from '~resources/components/component-with-v-if.vue'
import ComponentWithWatch from '~resources/components/component-with-watch.vue'
import { describeWithShallowAndMount, vueVersion } from '~resources/utils'
describeWithShallowAndMount('setData', mountingMethod => {
let info
beforeEach(() => {
info = sinon.stub(console, 'info')
})
afterEach(() => {
info.restore()
})
it('sets component data and updates nested vm nodes when called on Vue instance', () => {
const wrapper = mountingMethod(ComponentWithVIf)
expect(wrapper.findAll('.child.ready').length).to.equal(0)
wrapper.setData({ ready: true })
expect(wrapper.findAll('.child.ready').length).to.equal(1)
})
it('keeps element in sync with vnode', () => {
const Component = {
template: '<div class="some-class" v-if="show">A custom component!</div>',
data () {
return {
show: false
}
}
}
const wrapper = mountingMethod(Component)
wrapper.setData({ show: true })
expect(wrapper.element).to.equal(wrapper.vm.$el)
expect(wrapper.classes()).to.contain('some-class')
})
it('runs watch function when data is updated', () => {
const wrapper = mountingMethod(ComponentWithWatch)
const data1 = 'testest'
wrapper.setData({ data1 })
expect(wrapper.vm.data2).to.equal(data1)
})
it('runs watch function after all props are updated', () => {
const wrapper = mountingMethod(ComponentWithWatch)
const data1 = 'testest'
wrapper.setData({ data2: 'newProp', data1 })
expect(info.args[1][0]).to.equal(data1)
})
it('throws error if node is not a Vue instance', () => {
const message = 'wrapper.setData() can only be called on a Vue instance'
const compiled = compileToFunctions('<div><p></p></div>')
const wrapper = mountingMethod(compiled)
const p = wrapper.find('p')
expect(() => p.setData({ ready: true })).throw(Error, message)
})
it('throws error when called on functional vnode', () => {
const AFunctionalComponent = {
render: (h, context) => h('div', context.prop1),
functional: true
}
const message =
'[vue-test-utils]: wrapper.setData() cannot be called on a functional component'
const fn = () =>
mountingMethod(AFunctionalComponent).setData({ data1: 'data' })
expect(fn)
.to.throw()
.with.property('message', message)
// find on functional components isn't supported in Vue < 2.3
if (vueVersion < 2.3) {
return
}
const TestComponent = {
template: '<div><a-functional-component /></div>',
components: {
AFunctionalComponent
}
}
const fn2 = () =>
mountingMethod(TestComponent)
.find(AFunctionalComponent)
.setData({ data1: 'data' })
expect(fn2)
.to.throw()
.with.property('message', message)
})
it('updates watchers if computed is updated', () => {
const TestComponent = {
template: `
<em>{{ computedText }}</em>
`,
data () {
return {
text: '',
basket: []
}
},
computed: {
computedText () {
return this.text
}
},
watch: {
text () {
this.basket.push(this.computedText)
}
}
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({ text: 'hello' })
expect(wrapper.vm.basket[0]).to.equal('hello')
})
it('should not run watcher if data is null', () => {
const TestComponent = {
template: `
<div>
<div v-if="!message">There is no message yet</div>
<div v-else>{{ reversedMessage }}</div>
</div>
`,
data: () => ({
message: 'egassem'
}),
computed: {
reversedMessage: function () {
return this.message
.split('')
.reverse()
.join('')
}
}
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({ message: null })
expect(wrapper.text()).to.equal('There is no message yet')
})
it('updates an existing property in a data object', () => {
const TestComponent = {
data: () => ({
anObject: {
propA: {
prop1: 'a'
},
propB: 'b'
}
}),
render: () => {}
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
anObject: {
propA: {
prop1: 'c'
}
}
})
expect(wrapper.vm.anObject.propB).to.equal('b')
expect(wrapper.vm.anObject.propA.prop1).to.equal('c')
})
it('should append a new property to an object without removing existing properties', () => {
const TestComponent = {
data: () => ({
anObject: {
propA: {
prop1: 'a'
},
propB: 'b'
}
}),
render: () => {}
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
anObject: {
propA: {
prop2: 'b'
}
}
})
expect(wrapper.vm.anObject.propA.prop1).to.equal('a')
expect(wrapper.vm.anObject.propA.prop2).to.equal('b')
})
it('handles undefined values', () => {
const TestComponent = {
template: `
<div>
{{undefinedProperty && undefinedProperty.foo}}
</div>
`,
data: () => ({
undefinedProperty: undefined
})
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
undefinedProperty: {
foo: 'baz'
}
})
expect(wrapper.text()).to.contain('baz')
})
it('handles null values', () => {
const TestComponent = {
template: `
<div>{{nullProperty && nullProperty.foo}}</div>
`,
data: () => ({
nullProperty: null
})
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
nullProperty: {
foo: 'bar',
another: null
}
})
expect(wrapper.text()).to.contain('bar')
wrapper.setData({
nullProperty: {
another: {
obj: true
}
}
})
expect(wrapper.vm.nullProperty.another.obj).to.equal(true)
})
it('does not merge arrays', () => {
const TestComponent = {
template: '<div>{{nested.nested.nestedArray[0]}}</div>',
data: () => ({
items: [1, 2],
nested: {
nested: {
nestedArray: [1, 2]
}
}
})
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
items: [3]
})
expect(wrapper.vm.items).to.deep.equal([3])
wrapper.setData({
nested: {
nested: {
nestedArray: [10]
}
}
})
expect(wrapper.text()).to.equal('10')
expect(wrapper.vm.nested.nested.nestedArray).to.deep.equal([10])
})
it('should append a new property to an object when the new property is referenced by a template', () => {
const TestComponent = {
data: () => ({
anObject: {
propA: 'a',
propB: 'b'
}
}),
computed: {
anObjectKeys () {
return Object.keys(this.anObject).join(',')
}
},
template: `<div>{{ anObjectKeys }}</div>`
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
anObject: {
propC: 'c'
}
})
expect(wrapper.vm.anObject.propA).to.equal('a')
expect(wrapper.vm.anObject.propB).to.equal('b')
expect(wrapper.vm.anObject.propC).to.equal('c')
expect(wrapper.vm.anObjectKeys).to.equal('propA,propB,propC')
expect(wrapper.html()).to.equal('<div>propA,propB,propC</div>')
})
it('allows setting data of type Date synchronously', () => {
const TestComponent = {
template: `
<div>
{{selectedDate}}
</div>
`,
data: () => ({
selectedDate: undefined
})
}
const testDate = new Date()
const wrapper = mountingMethod(TestComponent)
wrapper.setData({ selectedDate: testDate })
expect(wrapper.vm.selectedDate).to.equal(testDate)
})
})