forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindAll.spec.js
344 lines (311 loc) · 11.9 KB
/
findAll.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
import { compileToFunctions } from 'vue-template-compiler'
import ComponentWithChild from '~resources/components/component-with-child.vue'
import ComponentWithoutName from '~resources/components/component-without-name.vue'
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
import ComponentWithVFor from '~resources/components/component-with-v-for.vue'
import Component from '~resources/components/component.vue'
import FunctionalComponent from '~resources/components/functional-component.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import {
functionalSFCsSupported,
describeWithShallowAndMount,
isRunningPhantomJS
} from '~resources/utils'
import { itDoNotRunIf, itSkipIf } from 'conditional-specs'
describeWithShallowAndMount('findAll', mountingMethod => {
it('returns an WrapperArray of elements matching tag selector passed', () => {
const compiled = compileToFunctions('<div><p></p><p></p></div>')
const wrapper = mountingMethod(compiled)
const divs = wrapper.findAll('p')
expect(divs.length).to.equal(2)
})
it('returns an array of Wrapper of elements matching class selector passed', () => {
const compiled = compileToFunctions('<div><div class="foo" /></div>')
const wrapper = mountingMethod(compiled)
const fooArr = wrapper.findAll('.foo')
expect(fooArr.length).to.equal(1)
})
it('returns an array of Wrapper of elements matching class selector passed if they are nested in a transition', () => {
const compiled = compileToFunctions('<transition><div /></transition>')
const wrapper = mountingMethod(compiled)
const divArr = wrapper.findAll('div')
expect(divArr.length).to.equal(1)
})
itDoNotRunIf(
isRunningPhantomJS,
'returns an array of Wrapper of elements matching class selector passed if they are declared inside a slot',
() => {
const wrapper = mountingMethod(ComponentWithSlots, {
slots: {
default: '<div class="foo"><div class="foo"></div></div>'
}
})
const fooArr = wrapper.findAll('.foo')
expect(fooArr.length).to.equal(2)
}
)
it('returns an array of Wrapper of elements matching class selector passed if they are declared inside a functional component', () => {
const Component = {
functional: true,
render (h) {
return h('p', {}, [
h('p', {
class: {
foo: true
}
}),
h('p')
])
},
name: 'common'
}
const wrapper = mountingMethod(Component)
expect(wrapper.findAll('p').length).to.equal(3)
})
it('works correctly with innerHTML', () => {
const TestComponent = {
render (createElement) {
return createElement('div', {
domProps: {
innerHTML: '<svg></svg>'
}
})
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.findAll('svg').length).to.equal(1)
})
it('returns an array of Wrappers of elements matching id selector passed', () => {
const compiled = compileToFunctions('<div><div id="foo" /></div>')
const wrapper = mountingMethod(compiled)
const fooArr = wrapper.findAll('#foo')
expect(fooArr.length).to.equal(1)
})
it('returns an array of Wrappers of elements matching attribute selector passed', () => {
const compiled = compileToFunctions('<div><a href="/"></a></div>')
const wrapper = mountingMethod(compiled)
const hrefArr = wrapper.findAll('[href="/"]')
expect(hrefArr.length).to.equal(1)
})
it('throws an error when passed an invalid DOM selector', () => {
const compiled = compileToFunctions('<div><a href="/"></a></div>')
const wrapper = mountingMethod(compiled)
const message =
'[vue-test-utils]: wrapper.findAll() must be passed a valid CSS selector, Vue constructor, or valid find option object'
const fn = () => wrapper.findAll('[href=&6"/"]')
expect(fn)
.to.throw()
.with.property('message', message)
})
it('returns an array of Wrappers of elements matching selector when descendant combinator passed', () => {
const compiled = compileToFunctions(
'<div><ul><li>list</li>item<li></li></ul></div>'
)
const wrapper = mountingMethod(compiled)
const liArr = wrapper.findAll('div li')
expect(liArr.length).to.equal(2)
})
it('does not return duplicate nodes', () => {
const compiled = compileToFunctions(
'<div><div><div><p/><p/></div></div></div></div>'
)
const wrapper = mountingMethod(compiled)
expect(wrapper.findAll('div p').length).to.equal(2)
})
it('returns an array of Wrappers of elements matching selector with direct descendant combinator passed', () => {
const compiled = compileToFunctions('<div><ul><ul></ul></ul></div>')
const wrapper = mountingMethod(compiled)
const ulArr = wrapper.findAll('div > ul')
expect(ulArr.length).to.equal(1)
})
it('returns an array of Wrappers of elements matching pseudo selector', () => {
const compiled = compileToFunctions('<div><p></p><p></p></div>')
const wrapper = mountingMethod(compiled)
const divs = wrapper.findAll('p:first-of-type')
expect(divs.length).to.equal(1)
})
it('returns an array of VueWrappers of Vue Components matching component', () => {
const wrapper = mountingMethod(ComponentWithChild)
const componentArr = wrapper.findAll(Component)
expect(componentArr.length).to.equal(1)
})
it('returns correct number of Vue Wrapper when component has a v-for', () => {
const items = [{ id: 1 }, { id: 2 }, { id: 3 }]
const wrapper = mountingMethod(ComponentWithVFor, { propsData: { items }})
const componentArray = wrapper.findAll(Component)
expect(componentArray.length).to.equal(items.length)
})
it('returns array of VueWrappers of Vue Components matching component if component name in parent is different to filename', () => {
const wrapper = mountingMethod(ComponentWithChild)
const div = wrapper.findAll('span').at(0)
const componentArr = div.findAll(Component)
expect(componentArr.length).to.equal(1)
})
it('returns an array of VueWrappers of Vue Components matching component using Wrapper as reference', () => {
// same test as above, but good to be explicit
const wrapper = mountingMethod(ComponentWithChild)
const div = wrapper.findAll('span').at(0)
const componentArr = div.findAll(Component)
expect(componentArr.length).to.equal(1)
})
it('only returns Vue components that exists as children of Wrapper', () => {
const AComponent = {
render: () => {},
name: 'a-component'
}
const TestComponent = {
template: `
<div>
<span>
<a-component />
</span>
<a-component />
</div>
`,
components: {
'a-component': AComponent
}
}
const wrapper = mountingMethod(TestComponent)
const span = wrapper.find('span')
expect(span.findAll(AComponent).length).to.equal(1)
})
it('returns matching Vue components that have no name property', () => {
const TestComponent = {
template: `
<div>
<component-without-name />
<component-without-name />
<component-without-name />
</div>
`,
components: {
ComponentWithoutName
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.findAll(ComponentWithoutName).length).to.equal(3)
})
itSkipIf(isRunningPhantomJS, 'returns Wrapper of class component', () => {
const TestComponent = {
template: `
<div>
<component-as-a-class />
</div>
`,
components: {
ComponentAsAClass
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.findAll(ComponentAsAClass).length).to.equal(1)
})
it('returns Wrapper of Vue Component matching functional component', () => {
if (!functionalSFCsSupported) {
return
}
const TestComponent = {
template: `
<div>
<functional-component />
</div>
`,
components: {
FunctionalComponent
}
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.findAll(FunctionalComponent).length).to.equal(1)
})
it('returns VueWrapper with length 0 if no nodes matching selector are found', () => {
const wrapper = mountingMethod(Component)
const preArray = wrapper.findAll('pre')
expect(preArray.length).to.equal(0)
expect(preArray.wrappers).to.deep.equal([])
})
it('returns an array of Wrapper of elements matching a component name in options object', () => {
const wrapper = mountingMethod(ComponentWithChild)
const wrapperArray = wrapper.findAll({ name: 'test-component' })
expect(wrapperArray.at(0).name()).to.equal('test-component')
expect(wrapperArray.length).to.equal(1)
})
it('returns an array of Wrapper of elements matching the ref in options object', () => {
const compiled = compileToFunctions('<div><div ref="foo" /></div>')
const wrapper = mountingMethod(compiled)
const fooArr = wrapper.findAll({ ref: 'foo' })
expect(fooArr.length).to.equal(1)
})
it('throws an error when ref selector is called on a wrapper that is not a Vue component', () => {
const compiled = compileToFunctions('<div><a href="/"></a></div>')
const wrapper = mountingMethod(compiled)
const a = wrapper.find('a')
const message =
'[vue-test-utils]: $ref selectors can only be used on Vue component wrappers'
const fn = () => a.findAll({ ref: 'foo' })
expect(fn)
.to.throw()
.with.property('message', message)
})
it('returns an array of Wrapper of elements matching the ref in options object if they are nested in a transition', () => {
const compiled = compileToFunctions(
'<transition><div ref="foo" /></transition>'
)
const wrapper = mountingMethod(compiled)
const divArr = wrapper.findAll({ ref: 'foo' })
expect(divArr.length).to.equal(1)
})
it('returns correct number of Vue Wrapper when component has a v-for and matches the ref in options object', () => {
const items = [{ id: 1 }, { id: 2 }, { id: 3 }]
const wrapper = mountingMethod(ComponentWithVFor, { propsData: { items }})
const componentArray = wrapper.findAll({ ref: 'item' })
expect(componentArray.length).to.equal(items.length)
})
it('returns VueWrapper with length 0 if no nodes matching the ref in options object are found', () => {
const wrapper = mountingMethod(Component)
const preArray = wrapper.findAll({ ref: 'foo' })
expect(preArray.length).to.equal(0)
expect(preArray.wrappers).to.deep.equal([])
})
it('throws an error if selector is not a valid selector', () => {
const wrapper = mountingMethod(Component)
const invalidSelectors = [
undefined,
null,
NaN,
0,
2,
true,
false,
() => {},
{},
{ name: undefined },
{ ref: 'foo', nope: true },
[]
]
invalidSelectors.forEach(invalidSelector => {
const message =
'[vue-test-utils]: wrapper.findAll() must be passed a valid CSS selector, Vue constructor, or valid find option object'
const fn = () => wrapper.findAll(invalidSelector)
expect(fn)
.to.throw()
.with.property('message', message)
})
})
itDoNotRunIf(
mountingMethod.name === 'shallowMount',
'returns a WrapperArray which includes VueWrapper if the elements binds a Vue instance', () => {
const childComponent = {
name: 'bar',
template: '<p class="foo" />'
}
const wrapper = mountingMethod({
name: 'foo',
template: '<div class="foo"><p class="foo" /><child-component /></div>',
components: { childComponent }
})
const wrappers = wrapper.findAll('.foo')
expect(wrappers.at(0).vm.$options.name).to.equal('foo')
expect(wrappers.at(1).vm).to.equal(undefined)
expect(wrappers.at(2).vm.$options.name).to.equal('bar')
})
})