-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathprop.spec.ts
469 lines (408 loc) · 15 KB
/
prop.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { NOOP } from '@vue/shared'
import {
setDynamicProp as _setDynamicProp,
setAttr,
setClass,
setDOMProp,
setDynamicProps,
setHtml,
setText,
} from '../../src/dom/prop'
import { setStyle } from '../../src/dom/style'
import {
createComponentInstance,
setCurrentInstance,
} from '../../src/component'
import { getMetadata, recordPropMetadata } from '../../src/componentMetadata'
let removeComponentInstance = NOOP
beforeEach(() => {
const instance = createComponentInstance((() => {}) as any, {}, null)
const reset = setCurrentInstance(instance)
removeComponentInstance = () => {
reset()
removeComponentInstance = NOOP
}
})
afterEach(() => {
removeComponentInstance()
})
describe('patchProp', () => {
describe('recordPropMetadata', () => {
test('should record prop metadata', () => {
const node = {} as Node // the node is just a key
let prev = recordPropMetadata(node, 'class', 'foo')
expect(prev).toBeUndefined()
prev = recordPropMetadata(node, 'class', 'bar')
expect(prev).toBe('foo')
prev = recordPropMetadata(node, 'style', 'color: red')
expect(prev).toBeUndefined()
prev = recordPropMetadata(node, 'style', 'color: blue')
expect(prev).toBe('color: red')
expect(getMetadata(node)).toEqual([
{ class: 'bar', style: 'color: blue' },
{},
])
})
test('should have different metadata for different nodes', () => {
const node1 = {} as Node
const node2 = {} as Node
recordPropMetadata(node1, 'class', 'foo')
recordPropMetadata(node2, 'class', 'bar')
expect(getMetadata(node1)).toEqual([{ class: 'foo' }, {}])
expect(getMetadata(node2)).toEqual([{ class: 'bar' }, {}])
})
})
describe('setClass', () => {
test('should set class', () => {
const el = document.createElement('div')
setClass(el, 'foo')
expect(el.className).toBe('foo')
setClass(el, ['bar', 'baz'])
expect(el.className).toBe('bar baz')
setClass(el, { a: true, b: false })
expect(el.className).toBe('a')
})
})
describe('setStyle', () => {
test('should set style', () => {
const el = document.createElement('div')
setStyle(el, 'color: red')
expect(el.style.cssText).toBe('color: red;')
})
test('should work with camelCase', () => {
const el = document.createElement('div')
setStyle(el, { fontSize: '12px' })
expect(el.style.cssText).toBe('font-size: 12px;')
})
test('shoud set style with object and array property', () => {
const el = document.createElement('div')
setStyle(el, { color: 'red' })
expect(el.style.cssText).toBe('color: red;')
setStyle(el, [{ color: 'blue' }, { fontSize: '12px' }])
expect(el.style.cssText).toBe('color: blue; font-size: 12px;')
})
test('should remove if falsy value', () => {
const el = document.createElement('div')
setStyle(el, { color: undefined, borderRadius: null })
expect(el.style.cssText).toBe('')
setStyle(el, { color: 'red' })
expect(el.style.cssText).toBe('color: red;')
setStyle(el, { color: undefined, borderRadius: null })
expect(el.style.cssText).toBe('')
})
test('should work with !important', () => {
const el = document.createElement('div')
setStyle(el, { color: 'red !important' })
expect(el.style.cssText).toBe('color: red !important;')
})
test('should work with camelCase and !important', () => {
const el = document.createElement('div')
setStyle(el, { fontSize: '12px !important' })
expect(el.style.cssText).toBe('font-size: 12px !important;')
})
test('should work with multiple entries', () => {
const el = document.createElement('div')
setStyle(el, { color: 'red', marginRight: '10px' })
expect(el.style.getPropertyValue('color')).toBe('red')
expect(el.style.getPropertyValue('margin-right')).toBe('10px')
})
test('should patch with falsy style value', () => {
const el = document.createElement('div')
setStyle(el, { width: '100px' })
expect(el.style.cssText).toBe('width: 100px;')
setStyle(el, { width: 0 })
expect(el.style.cssText).toBe('width: 0px;')
})
test('should remove style attribute on falsy value', () => {
const el = document.createElement('div')
setStyle(el, { width: '100px' })
expect(el.style.cssText).toBe('width: 100px;')
setStyle(el, { width: undefined })
expect(el.style.cssText).toBe('')
setStyle(el, { width: '100px' })
expect(el.style.cssText).toBe('width: 100px;')
setStyle(el, null)
expect(el.hasAttribute('style')).toBe(false)
expect(el.style.cssText).toBe('')
})
test('should warn for trailing semicolons', () => {
const el = document.createElement('div')
setStyle(el, { color: 'red;' })
expect(
`Unexpected semicolon at the end of 'color' style value: 'red;'`,
).toHaveBeenWarned()
setStyle(el, { '--custom': '100; ' })
expect(
`Unexpected semicolon at the end of '--custom' style value: '100; '`,
).toHaveBeenWarned()
})
test('should not warn for trailing semicolons', () => {
const el = document.createElement('div')
setStyle(el, { '--custom': '100\\;' })
expect(el.style.getPropertyValue('--custom')).toBe('100\\;')
})
test('should work with shorthand properties', () => {
const el = document.createElement('div')
setStyle(el, { borderBottom: '1px solid red', border: '1px solid green' })
expect(el.style.border).toBe('1px solid green')
expect(el.style.borderBottom).toBe('1px solid green')
})
// 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]
},
},
}
}
test('should work with css custom properties', () => {
const el = mockElementWithStyle()
setStyle(el as any, { '--theme': 'red' })
expect(el.style.getPropertyValue('--theme')).toBe('red')
})
test('should auto vendor prefixing', () => {
const el = mockElementWithStyle()
setStyle(el as any, { transition: 'all 1s' })
expect(el.style.WebkitTransition).toBe('all 1s')
})
test('should work with multiple values', () => {
const el = mockElementWithStyle()
setStyle(el as any, { display: ['-webkit-box', '-ms-flexbox', 'flex'] })
expect(el.style.display).toBe('flex')
})
})
describe('setAttr', () => {
test('should set attribute', () => {
const el = document.createElement('div')
setAttr(el, 'id', 'foo')
expect(el.getAttribute('id')).toBe('foo')
setAttr(el, 'name', 'bar')
expect(el.getAttribute('name')).toBe('bar')
})
test('should remove attribute', () => {
const el = document.createElement('div')
setAttr(el, 'id', 'foo')
setAttr(el, 'data', 'bar')
expect(el.getAttribute('id')).toBe('foo')
expect(el.getAttribute('data')).toBe('bar')
setAttr(el, 'id', null)
expect(el.getAttribute('id')).toBeNull()
setAttr(el, 'data', undefined)
expect(el.getAttribute('data')).toBeNull()
})
test('should set boolean attribute to string', () => {
const el = document.createElement('div')
setAttr(el, 'disabled', true)
expect(el.getAttribute('disabled')).toBe('true')
setAttr(el, 'disabled', false)
expect(el.getAttribute('disabled')).toBe('false')
})
})
describe('setDOMProp', () => {
test('should set DOM property', () => {
const el = document.createElement('div')
setDOMProp(el, 'textContent', null)
expect(el.textContent).toBe('')
setDOMProp(el, 'textContent', 'foo')
expect(el.textContent).toBe('foo')
setDOMProp(el, 'innerHTML', null)
expect(el.innerHTML).toBe('')
setDOMProp(el, 'innerHTML', '<p>bar</p>')
expect(el.innerHTML).toBe('<p>bar</p>')
})
test('should set value prop', () => {
const el = document.createElement('input')
setDOMProp(el, 'value', 'foo')
expect(el.value).toBe('foo')
setDOMProp(el, 'value', null)
expect(el.value).toBe('')
expect(el.getAttribute('value')).toBe(null)
const obj = {}
setDOMProp(el, 'value', obj)
expect(el.value).toBe(obj.toString())
expect((el as any)._value).toBe(obj)
const option = document.createElement('option')
setDOMProp(option, 'textContent', 'foo')
expect(option.value).toBe('foo')
expect(option.getAttribute('value')).toBe(null)
setDOMProp(option, 'value', 'bar')
expect(option.textContent).toBe('foo')
expect(option.value).toBe('bar')
expect(option.getAttribute('value')).toBe('bar')
})
test('should be boolean prop', () => {
const el = document.createElement('select')
setDOMProp(el, 'multiple', '')
expect(el.multiple).toBe(true)
setDOMProp(el, 'multiple', null)
expect(el.multiple).toBe(false)
setDOMProp(el, 'multiple', true)
expect(el.multiple).toBe(true)
setDOMProp(el, 'multiple', 0)
expect(el.multiple).toBe(false)
setDOMProp(el, 'multiple', '0')
expect(el.multiple).toBe(true)
setDOMProp(el, 'multiple', false)
expect(el.multiple).toBe(false)
setDOMProp(el, 'multiple', 1)
expect(el.multiple).toBe(true)
setDOMProp(el, 'multiple', undefined)
expect(el.multiple).toBe(false)
})
test('should remove attribute when value is falsy', () => {
const el = document.createElement('div')
setDOMProp(el, 'id', '')
expect(el.hasAttribute('id')).toBe(true)
setDOMProp(el, 'id', null)
expect(el.hasAttribute('id')).toBe(false)
setDOMProp(el, 'id', '')
expect(el.hasAttribute('id')).toBe(true)
setDOMProp(el, 'id', undefined)
expect(el.hasAttribute('id')).toBe(false)
setDOMProp(el, 'id', '')
expect(el.hasAttribute('id')).toBe(true)
const img = document.createElement('img')
setDOMProp(img, 'width', '')
expect(img.hasAttribute('width')).toBe(false)
setDOMProp(img, 'width', 0)
expect(img.hasAttribute('width')).toBe(true)
setDOMProp(img, 'width', null)
expect(img.hasAttribute('width')).toBe(false)
setDOMProp(img, 'width', 0)
expect(img.hasAttribute('width')).toBe(true)
setDOMProp(img, 'width', undefined)
expect(img.hasAttribute('width')).toBe(false)
setDOMProp(img, 'width', 0)
expect(img.hasAttribute('width')).toBe(true)
})
test('should warn when set prop error', () => {
const el = document.createElement('div')
Object.defineProperty(el, 'someProp', {
set() {
throw new TypeError('Invalid type')
},
})
setDOMProp(el, 'someProp', 'foo')
expect(
`Failed setting prop "someProp" on <div>: value foo is invalid.`,
).toHaveBeenWarnedLast()
})
})
describe('setDynamicProp', () => {
const element = document.createElement('div')
function setDynamicProp(
key: string,
value: any,
el = element.cloneNode(true) as HTMLElement,
) {
_setDynamicProp(el, key, value)
return el
}
test('should be able to set id', () => {
let res = setDynamicProp('id', 'bar')
expect(res.id).toBe('bar')
})
test('should be able to set class', () => {
let res = setDynamicProp('class', 'foo')
expect(res.className).toBe('foo')
})
test('should be able to set style', () => {
let res = setDynamicProp('style', 'color: red')
expect(res.style.cssText).toBe('color: red;')
})
test('should be able to set .prop', () => {
let res = setDynamicProp('.foo', 'bar')
expect((res as any)['foo']).toBe('bar')
expect(res.getAttribute('foo')).toBeNull()
})
test('should be able to set ^attr', () => {
let res = setDynamicProp('^foo', 'bar')
expect(res.getAttribute('foo')).toBe('bar')
expect((res as any)['foo']).toBeUndefined()
})
test('should be able to set boolean prop', () => {
let res = setDynamicProp(
'disabled',
true,
document.createElement('button'),
)
expect(res.getAttribute('disabled')).toBe('')
setDynamicProp('disabled', false, res)
expect(res.getAttribute('disabled')).toBeNull()
})
// The function shouldSetAsProp has complete tests elsewhere,
// so here we only do a simple test.
test('should be able to set innerHTML and textContent', () => {
let res = setDynamicProp('innerHTML', '<p>bar</p>')
expect(res.innerHTML).toBe('<p>bar</p>')
res = setDynamicProp('textContent', 'foo')
expect(res.textContent).toBe('foo')
})
test.todo('should be able to set something on SVG')
})
describe('setDynamicProps', () => {
test('basic set dynamic props', () => {
const el = document.createElement('div')
setDynamicProps(el, { foo: 'val' }, { bar: 'val' })
expect(el.getAttribute('foo')).toBe('val')
expect(el.getAttribute('bar')).toBe('val')
})
test('should merge props', () => {
const el = document.createElement('div')
setDynamicProps(el, { foo: 'val' }, { foo: 'newVal' })
expect(el.getAttribute('foo')).toBe('newVal')
})
test('should reset old props', () => {
const el = document.createElement('div')
setDynamicProps(el, { foo: 'val' })
expect(el.attributes.length).toBe(1)
expect(el.getAttribute('foo')).toBe('val')
setDynamicProps(el, { bar: 'val' })
expect(el.attributes.length).toBe(1)
expect(el.getAttribute('bar')).toBe('val')
expect(el.getAttribute('foo')).toBeNull()
})
test('should reset old modifier props', () => {
const el = document.createElement('div')
setDynamicProps(el, { ['.foo']: 'val' })
expect((el as any).foo).toBe('val')
setDynamicProps(el, { ['.bar']: 'val' })
expect((el as any).bar).toBe('val')
expect((el as any).foo).toBe('')
setDynamicProps(el, { ['^foo']: 'val' })
expect(el.attributes.length).toBe(1)
expect(el.getAttribute('foo')).toBe('val')
setDynamicProps(el, { ['^bar']: 'val' })
expect(el.attributes.length).toBe(1)
expect(el.getAttribute('bar')).toBe('val')
expect(el.getAttribute('foo')).toBeNull()
})
})
describe('setText', () => {
test('should set textContent', () => {
const el = document.createElement('div')
setText(el, 'foo')
expect(el.textContent).toBe('foo')
setText(el, 'bar')
expect(el.textContent).toBe('bar')
})
})
describe('setHtml', () => {
test('should set innerHTML', () => {
const el = document.createElement('div')
setHtml(el, '<p>foo</p>')
expect(el.innerHTML).toBe('<p>foo</p>')
setHtml(el, '<p>bar</p>')
expect(el.innerHTML).toBe('<p>bar</p>')
})
})
})