/
find.spec.ts
370 lines (317 loc) · 10.1 KB
/
find.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import { defineComponent, h, nextTick, Fragment } from 'vue'
import { mount, VueWrapper } from '../src'
import SuspenseComponent from './components/Suspense.vue'
import ParentComponent from './components/ParentComponent.vue'
import MultipleRootRender from './components/MultipleRootRender.vue'
describe('find', () => {
it('find using single root node', () => {
const Component = defineComponent({
render() {
return h('div', {}, [h('span', { id: 'my-span' })])
}
})
const wrapper = mount(Component)
expect(wrapper.find('#my-span').exists()).toBe(true)
})
describe('find DOM element by ref', () => {
it('works for root element', () => {
const Component = defineComponent({
render() {
return h('div', {}, [h('span', { ref: 'span', id: 'my-span' })])
}
})
const wrapper = mount(Component)
expect(wrapper.find({ ref: 'span' }).exists()).toBe(true)
expect(wrapper.find({ ref: 'span' }).attributes('id')).toBe('my-span')
})
it('works when ref is pointing to non-element node', () => {
const Component = defineComponent({
render() {
return h('div', null, h(Fragment, { ref: 'plain' }, ['hello']))
}
})
const wrapper = mount(Component)
expect(wrapper.find({ ref: 'plain' }).exists()).toBe(true)
expect(wrapper.find({ ref: 'plain' }).element).toBeInstanceOf(Text)
})
it('does not find ref located in the same component but not in current DOM wrapper', () => {
const Component = defineComponent({
render() {
return h('div', [
h('span', { ref: 'span', id: 'my-span' }),
h('div', { class: 'search-target' })
])
}
})
const wrapper = mount(Component)
expect(
wrapper.find('.search-target').find({ ref: 'span' }).exists()
).toBe(false)
})
})
it('find using current node after findAllComponents', () => {
const ComponentB = defineComponent({
name: 'ComponentB',
template: '<div><slot></slot></div>'
})
const ComponentA = defineComponent({
name: 'ComponentA',
template: `
<div>
<component-b v-for="item in [1, 2]" :key="item">
<input type="text" :value="item">
</component-b>
</div>
`,
components: { ComponentB }
})
const wrapper = mount(ComponentA)
const firstCompB = wrapper.findComponent({ name: 'ComponentB' })
const lastCompB = wrapper.findAllComponents({ name: 'ComponentB' })[1]
expect(firstCompB.find('input').element.value).toBe('1')
expect(lastCompB.find('input').element.value).toBe('2')
})
it('returns the root element in single root element', () => {
const Component = defineComponent({
render() {
return h('div', { class: 'foo' }, 'text')
}
})
const wrapper = mount(Component)
expect(wrapper.find('.foo').exists()).toBe(true)
})
it('can be chained', () => {
const Component = defineComponent({
render() {
return h('div', { class: 'foo' }, [h('div', { class: 'bar' })])
}
})
const wrapper = mount(Component)
expect(wrapper.find('.foo').find('.bar').exists()).toBe(true)
})
it('works with suspense', async () => {
const wrapper = mount(SuspenseComponent)
expect(wrapper.html()).toContain('Fallback content')
expect(wrapper.find('div').exists()).toBeTruthy()
})
it('can wrap `find` in an async function', async () => {
async function findAfterNextTick(
wrapper: VueWrapper<any>,
selector: string
) {
await nextTick()
return wrapper.find(selector)
}
const wrapper = mount({
template: `<div>My component</div>`
})
const foundElement = await findAfterNextTick(
wrapper,
'.something-that-does-not-exist'
)
expect(foundElement.exists()).toBeFalsy()
})
it('handle empty root node', () => {
const EmptyTestComponent = {
name: 'EmptyTestComponent',
render: () => null
}
const Component = defineComponent({
render() {
return h('div', [h(EmptyTestComponent)])
}
})
const wrapper = mount(Component)
const etc = wrapper.findComponent({ name: 'EmptyTestComponent' })
expect(etc.find('p').exists()).toBe(false)
})
it('finds root node with SFC render function', () => {
const wrapper = mount(MultipleRootRender)
expect(wrapper.find('a').exists()).toBe(true)
})
})
describe('findAll', () => {
it('findAll using single root node', () => {
const Component = defineComponent({
render() {
return h('div', {}, [
h('span', { className: 'span' }),
h('span', { className: 'span' })
])
}
})
const wrapper = mount(Component)
expect(wrapper.findAll('.span')).toHaveLength(2)
})
it('findAll using multiple root nodes', () => {
const Component = defineComponent({
render() {
return [
h('span', { className: 'span' }),
h('span', { className: 'span' })
]
}
})
const wrapper = mount(Component)
expect(wrapper.findAll('.span')).toHaveLength(2)
})
it('findAll using current node after findAllComponents', () => {
const ComponentB = defineComponent({
name: 'ComponentB',
template: '<div><slot></slot></div>'
})
const ComponentA = defineComponent({
name: 'ComponentA',
template: `
<div>
<component-b v-for="item in [1, 2]" :key="item">
<input type="text" :value="item">
</component-b>
</div>
`,
components: { ComponentB }
})
const wrapper = mount(ComponentA)
const firstCompB = wrapper.findComponent({ name: 'ComponentB' })
const lastCompB = wrapper.findAllComponents({ name: 'ComponentB' })[1]
expect(firstCompB.findAll('input')[0].element.value).toBe('1')
expect(lastCompB.findAll('input')[0].element.value).toBe('2')
})
it('works with suspense', async () => {
const wrapper = mount(SuspenseComponent)
expect(wrapper.html()).toContain('Fallback content')
expect(wrapper.findAll('div')).toBeTruthy()
})
it('chaining finds compiles successfully', () => {
const Bar = {
render() {
return h('span', { id: 'bar' })
}
}
const Foo = {
render() {
return h('span', { id: 'foo' }, h(Bar))
}
}
const wrapper = mount(Foo)
expect(wrapper.find('#foo').find('#bar').exists()).toBe(true)
})
it('handle empty/comment root node', () => {
const EmptyTestComponent = {
name: 'EmptyTestComponent',
render: () => null
}
const Component = defineComponent({
render() {
return h('div', [h(EmptyTestComponent)])
}
})
const wrapper = mount(Component)
const etc = wrapper.findComponent({ name: 'EmptyTestComponent' })
expect(etc.findAll('p')).toHaveLength(0)
})
describe('with multiple root nodes', () => {
const MultipleRootComponentWithRenderFn = defineComponent({
render() {
return [
h(
'div',
{ class: 'root1' },
h('div', { class: 'target1' }, 'target1')
),
h(
'div',
{ class: 'root2' },
h('div', { class: 'target2' }, 'target2')
),
h(
'div',
{ class: 'root3' },
h('div', { class: 'target3' }, 'target3')
)
]
}
})
const MultipleRootComponentWithTemplate = defineComponent({
template: [
'<div class="root1"><div class="target1">target1</div></div>',
'<div class="root2"><div class="target2">target2</div></div>',
'<div class="root3"><div class="target3">target3</div></div>'
].join('\n')
})
const WrapperComponent = defineComponent({
components: {
MultipleRootComponentWithTemplate,
MultipleRootComponentWithRenderFn
},
template: [
'<div><multiple-root-component-with-template /></div>',
'<div><multiple-root-component-with-render-fn /></div>'
].join('\n')
})
it('find one of root nodes', () => {
const Component = defineComponent({
render() {
return [h('div', 'text'), h('span', { id: 'my-span' })]
}
})
const wrapper = mount(Component)
expect(wrapper.find('#my-span').exists()).toBe(true)
})
it('finds second root node when component is not mount root', () => {
const wrapper = mount(WrapperComponent)
expect(
wrapper
.findComponent(MultipleRootComponentWithRenderFn)
.find('.root2')
.exists()
).toBe(true)
expect(
wrapper
.findComponent(MultipleRootComponentWithTemplate)
.find('.root2')
.exists()
).toBe(true)
})
it('finds contents of second root node when component is not mount root', () => {
const wrapper = mount(WrapperComponent)
expect(
wrapper
.findComponent(MultipleRootComponentWithTemplate)
.find('.target2')
.exists()
).toBe(true)
expect(
wrapper
.findComponent(MultipleRootComponentWithRenderFn)
.find('.target2')
.exists()
).toBe(true)
})
it('finds all root nodes with SFC render function', () => {
const wrapper = mount(MultipleRootRender)
expect(wrapper.findAll('a')).toHaveLength(3)
})
})
// https://github.com/vuejs/test-utils/issues/1233
it('finds 3 children', () => {
const wrapper = mount(ParentComponent)
const parent = wrapper.get('.parent')
expect(parent.findAll('div').length).toBe(3)
})
it('finds itself on Vue wrapper', () => {
const wrapper = mount(ParentComponent)
const parent = wrapper.get('div')
expect(parent.classes()).toContain('parent')
})
it('does not find itself on DOM node', () => {
const wrapper = mount(ParentComponent)
const parent = wrapper.get('.parent')
expect(parent.find('div').classes()).toContain('first')
})
// https://github.com/vuejs/test-utils/issues/1233
it('finds all divs with findAll', () => {
const wrapper = mount(ParentComponent)
expect(wrapper.findAll('div').length).toBe(4)
})
})