forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatchStyle.spec.ts
140 lines (124 loc) · 4.18 KB
/
patchStyle.spec.ts
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
import { patchProp } from '../src/patchProp'
describe(`runtime-dom: style patching`, () => {
it('string', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, 'color:red')
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
})
// #1309
it('should not patch same string style', () => {
const el = document.createElement('div')
const fn = jest.fn()
const value = (el.style.cssText = 'color:red;')
Object.defineProperty(el.style, 'cssText', {
get(): any {
return value
},
set: fn
})
patchProp(el, 'style', value, value)
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
expect(fn).not.toBeCalled()
})
it('plain object', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, { color: 'red' })
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
})
it('camelCase', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, { marginRight: '10px' })
expect(el.style.cssText.replace(/\s/g, '')).toBe('margin-right:10px;')
})
it('remove if falsy value', () => {
const el = document.createElement('div')
patchProp(el, 'style', null, {
color: undefined,
borderRadius: null
})
expect(el.style.cssText.replace(/\s/g, '')).toBe('')
patchProp(
el,
'style',
{ color: 'red' },
{ color: null, borderRadius: undefined }
)
expect(el.style.cssText.replace(/\s/g, '')).toBe('')
})
it('!important', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, { color: 'red !important' })
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red!important;')
})
it('camelCase with !important', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, { marginRight: '10px !important' })
expect(el.style.cssText.replace(/\s/g, '')).toBe(
'margin-right:10px!important;'
)
})
it('object with multiple entries', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, { color: 'red', marginRight: '10px' })
expect(el.style.getPropertyValue('color')).toBe('red')
expect(el.style.getPropertyValue('margin-right')).toBe('10px')
})
it('patch with falsy style value', () => {
const el = document.createElement('div')
patchProp(el as any, 'style', { width: '100px' }, { width: 0 })
expect(el.style.width).toBe('0px')
})
it('should remove style attribute on falsy value', () => {
const el = document.createElement('div')
el.style.cssText = 'color: red;'
patchProp(el as any, 'style', {}, null)
expect(el.hasAttribute('style')).toBe(false)
expect(el.style.cssText).toBe('')
})
it('should not overwritten the same value', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, { left: '10px' })
expect(el.style.getPropertyValue('left')).toBe('10px')
el.style.left = '20px'
expect(el.style.getPropertyValue('left')).toBe('20px')
patchProp(el, 'style', { left: '10px' }, { left: '10px' })
expect(el.style.getPropertyValue('left')).toBe('20px')
})
// JSDOM doesn't support custom properties on style object so we have to
// mock it here.
function mockElementWithStyle() {
const store: any = {}
return {
style: {
display: '',
WebkitTransition: '',
setProperty(key: string, val: string) {
store[key] = val
},
getPropertyValue(key: string) {
return store[key]
}
}
}
}
it('CSS custom properties', () => {
const el = mockElementWithStyle()
patchProp(el as any, 'style', {}, { '--theme': 'red' } as any)
expect(el.style.getPropertyValue('--theme')).toBe('red')
})
it('auto vendor prefixing', () => {
const el = mockElementWithStyle()
patchProp(el as any, 'style', {}, { transition: 'all 1s' })
expect(el.style.WebkitTransition).toBe('all 1s')
})
it('multiple values', () => {
const el = mockElementWithStyle()
patchProp(
el as any,
'style',
{},
{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }
)
expect(el.style.display).toBe('flex')
})
})