-
Notifications
You must be signed in to change notification settings - Fork 668
/
slots.spec.js
417 lines (384 loc) · 17.6 KB
/
slots.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { compileToFunctions } from 'vue-template-compiler'
import Component from '~resources/components/component.vue'
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import {
describeWithMountingMethods,
vueVersion,
itSkipIf,
itDoNotRunIf,
isRunningPhantomJS
} from '~resources/utils'
describeWithMountingMethods('options.slots', (mountingMethod) => {
let _window
beforeEach(() => {
if (typeof window !== 'undefined') {
_window = window
}
})
afterEach(() => {
if (typeof window !== 'undefined' && !window.navigator.userAgent.match(/Chrome/i)) {
window = _window // eslint-disable-line no-native-reassign
}
})
it('mounts component with default slot if passed component in slot object', () => {
const wrapper = mountingMethod(ComponentWithSlots, { slots: { default: Component }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<div></div>')
} else {
expect(wrapper.contains(Component)).to.equal(true)
}
})
it('mounts component with default slot if passed component in array in slot object', () => {
const wrapper = mountingMethod(ComponentWithSlots, { slots: { default: [Component] }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<div></div>')
} else {
expect(wrapper.contains(Component)).to.equal(true)
}
})
it('mounts component with default slot if passed object with template prop in slot object', () => {
const compiled = compileToFunctions('<div id="div" />')
const wrapper = mountingMethod(ComponentWithSlots, { slots: { default: [compiled] }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('div id="div"')
} else {
expect(wrapper.contains('#div')).to.equal(true)
}
})
itDoNotRunIf(
process.env.TEST_ENV === 'node' || isRunningPhantomJS,
'mounts component with default slot if passed string in slot object', () => {
const wrapper = mountingMethod(ComponentWithSlots, { slots: { default: '<span />' }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<span')
} else {
expect(wrapper.contains('span')).to.equal(true)
}
})
itDoNotRunIf(
process.env.TEST_ENV === 'node' || vueVersion < 2.3 || isRunningPhantomJS,
'works correctly with class component', () => {
const wrapper = mountingMethod(ComponentAsAClass, { slots: { default: '<span />' }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<span')
} else {
expect(wrapper.contains('span')).to.equal(true)
}
})
itDoNotRunIf(
typeof window === 'undefined' || window.navigator.userAgent.match(/Chrome/i),
'throws error if the UserAgent is PhantomJS when passed string is in slot object', () => {
window = { navigator: { userAgent: 'PhantomJS' }} // eslint-disable-line no-native-reassign
const message = '[vue-test-utils]: the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.'
const fn = () => mountingMethod(ComponentWithSlots, { slots: { default: 'foo' }})
expect(fn).to.throw().with.property('message', message)
})
itDoNotRunIf(
process.env.TEST_ENV !== 'node',
'throws error passed string is in slot object', () => {
const message = '[vue-test-utils]: the slots string option does not support strings in server-test-uitls.'
const fn = () => mountingMethod(ComponentWithSlots, { slots: { default: 'foo' }})
expect(fn).to.throw().with.property('message', message)
})
itDoNotRunIf(
isRunningPhantomJS,
'mounts component with default slot if passed string in slot object', () => {
if (mountingMethod.name === 'renderToString') {
return
}
const wrapper1 = mountingMethod(ComponentWithSlots, { slots: { default: 'foo<span>123</span>{{ foo }}' }})
expect(wrapper1.find('main').html()).to.equal('<main>foo<span>123</span>bar</main>')
const wrapper2 = mountingMethod(ComponentWithSlots, { slots: { default: '<p>1</p>{{ foo }}2' }})
expect(wrapper2.find('main').html()).to.equal('<main><p>1</p>bar2</main>')
const wrapper3 = mountingMethod(ComponentWithSlots, { slots: { default: '<p>1</p>{{ foo }}<p>2</p>' }})
expect(wrapper3.find('main').html()).to.equal('<main><p>1</p>bar<p>2</p></main>')
const wrapper4 = mountingMethod(ComponentWithSlots, { slots: { default: '123' }})
expect(wrapper4.find('main').html()).to.equal('<main>123</main>')
const wrapper5 = mountingMethod(ComponentWithSlots, { slots: { default: '1{{ foo }}2' }})
expect(wrapper5.find('main').html()).to.equal('<main>1bar2</main>')
wrapper5.trigger('keydown')
const wrapper6 = mountingMethod(ComponentWithSlots, { slots: { default: '<p>1</p><p>2</p>' }})
expect(wrapper6.find('main').html()).to.equal('<main><p>1</p><p>2</p></main>')
const wrapper7 = mountingMethod(ComponentWithSlots, { slots: { default: '1<p>2</p>3' }})
expect(wrapper7.find('main').html()).to.equal('<main>1<p>2</p>3</main>')
})
itSkipIf(mountingMethod.name === 'renderToString',
'throws error if passed string in default slot object and vue-template-compiler is undefined', () => {
const compilerSave = require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = undefined
delete require.cache[require.resolve('../../../packages/test-utils')]
const mountingMethodFresh = require('../../../packages/test-utils')[mountingMethod.name]
const message = '[vue-test-utils]: vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined'
const fn = () => mountingMethodFresh(ComponentWithSlots, { slots: { default: '<span />' }})
try {
expect(fn).to.throw().with.property('message', message)
} catch (err) {
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
throw err
}
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
})
itDoNotRunIf(
process.env.TEST_ENV === 'node' || isRunningPhantomJS,
'mounts component with default slot if passed string in slot array object', () => {
const wrapper = mountingMethod(ComponentWithSlots, { slots: { default: ['<span />'] }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<span')
} else {
expect(wrapper.contains('span')).to.equal(true)
}
})
itDoNotRunIf(
process.env.TEST_ENV === 'node' || isRunningPhantomJS,
'mounts component with default slot if passed string in slot text array object', () => {
const wrapper = mountingMethod(ComponentWithSlots, { slots: { default: ['{{ foo }}<span>1</span>', 'bar'] }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<main>bar<span>1</span>bar</main>')
} else {
expect(wrapper.find('main').html()).to.equal('<main>bar<span>1</span>bar</main>')
}
})
itSkipIf(mountingMethod.name === 'renderToString',
'throws error if passed string in default slot array vue-template-compiler is undefined', () => {
const compilerSave = require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = undefined
delete require.cache[require.resolve('../../../packages/test-utils')]
const mountingMethodFresh = require('../../../packages/test-utils')[mountingMethod.name]
const message = '[vue-test-utils]: vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined'
const fn = () => mountingMethodFresh(ComponentWithSlots, { slots: { default: ['<span />'] }})
try {
expect(fn).to.throw().with.property('message', message)
} catch (err) {
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
throw err
}
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
})
it('mounts component with named slot if passed component in slot object', () => {
const wrapper = mountingMethod(ComponentWithSlots, {
slots: {
header: [Component],
footer: [Component]
}
})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<header><div></div></header> <main></main> <footer><div></div></footer>')
} else {
expect(wrapper.findAll(Component).length).to.equal(2)
}
})
it('mounts component with named slot if passed component in slot object', () => {
const wrapper = mountingMethod(ComponentWithSlots, {
slots: {
header: Component
}
})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<header><div></div></header>')
} else {
expect(wrapper.findAll(Component).length).to.equal(1)
expect(Array.isArray(wrapper.vm.$slots.header)).to.equal(true)
}
})
it('mounts functional component with default slot if passed component in slot object', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const wrapper = mountingMethod(TestComponent, { slots: { default: Component }})
if (mountingMethod.name === 'renderToString') {
const renderedAttribute = vueVersion < 2.3 ? 'server-rendered' : 'data-server-rendered'
expect(wrapper).contains(`<div ${renderedAttribute}="true"><div></div></div>`)
} else {
expect(wrapper.contains(Component)).to.equal(true)
}
})
it('mounts component with default slot if passed component in slot object', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const wrapper = mountingMethod(TestComponent, { slots: { default: [Component] }})
if (mountingMethod.name === 'renderToString') {
const renderedAttribute = vueVersion < 2.3 ? 'server-rendered' : 'data-server-rendered'
expect(wrapper).contains(`<div ${renderedAttribute}="true"><div></div></div>`)
} else {
expect(wrapper.contains(Component)).to.equal(true)
}
})
it('mounts component with default slot if passed object with template prop in slot object', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const compiled = compileToFunctions('<div id="div" />')
const wrapper = mountingMethod(TestComponent, { slots: { default: [compiled] }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<div id="div">')
} else {
expect(wrapper.contains('#div')).to.equal(true)
}
})
itDoNotRunIf(process.env.TEST_ENV === 'node' || isRunningPhantomJS,
'mounts component with default slot if passed string in slot object', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const wrapper = mountingMethod(TestComponent, { slots: { default: '<span />' }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<span')
} else {
expect(wrapper.contains('span')).to.equal(true)
}
})
itDoNotRunIf(
process.env.TEST_ENV === 'node' || isRunningPhantomJS,
'mounts component with named slot if passed string in slot object', () => {
const TestComponent = {
functional: true,
render: (h, ctx) => h('div', {}, ctx.slots().named)
}
const wrapper = mountingMethod(TestComponent, { slots: { named: Component }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<div></div>')
} else {
expect(wrapper.contains(Component)).to.equal(true)
}
})
itDoNotRunIf(
process.env.TEST_ENV === 'node' || isRunningPhantomJS,
'mounts component with named slot if passed string in slot object in array', () => {
const TestComponent = {
functional: true,
render: (h, ctx) => h('div', {}, ctx.slots().named)
}
const wrapper = mountingMethod(TestComponent, { slots: { named: [Component] }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<div></div>')
} else {
expect(wrapper.contains(Component)).to.equal(true)
}
})
itDoNotRunIf(
process.env.TEST_ENV === 'node' || isRunningPhantomJS,
'mounts component with named slot if passed string in slot object in array', () => {
const TestComponent = {
functional: true,
render: (h, ctx) => h('div', {}, ctx.slots().named)
}
const wrapper = mountingMethod(TestComponent, { slots: { named: '<span />' }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<span')
} else {
expect(wrapper.contains('span')).to.equal(true)
}
})
itDoNotRunIf(
process.env.TEST_ENV === 'node' || isRunningPhantomJS,
'mounts component with named slot if passed string in slot object in array', () => {
const TestComponent = {
functional: true,
render: (h, ctx) => h('div', {}, ctx.slots().named)
}
const wrapper = mountingMethod(TestComponent, { slots: { named: ['<span />'] }})
if (mountingMethod.name === 'renderToString') {
expect(wrapper).contains('<span')
} else {
expect(wrapper.contains('span')).to.equal(true)
}
})
it('throws error if passed false for named slots', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const fn = () => mountingMethod(TestComponent, { slots: { named: [false] }})
const message = '[vue-test-utils]: slots[key] must be a Component, string or an array of Components'
expect(fn).to.throw().with.property('message', message)
})
it('throws error if passed a number for named slots', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const fn = () => mountingMethod(TestComponent, { slots: { named: [1] }})
const message = '[vue-test-utils]: slots[key] must be a Component, string or an array of Components'
expect(fn).to.throw().with.property('message', message)
})
it('throws error if passed false for named slots', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const fn = () => mountingMethod(TestComponent, { slots: { named: false }})
const message = '[vue-test-utils]: slots[key] must be a Component, string or an array of Components'
expect(fn).to.throw().with.property('message', message)
})
it('throws error if passed a number for named slots', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const fn = () => mountingMethod(TestComponent, { slots: { named: 1 }})
const message = '[vue-test-utils]: slots[key] must be a Component, string or an array of Components'
expect(fn).to.throw().with.property('message', message)
})
itSkipIf(mountingMethod.name === 'renderToString',
'throws error if passed string in default slot array when vue-template-compiler is undefined', () => {
const TestComponent = {
name: 'component-with-slots',
functional: true,
render: (h, ctx) => h('div', ctx.data, ctx.slots().default)
}
const compilerSave = require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = undefined
delete require.cache[require.resolve('../../../packages/test-utils')]
const mountingMethodFresh = require('../../../packages/test-utils')[mountingMethod.name]
const message = '[vue-test-utils]: vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined'
const fn = () => mountingMethodFresh(TestComponent, { slots: { default: ['<span />'] }})
try {
expect(fn).to.throw().with.property('message', message)
} catch (err) {
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
throw err
}
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
})
itDoNotRunIf(
mountingMethod.name === 'renderToString' || isRunningPhantomJS,
'does not error when triggering a click in a slot', () => {
const Parent = {
name: 'Parent',
template: `
<Child>
<template slot-scope="props">
<slot name="content"></slot>
</template>
</Child>
`
}
const Child = {
name: 'Child',
template: `
<div>
<slot val="123"></slot>
</div>
`
}
const wrapper = mountingMethod(Parent, {
slots: { content: '*parent content!*' },
stubs: { Child }
})
wrapper.find('div').trigger('click')
})
})