-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathslots.spec.js
270 lines (244 loc) · 12.2 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
import { compileToFunctions } from 'vue-template-compiler'
import { mount } from '~vue-test-utils'
import Component from '~resources/components/component.vue'
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
describe('mount.slots', () => {
let _window
beforeEach(() => {
_window = window
})
afterEach(() => {
if (!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 = mount(ComponentWithSlots, { slots: { default: Component }})
expect(wrapper.contains(Component)).to.equal(true)
})
it('mounts component with default slot if passed component in array in slot object', () => {
const wrapper = mount(ComponentWithSlots, { slots: { default: [Component] }})
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 = mount(ComponentWithSlots, { slots: { default: [compiled] }})
expect(wrapper.contains('#div')).to.equal(true)
})
it('mounts component with default slot if passed string in slot object', () => {
const wrapper = mount(ComponentWithSlots, { slots: { default: '<span />' }})
expect(wrapper.contains('span')).to.equal(true)
})
it('throws error if the UserAgent is PhantomJS when passed string is in slot object', () => {
if (window.navigator.userAgent.match(/Chrome/i)) {
return
}
window = { navigator: { userAgent: 'PhantomJS' }} // eslint-disable-line no-native-reassign
const message = '[vue-test-utils]: option.slots does not support strings PhantomJS. Please use Puppeteer, or pass a component'
const fn = () => mount(ComponentWithSlots, { slots: { default: 'foo' }})
expect(fn).to.throw().with.property('message', message)
})
it('mounts component with default slot if passed string in slot object', () => {
const wrapper1 = mount(ComponentWithSlots, { slots: { default: 'foo<span>123</span>{{ foo }}' }})
expect(wrapper1.find('main').html()).to.equal('<main>foo<span>123</span>bar</main>')
const wrapper2 = mount(ComponentWithSlots, { slots: { default: '<p>1</p>{{ foo }}2' }})
expect(wrapper2.find('main').html()).to.equal('<main><p>1</p>bar2</main>')
const wrapper3 = mount(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 = mount(ComponentWithSlots, { slots: { default: '123' }})
expect(wrapper4.find('main').html()).to.equal('<main>123</main>')
const wrapper5 = mount(ComponentWithSlots, { slots: { default: '1{{ foo }}2' }})
expect(wrapper5.find('main').html()).to.equal('<main>1bar2</main>')
wrapper5.trigger('keydown')
expect(wrapper5.find('main').html()).to.equal('<main>1BAR2</main>')
const wrapper6 = mount(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 = mount(ComponentWithSlots, { slots: { default: '1<p>2</p>3' }})
expect(wrapper7.find('main').html()).to.equal('<main>1<p>2</p>3</main>')
})
it('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('../../../../../src/mount')]
const mountFresh = require('../../../../../src/mount').default
const message = '[vue-test-utils]: vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined'
const fn = () => mountFresh(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 default slot if passed string in slot array object', () => {
const wrapper = mount(ComponentWithSlots, { slots: { default: ['<span />'] }})
expect(wrapper.contains('span')).to.equal(true)
})
it('mounts component with default slot if passed string in slot text array object', () => {
const wrapper = mount(ComponentWithSlots, { slots: { default: ['{{ foo }}<span>1</span>', 'bar'] }})
expect(wrapper.find('main').html()).to.equal('<main>bar<span>1</span>bar</main>')
})
it('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('../../../../../src/mount')]
const mountFresh = require('../../../../../src/mount').default
const message = '[vue-test-utils]: vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined'
const fn = () => mountFresh(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 = mount(ComponentWithSlots, {
slots: {
header: [Component],
footer: [Component]
}
})
expect(wrapper.findAll(Component).length).to.equal(2)
})
it('mounts component with named slot if passed component in slot object', () => {
const wrapper = mount(ComponentWithSlots, {
slots: {
header: Component
}
})
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 = mount(TestComponent, { slots: { default: Component }})
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 = mount(TestComponent, { slots: { default: [Component] }})
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 = mount(TestComponent, { slots: { default: [compiled] }})
expect(wrapper.contains('#div')).to.equal(true)
})
it('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 = mount(TestComponent, { slots: { default: '<span />' }})
expect(wrapper.contains('span')).to.equal(true)
})
it('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 = mount(TestComponent, { slots: { named: Component }})
expect(wrapper.contains(Component)).to.equal(true)
})
it('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 = mount(TestComponent, { slots: { named: [Component] }})
expect(wrapper.contains(Component)).to.equal(true)
})
it('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 = mount(TestComponent, { slots: { named: '<span />' }})
expect(wrapper.contains('span')).to.equal(true)
})
it('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 = mount(TestComponent, { slots: { named: ['<span />'] }})
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 = () => mount(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 = () => mount(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 = () => mount(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 = () => mount(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 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('../../../../../src/mount')]
const mountFresh = require('../../../../../src/mount').default
const message = '[vue-test-utils]: vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined'
const fn = () => mountFresh(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
})
})