forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscopedSlots.spec.js
207 lines (198 loc) · 6.35 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
import {
describeWithShallowAndMount,
vueVersion,
isRunningPhantomJS
} from '~resources/utils'
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
import { itSkipIf, itDoNotRunIf } from 'conditional-specs'
describeWithShallowAndMount('scopedSlots', mountingMethod => {
const windowSave = window
afterEach(() => {
window = windowSave // eslint-disable-line no-native-reassign
})
itDoNotRunIf(
vueVersion < 2.5 || isRunningPhantomJS,
'mounts component scoped slots in render function',
() => {
const destructuringWrapper = mountingMethod(
{
render: function () {
return this.$scopedSlots.default({
index: 1,
item: 'foo'
})
}
},
{
scopedSlots: {
default:
'<p slot-scope="{ index, item }">{{index}},{{item}}</p>'
}
}
)
expect(destructuringWrapper.html()).to.equal('<p>1,foo</p>')
const notDestructuringWrapper = mountingMethod(
{
render: function () {
return this.$scopedSlots.default({
index: 1,
item: 'foo'
})
}
},
{
scopedSlots: {
default:
'<p slot-scope="props">{{props.index}},{{props.item}}</p>'
}
}
)
expect(notDestructuringWrapper.html()).to.equal('<p>1,foo</p>')
}
)
itDoNotRunIf(
vueVersion < 2.5 || isRunningPhantomJS,
'mounts component scoped slots',
() => {
const wrapper = mountingMethod(ComponentWithScopedSlots, {
slots: { default: '<span>123</span>' },
scopedSlots: {
destructuring:
'<p slot-scope="{ index, item }">{{index}},{{item}}</p>',
list: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>',
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 || isRunningPhantomJS,
'throws exception when it is seted to a template tag at top',
() => {
const fn = () => {
mountingMethod(ComponentWithScopedSlots, {
scopedSlots: {
single: '<template></template>'
}
})
}
const message =
'[vue-test-utils]: the scopedSlots option does not support a template tag as the root element.'
expect(fn)
.to.throw()
.with.property('message', message)
}
)
itDoNotRunIf(
vueVersion >= 2.5 || isRunningPhantomJS,
'throws exception when vue version < 2.5',
() => {
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)
}
)
itDoNotRunIf(
vueVersion < 2.5,
'throws exception when using PhantomJS',
() => {
if (window.navigator.userAgent.match(/Chrome|PhantomJS/i)) {
return
}
window = { navigator: { userAgent: 'PhantomJS' }} // eslint-disable-line no-native-reassign
const fn = () => {
mountingMethod(ComponentWithScopedSlots, {
scopedSlots: {
list: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>'
}
})
}
const message =
'[vue-test-utils]: the scopedSlots option does not support PhantomJS. Please use Puppeteer, or pass a component.'
expect(fn)
.to.throw()
.with.property('message', message)
}
)
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 precompiled components if vue-template-compiler is undefined'
const fn = () => {
mountingMethodFresh(ComponentWithScopedSlots, {
scopedSlots: {
list: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>'
}
})
}
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
}
)
})