forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.spec.js
388 lines (359 loc) · 11.4 KB
/
style.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
import Vue from 'vue'
function checkPrefixedProp (prop) {
const el = document.createElement('div')
const upper = prop.charAt(0).toUpperCase() + prop.slice(1)
if (!(prop in el.style)) {
const prefixes = ['Webkit', 'Moz', 'ms']
let i = prefixes.length
while (i--) {
if ((prefixes[i] + upper) in el.style) {
prop = prefixes[i] + upper
}
}
}
return prop
}
describe('Directive v-bind:style', () => {
let vm
beforeEach(() => {
vm = new Vue({
template: '<div :style="styles"></div>',
data () {
return {
styles: {},
fontSize: 16
}
}
}).$mount()
})
it('string', done => {
vm.styles = 'color:red;'
waitForUpdate(() => {
expect(vm.$el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
}).then(done)
})
it('falsy number', done => {
vm.styles = { opacity: 0 }
waitForUpdate(() => {
expect(vm.$el.style.opacity).toBe('0')
}).then(done)
})
it('plain object', done => {
vm.styles = { color: 'red' }
waitForUpdate(() => {
expect(vm.$el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
}).then(done)
})
it('camelCase', done => {
vm.styles = { marginRight: '10px' }
waitForUpdate(() => {
expect(vm.$el.style.marginRight).toBe('10px')
}).then(done)
})
it('remove if falsy value', done => {
vm.$el.style.color = 'red'
waitForUpdate(() => {
vm.styles = { color: null }
}).then(() => {
expect(vm.$el.style.color).toBe('')
}).then(done)
})
it('ignore unsupported property', done => {
vm.styles = { foo: 'bar' }
waitForUpdate(() => {
expect(vm.$el.style.foo).not.toBe('bar')
}).then(done)
})
it('auto prefix', done => {
const prop = checkPrefixedProp('transform')
const val = 'scale(0.5)'
vm.styles = { transform: val }
waitForUpdate(() => {
expect(vm.$el.style[prop]).toBe(val)
}).then(done)
})
it('auto-prefixed style value as array', done => {
vm.styles = { display: ['-webkit-box', '-ms-flexbox', 'flex'] }
const testEl = document.createElement('div')
vm.styles.display.forEach(value => {
testEl.style.display = value
})
waitForUpdate(() => {
expect(vm.$el.style.display).toBe(testEl.style.display)
}).then(done)
})
it('!important', done => {
vm.styles = { display: 'block !important' }
waitForUpdate(() => {
expect(vm.$el.style.getPropertyPriority('display')).toBe('important')
}).then(done)
})
it('camelCase with !important', done => {
vm.styles = { zIndex: '100 !important' }
waitForUpdate(() => {
expect(vm.$el.style.getPropertyPriority('z-index')).toBe('important')
}).then(done)
})
it('object with multiple entries', done => {
vm.$el.style.color = 'red'
vm.styles = {
marginLeft: '10px',
marginRight: '15px'
}
waitForUpdate(() => {
expect(vm.$el.style.getPropertyValue('color')).toBe('red')
expect(vm.$el.style.getPropertyValue('margin-left')).toBe('10px')
expect(vm.$el.style.getPropertyValue('margin-right')).toBe('15px')
vm.styles = {
color: 'blue',
padding: null
}
}).then(() => {
expect(vm.$el.style.getPropertyValue('color')).toBe('blue')
expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
expect(vm.$el.style.getPropertyValue('margin-left')).toBeFalsy()
expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
// handle falsy value
vm.styles = null
}).then(() => {
expect(vm.$el.style.getPropertyValue('color')).toBeFalsy()
expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
expect(vm.$el.style.getPropertyValue('margin-left')).toBeFalsy()
expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
}).then(done)
})
it('array of objects', done => {
vm.$el.style.padding = '10px'
vm.styles = [{ color: 'red' }, { marginRight: '20px' }]
waitForUpdate(() => {
expect(vm.$el.style.getPropertyValue('color')).toBe('red')
expect(vm.$el.style.getPropertyValue('margin-right')).toBe('20px')
expect(vm.$el.style.getPropertyValue('padding')).toBe('10px')
vm.styles = [{ color: 'blue' }, { padding: null }]
}).then(() => {
expect(vm.$el.style.getPropertyValue('color')).toBe('blue')
expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
}).then(done)
})
it('updates objects deeply', done => {
vm.styles = { display: 'none' }
waitForUpdate(() => {
expect(vm.$el.style.display).toBe('none')
vm.styles.display = 'block'
}).then(() => {
expect(vm.$el.style.display).toBe('block')
}).then(done)
})
it('background size with only one value', done => {
vm.styles = { backgroundSize: '100%' }
waitForUpdate(() => {
expect(vm.$el.style.cssText.replace(/\s/g, '')).toMatch(/background-size:100%(auto)?;/)
}).then(done)
})
it('should work with interpolation', done => {
vm.styles = { fontSize: `${vm.fontSize}px` }
waitForUpdate(() => {
expect(vm.$el.style.fontSize).toBe('16px')
}).then(done)
})
const supportCssVariable = () => {
const el = document.createElement('div')
el.style.setProperty('--color', 'red')
return el.style.getPropertyValue('--color') === 'red'
}
if (supportCssVariable()) {
it('CSS variables', done => {
vm.styles = { '--color': 'red' }
waitForUpdate(() => {
expect(vm.$el.style.getPropertyValue('--color')).toBe('red')
}).then(done)
})
}
it('should merge static style with binding style', () => {
const vm = new Vue({
template: '<div style="background: url(https://vuejs.org/images/logo.png);color: blue" :style="test"></div>',
data: {
test: { color: 'red', fontSize: '12px' }
}
}).$mount()
const style = vm.$el.style
expect(style.getPropertyValue('background-image')).toMatch('https://vuejs.org/images/logo.png')
expect(style.getPropertyValue('color')).toBe('red')
expect(style.getPropertyValue('font-size')).toBe('12px')
})
it('should merge between parent and child', done => {
const vm = new Vue({
template: '<child style="text-align: left;margin-right:20px" :style="test"></child>',
data: {
test: { color: 'red', fontSize: '12px' }
},
components: {
child: {
template: '<div style="margin-right:10px;" :style="{marginLeft: marginLeft}"></div>',
data: () => ({ marginLeft: '16px' })
}
}
}).$mount()
const style = vm.$el.style
const child = vm.$children[0]
const css = style.cssText.replace(/\s/g, '')
expect(css).toContain('margin-right:20px;')
expect(css).toContain('margin-left:16px;')
expect(css).toContain('text-align:left;')
expect(css).toContain('color:red;')
expect(css).toContain('font-size:12px;')
expect(style.color).toBe('red')
expect(style.marginRight).toBe('20px')
vm.test.color = 'blue'
waitForUpdate(() => {
expect(style.color).toBe('blue')
child.marginLeft = '30px'
}).then(() => {
expect(style.marginLeft).toBe('30px')
child.fontSize = '30px'
}).then(() => {
expect(style.fontSize).toBe('12px')
}).then(done)
})
it('should not pass to child root element', () => {
const vm = new Vue({
template: '<child :style="test"></child>',
data: {
test: { color: 'red', fontSize: '12px' }
},
components: {
child: {
template: '<div><nested ref="nested" style="color: blue;text-align:left"></nested></div>',
components: {
nested: {
template: '<div></div>'
}
}
}
}
}).$mount()
const style = vm.$el.style
expect(style.color).toBe('red')
expect(style.textAlign).toBe('')
expect(style.fontSize).toBe('12px')
expect(vm.$children[0].$refs.nested.$el.style.color).toBe('blue')
})
it('should merge between nested components', (done) => {
const vm = new Vue({
template: '<child :style="test"></child>',
data: {
test: { color: 'red', fontSize: '12px' }
},
components: {
child: {
template: '<nested style="color: blue;text-align:left"></nested>',
components: {
nested: {
template: '<div style="margin-left: 12px;" :style="nestedStyle"></div>',
data: () => ({ nestedStyle: { marginLeft: '30px' }})
}
}
}
}
}).$mount()
const style = vm.$el.style
const child = vm.$children[0].$children[0]
expect(style.color).toBe('red')
expect(style.marginLeft).toBe('30px')
expect(style.textAlign).toBe('left')
expect(style.fontSize).toBe('12px')
vm.test.color = 'yellow'
waitForUpdate(() => {
child.nestedStyle.marginLeft = '60px'
}).then(() => {
expect(style.marginLeft).toBe('60px')
child.nestedStyle = {
fontSize: '14px',
marginLeft: '40px'
}
}).then(() => {
expect(style.fontSize).toBe('12px')
expect(style.marginLeft).toBe('40px')
}).then(done)
})
it('should not merge for different adjacent elements', (done) => {
const vm = new Vue({
template:
'<div>' +
'<section style="color: blue" :style="style" v-if="!bool"></section>' +
'<div></div>' +
'<section style="margin-top: 12px" v-if="bool"></section>' +
'</div>',
data: {
bool: false,
style: {
fontSize: '12px'
}
}
}).$mount()
const style = vm.$el.children[0].style
expect(style.fontSize).toBe('12px')
expect(style.color).toBe('blue')
waitForUpdate(() => {
vm.bool = true
}).then(() => {
expect(style.color).toBe('')
expect(style.fontSize).toBe('')
expect(style.marginTop).toBe('12px')
}).then(done)
})
it('should not merge for v-if, v-else-if and v-else elements', (done) => {
const vm = new Vue({
template:
'<div>' +
'<section style="color: blue" :style="style" v-if="foo"></section>' +
'<section style="margin-top: 12px" v-else-if="bar"></section>' +
'<section style="margin-bottom: 24px" v-else></section>' +
'<div></div>' +
'</div>',
data: {
foo: true,
bar: false,
style: {
fontSize: '12px'
}
}
}).$mount()
const style = vm.$el.children[0].style
expect(style.fontSize).toBe('12px')
expect(style.color).toBe('blue')
waitForUpdate(() => {
vm.foo = false
}).then(() => {
expect(style.color).toBe('')
expect(style.fontSize).toBe('')
expect(style.marginBottom).toBe('24px')
vm.bar = true
}).then(() => {
expect(style.color).toBe('')
expect(style.fontSize).toBe('')
expect(style.marginBottom).toBe('')
expect(style.marginTop).toBe('12px')
}).then(done)
})
// #5318
it('should work for elements passed down as a slot', done => {
const vm = new Vue({
template: `<test><div :style="style"/></test>`,
data: {
style: { color: 'red' }
},
components: {
test: {
template: `<div><slot/></div>`
}
}
}).$mount()
expect(vm.$el.children[0].style.color).toBe('red')
vm.style.color = 'green'
waitForUpdate(() => {
expect(vm.$el.children[0].style.color).toBe('green')
}).then(done)
})
})