forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper-array.spec.js
350 lines (316 loc) · 10.8 KB
/
wrapper-array.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { Wrapper, WrapperArray } from '~vue/test-utils'
import { describeWithShallowAndMount } from '~resources/utils'
describeWithShallowAndMount('WrapperArray', mountingMethod => {
function getWrapperArray (wrappers) {
if (!wrappers) {
wrappers = [1, 2, 3].map((v) => {
const p = document.createElement('p')
p.textContent = v
return new Wrapper(p)
})
}
return new WrapperArray(wrappers)
}
['wrappers', 'length'].forEach(property => {
it(`has the ${property} property which is read-only`, () => {
const wrapperArray = getWrapperArray()
const message = `[vue-test-utils]: wrapperArray.${property} is read-only`
expect(() => { wrapperArray[property] = 'foo' })
.to.throw()
.with.property('message', message)
})
})
it('returns class with length equal to length of wrappers passed in constructor', () => {
const wrapperArray = getWrapperArray()
expect(wrapperArray.length).to.equal(3)
})
it('returns wrapper at index 0 when at(0) is called', () => {
const wrapperArray = getWrapperArray()
expect(wrapperArray.at(0).text()).to.equal('1')
})
it('returns filtered wrapper when filter is called', () => {
const wrapperArray = getWrapperArray()
expect(
wrapperArray.filter(w => {
return w.text() !== '2'
}).length
).to.equal(2)
})
const methods = [
'at',
'attributes',
'classes',
'contains',
'emitted',
'emittedByOrder',
'hasAttribute',
'hasClass',
'hasProp',
'hasStyle',
'find',
'findAll',
'html',
'text',
'is',
'isEmpty',
'isVisible',
'isVueInstance',
'name',
'props',
'setChecked',
'setComputed',
'setMethods',
'setData',
'setProps',
'setSelected',
'setValue',
'trigger',
'update',
'destroy'
]
methods.forEach(method => {
it(`throws error if ${method} is called when there are no items in wrapper array`, () => {
if (method === 'at') {
return
}
const wrapperArray = getWrapperArray([])
const message = `[vue-test-utils]: ${method} cannot be called on 0 items`
expect(() => wrapperArray[method]())
.to.throw()
.with.property('message', message)
})
it(`${method} throws error if called when there are items in wrapper array`, () => {
if (
[
'at',
'contains',
'hasAttribute',
'hasClass',
'hasProp',
'hasStyle',
'is',
'isEmpty',
'isVisible',
'isVueInstance',
'setChecked',
'setComputed',
'setMethods',
'setData',
'setProps',
'setValue',
'trigger',
'update',
'destroy'
].includes(method)
) {
return
}
const wrapperArray = getWrapperArray([1, 2, 3])
const message = `[vue-test-utils]: ${method} must be called on a single wrapper, use at(i) to access a wrapper`
expect(() => wrapperArray[method]())
.to.throw()
.with.property('message', message)
})
})
it('exists returns true if it has every existing wrappers', () => {
const wrapperArray = getWrapperArray()
wrapperArray.wrappers.forEach(w => {
expect(w.exists()).to.equal(true)
})
expect(wrapperArray.exists()).to.equal(true)
})
it('exists returns false if it does not have existing wrappers', () => {
const wrapperArray = getWrapperArray([])
expect(wrapperArray.exists()).to.equal(false)
})
it('exists returns false if it has not existing wrappers', () => {
const wrapper1 = {
exists () {
return true
}
}
const wrapper2 = {
exists () {
return false
}
}
const wrapperArray = getWrapperArray([wrapper1, wrapper2])
expect(wrapperArray.exists()).to.equal(false)
})
it('contains returns true if every wrapper.contains() returns true', () => {
const selector = 'selector'
const contains = sinon.stub()
contains.withArgs(selector).returns(true)
const wrapperArray = getWrapperArray([{ contains }, { contains }])
expect(wrapperArray.contains(selector)).to.equal(true)
})
it('contains returns false if not every wrapper.contains() returns true', () => {
const wrapperArray = getWrapperArray([
{ contains: () => true },
{ contains: () => false }
])
expect(wrapperArray.contains()).to.equal(false)
})
it('hasAttribute returns true if every wrapper.hasAttribute() returns true', () => {
const attribute = 'attribute'
const value = 'value'
const hasAttribute = sinon.stub()
hasAttribute.withArgs(attribute, value).returns(true)
const wrapperArray = getWrapperArray([{ hasAttribute }, { hasAttribute }])
expect(wrapperArray.hasAttribute(attribute, value)).to.equal(true)
})
it('hasAttribute returns false if not every wrapper.hasAttribute() returns true', () => {
const wrapperArray = getWrapperArray([
{ hasAttribute: () => true },
{ hasAttribute: () => false }
])
expect(wrapperArray.hasAttribute('attribute', 'value')).to.equal(false)
})
it('hasClass returns true if every wrapper.hasClass() returns true', () => {
const className = 'class'
const hasClass = sinon.stub()
hasClass.withArgs(className).returns(true)
const wrapperArray = getWrapperArray([{ hasClass }, { hasClass }])
expect(wrapperArray.hasClass(className)).to.equal(true)
})
it('hasClass returns false if not every wrapper.hasClass() returns true', () => {
const wrapperArray = getWrapperArray([
{ hasClass: () => true },
{ hasClass: () => false }
])
expect(wrapperArray.hasClass('class')).to.equal(false)
})
it('hasProp returns true if every wrapper.hasProp() returns true', () => {
const prop = 'prop'
const value = 'value'
const hasProp = sinon.stub()
hasProp.withArgs(prop, value).returns(true)
const wrapperArray = getWrapperArray([{ hasProp }, { hasProp }])
expect(wrapperArray.hasProp(prop, value)).to.equal(true)
})
it('hasProp returns false if not every wrapper.hasProp() returns true', () => {
const wrapperArray = getWrapperArray([
{ hasProp: () => true },
{ hasProp: () => false }
])
expect(wrapperArray.hasProp('prop', 'value')).to.equal(false)
})
it('hasStyle returns true if every wrapper.hasStyle() returns true', () => {
const style = 'style'
const value = 'value'
const hasStyle = sinon.stub()
hasStyle.withArgs(style, value).returns(true)
const wrapperArray = getWrapperArray([{ hasStyle }, { hasStyle }])
expect(wrapperArray.hasStyle(style, value)).to.equal(true)
})
it('hasStyle returns false if not every wrapper.hasStyle() returns true', () => {
const wrapperArray = getWrapperArray([
{ hasStyle: () => true },
{ hasStyle: () => false }
])
expect(wrapperArray.hasStyle('style', 'value')).to.equal(false)
})
it('is returns true if every wrapper.is() returns true', () => {
const selector = 'selector'
const is = sinon.stub()
is.withArgs(selector).returns(true)
const wrapperArray = getWrapperArray([{ is }, { is }])
expect(wrapperArray.is(selector)).to.equal(true)
})
it('is returns false if not every wrapper.is() returns true', () => {
const wrapperArray = getWrapperArray([
{ is: () => true },
{ is: () => false }
])
expect(wrapperArray.is('selector')).to.equal(false)
})
it('isEmpty returns true if every wrapper.isEmpty() returns true', () => {
const wrapperArray = getWrapperArray([
{ isEmpty: () => true },
{ isEmpty: () => true }
])
expect(wrapperArray.isEmpty()).to.equal(true)
})
it('isEmpty returns false if not every wrapper.isEmpty() returns true', () => {
const wrapperArray = getWrapperArray([
{ isEmpty: () => true },
{ isEmpty: () => false }
])
expect(wrapperArray.isEmpty()).to.equal(false)
})
it('isVisible returns true if every wrapper.isVisible() returns true', () => {
const wrapperArray = getWrapperArray([
{ isVisible: () => true },
{ isVisible: () => true }
])
expect(wrapperArray.isVisible()).to.equal(true)
})
it('isVisible returns false if not every wrapper.isVisible() returns true', () => {
const wrapperArray = getWrapperArray([
{ isVisible: () => true },
{ isVisible: () => false }
])
expect(wrapperArray.isVisible()).to.equal(false)
})
it('isVueInstance returns true if every wrapper.isVueInstance() returns true', () => {
const wrapperArray = getWrapperArray([
{ isVueInstance: () => true },
{ isVueInstance: () => true }
])
expect(wrapperArray.isVueInstance()).to.equal(true)
})
it('isVueInstance returns false if not every wrapper.isVueInstance() returns true', () => {
const wrapperArray = getWrapperArray([
{ isVueInstance: () => true },
{ isVueInstance: () => false }
])
expect(wrapperArray.isVueInstance()).to.equal(false)
})
it('setComputed calls setMethods on each wrapper', () => {
const setComputed = sinon.stub()
const computed = {}
const wrapperArray = getWrapperArray([{ setComputed }, { setComputed }])
wrapperArray.setComputed(computed)
expect(setComputed.calledTwice).to.equal(true)
expect(setComputed.calledWith(computed)).to.equal(true)
})
it('setMethods calls setMethods on each wrapper', () => {
const setMethods = sinon.stub()
const methods = {}
const wrapperArray = getWrapperArray([{ setMethods }, { setMethods }])
wrapperArray.setMethods(methods)
expect(setMethods.calledTwice).to.equal(true)
expect(setMethods.calledWith(methods)).to.equal(true)
})
it('setData calls setData on each wrapper', () => {
const setData = sinon.stub()
const data = {}
const wrapperArray = getWrapperArray([{ setData }, { setData }])
wrapperArray.setData(data)
expect(setData.calledTwice).to.equal(true)
expect(setData.calledWith(data)).to.equal(true)
})
it('setProps calls setProps on each wrapper', () => {
const setProps = sinon.stub()
const props = {}
const wrapperArray = getWrapperArray([{ setProps }, { setProps }])
wrapperArray.setProps(props)
expect(setProps.calledTwice).to.equal(true)
expect(setProps.calledWith(props)).to.equal(true)
})
it('trigger calls trigger on each wrapper', () => {
const trigger = sinon.stub()
const event = 'click'
const options = {}
const wrapperArray = getWrapperArray([{ trigger }, { trigger }])
wrapperArray.trigger(event, options)
expect(trigger.calledTwice).to.equal(true)
expect(trigger.calledWith(event, options)).to.equal(true)
})
it('destroy calls destroy on each wrapper', () => {
const destroy = sinon.stub()
const wrapperArray = getWrapperArray([{ destroy }, { destroy }])
wrapperArray.destroy()
expect(destroy.calledTwice).to.equal(true)
})
})