forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscopedSlots.spec.js
237 lines (225 loc) · 6.64 KB
/
scopedSlots.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
import {
describeWithShallowAndMount,
vueVersion
} from '~resources/utils'
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
import { itDoNotRunIf } from 'conditional-specs'
describeWithShallowAndMount('scopedSlots', mountingMethod => {
const windowSave = window
afterEach(() => {
window = windowSave // eslint-disable-line no-native-reassign
})
itDoNotRunIf(
vueVersion < 2.1,
'handles templates as the root node', () => {
const wrapper = mountingMethod({
template: '<div><slot name="single" :text="foo" :i="123"></slot></div>',
data: () => ({
foo: 'bar'
})
}, {
scopedSlots: {
single: '<template><p>{{props.text}},{{props.i}}</p></template>'
}
})
expect(wrapper.html()).to.equal('<div><p>bar,123</p></div>')
})
itDoNotRunIf(
vueVersion < 2.1,
'handles render functions', () => {
const wrapper = mountingMethod({
template: '<div><slot name="single" :text="foo" /></div>',
data: () => ({
foo: 'bar'
})
}, {
scopedSlots: {
single: function (props) {
return this.$createElement('p', props.text)
}
}
})
expect(wrapper.html()).to.equal('<div><p>bar</p></div>')
})
itDoNotRunIf(
vueVersion < 2.5,
'mounts component scoped slots in render function',
() => {
const destructuringWrapper = mountingMethod(
{
render: function () {
return this.$scopedSlots.default({
index: 1,
item: 'foo'
})
}
},
{
scopedSlots: {
default:
'<template slot-scope="{ index, item }"><p>{{index}},{{item}}</p></template>'
}
}
)
expect(destructuringWrapper.html()).to.equal('<p>1,foo</p>')
const notDestructuringWrapper = mountingMethod(
{
render: function () {
return this.$scopedSlots.named({
index: 1,
item: 'foo'
})
}
},
{
scopedSlots: {
named:
'<p slot-scope="foo">{{foo.index}},{{foo.item}}</p>'
}
}
)
expect(notDestructuringWrapper.html()).to.equal('<p>1,foo</p>')
}
)
itDoNotRunIf(
vueVersion < 2.5,
'mounts component scoped slots in render function which exists in functional component',
() => {
const destructuringWrapper = mountingMethod(
{
functional: true,
render: function (createElement, context) {
return context.data.scopedSlots.default({
index: 1,
item: 'foo'
})
}
},
{
scopedSlots: {
default:
'<template slot-scope="{ index, item }"><p>{{index}},{{item}}</p></template>'
}
}
)
expect(destructuringWrapper.html()).to.equal('<p>1,foo</p>')
const notDestructuringWrapper = mountingMethod(
{
functional: true,
render: function (createElement, context) {
return context.data.scopedSlots.named({
index: 1,
item: 'foo'
})
}
},
{
scopedSlots: {
named:
'<p slot-scope="foo">{{foo.index}},{{foo.item}}</p>'
}
}
)
expect(notDestructuringWrapper.html()).to.equal('<p>1,foo</p>')
}
)
itDoNotRunIf(
vueVersion < 2.5,
'mounts component scoped slots',
() => {
const wrapper = mountingMethod(ComponentWithScopedSlots, {
slots: { default: '<span>123</span>' },
scopedSlots: {
destructuring:
'<p slot-scope="{ index, item }">{{index}},{{item}}</p>',
list: '<template slot-scope="foo"><p>{{foo.index}},{{foo.text}}</p></template>',
single: '<p slot-scope="bar">{{bar.text}}</p>',
noProps: '<p slot-scope="baz">baz</p>'
}
})
expect(wrapper.find('#destructuring').html()).to.equal(
'<div id="destructuring"><p>0,1</p><p>1,2</p><p>2,3</p></div>'
)
expect(wrapper.find('#slots').html()).to.equal(
'<div id="slots"><span>123</span></div>'
)
expect(wrapper.find('#list').html()).to.equal(
'<div id="list"><p>0,a1</p><p>1,a2</p><p>2,a3</p></div>'
)
expect(wrapper.find('#single').html()).to.equal(
'<div id="single"><p>abc</p></div>'
)
expect(wrapper.find('#noProps').html()).to.equal(
'<div id="noProps"><p>baz</p></div>'
)
wrapper.vm.items = [4, 5, 6]
wrapper.vm.foo = [{ text: 'b1' }, { text: 'b2' }, { text: 'b3' }]
wrapper.vm.bar = 'ABC'
expect(wrapper.find('#destructuring').html()).to.equal(
'<div id="destructuring"><p>0,4</p><p>1,5</p><p>2,6</p></div>'
)
expect(wrapper.find('#slots').html()).to.equal(
'<div id="slots"><span>123</span></div>'
)
expect(wrapper.find('#list').html()).to.equal(
'<div id="list"><p>0,b1</p><p>1,b2</p><p>2,b3</p></div>'
)
expect(wrapper.find('#single').html()).to.equal(
'<div id="single"><p>ABC</p></div>'
)
expect(wrapper.find('#noProps').html()).to.equal(
'<div id="noProps"><p>baz</p></div>'
)
}
)
itDoNotRunIf(
vueVersion < 2.5,
'handles JSX', () => {
const wrapper = mountingMethod({
template: '<div><slot name="single" :text="foo"></slot></div>',
data: () => ({
foo: 'bar'
})
}, {
scopedSlots: {
single ({ text }) {
return <p>{ text }</p>
}
}
})
expect(wrapper.html()).to.equal('<div><p>bar</p></div>')
})
itDoNotRunIf(
vueVersion < 2.5,
'handles no slot-scope', () => {
const wrapper = mountingMethod({
template: '<div><slot name="single" :text="foo" :i="123"></slot></div>',
data: () => ({
foo: 'bar'
})
}, {
scopedSlots: {
single: '<p>{{props.text}},{{props.i}}</p>'
}
})
expect(wrapper.html()).to.equal('<div><p>bar,123</p></div>')
})
itDoNotRunIf(
vueVersion > 2.0,
'throws exception when vue version < 2.1',
() => {
const fn = () => {
mountingMethod(ComponentWithScopedSlots, {
scopedSlots: {
list: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>'
}
})
}
const message =
'[vue-test-utils]: the scopedSlots option is only supported in [email protected]+.'
expect(fn)
.to.throw()
.with.property('message', message)
}
)
})