forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOMPropertyOperations-test.js
More file actions
552 lines (491 loc) · 22.5 KB
/
DOMPropertyOperations-test.js
File metadata and controls
552 lines (491 loc) · 22.5 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
// Set by `yarn test-fire`.
const {disableInputAttributeSyncing} = require('shared/ReactFeatureFlags');
describe('DOMPropertyOperations', () => {
let React;
let ReactDOM;
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
});
describe('setValueForProperty', () => {
it('should set values as properties by default', () => {
const container = document.createElement('div');
ReactDOM.render(<div title="Tip!" />, container);
expect(container.firstChild.title).toBe('Tip!');
});
it('should set values as attributes if necessary', () => {
const container = document.createElement('div');
ReactDOM.render(<div role="#" />, container);
expect(container.firstChild.getAttribute('role')).toBe('#');
expect(container.firstChild.role).toBeUndefined();
});
it('should set values as namespace attributes if necessary', () => {
const container = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg',
);
ReactDOM.render(<image xlinkHref="about:blank" />, container);
expect(
container.firstChild.getAttributeNS(
'http://www.w3.org/1999/xlink',
'href',
),
).toBe('about:blank');
});
it('should set values as boolean properties', () => {
const container = document.createElement('div');
ReactDOM.render(<div disabled="disabled" />, container);
expect(container.firstChild.getAttribute('disabled')).toBe('');
ReactDOM.render(<div disabled={true} />, container);
expect(container.firstChild.getAttribute('disabled')).toBe('');
ReactDOM.render(<div disabled={false} />, container);
expect(container.firstChild.getAttribute('disabled')).toBe(null);
ReactDOM.render(<div disabled={true} />, container);
ReactDOM.render(<div disabled={null} />, container);
expect(container.firstChild.getAttribute('disabled')).toBe(null);
ReactDOM.render(<div disabled={true} />, container);
ReactDOM.render(<div disabled={undefined} />, container);
expect(container.firstChild.getAttribute('disabled')).toBe(null);
});
it('should convert attribute values to string first', () => {
// Browsers default to this behavior, but some test environments do not.
// This ensures that we have consistent behavior.
const obj = {
toString: function() {
return 'css-class';
},
};
const container = document.createElement('div');
ReactDOM.render(<div className={obj} />, container);
expect(container.firstChild.getAttribute('class')).toBe('css-class');
});
it('should not remove empty attributes for special input properties', () => {
const container = document.createElement('div');
ReactDOM.render(<input value="" onChange={() => {}} />, container);
if (disableInputAttributeSyncing) {
expect(container.firstChild.hasAttribute('value')).toBe(false);
} else {
expect(container.firstChild.getAttribute('value')).toBe('');
}
expect(container.firstChild.value).toBe('');
});
it('should not remove empty attributes for special option properties', () => {
const container = document.createElement('div');
ReactDOM.render(
<select>
<option value="">empty</option>
<option>filled</option>
</select>,
container,
);
// Regression test for https://github.com/facebook/react/issues/6219
expect(container.firstChild.firstChild.value).toBe('');
expect(container.firstChild.lastChild.value).toBe('filled');
});
it('should remove for falsey boolean properties', () => {
const container = document.createElement('div');
ReactDOM.render(<div allowFullScreen={false} />, container);
expect(container.firstChild.hasAttribute('allowFullScreen')).toBe(false);
});
it('should remove when setting custom attr to null', () => {
const container = document.createElement('div');
ReactDOM.render(<div data-foo="bar" />, container);
expect(container.firstChild.hasAttribute('data-foo')).toBe(true);
ReactDOM.render(<div data-foo={null} />, container);
expect(container.firstChild.hasAttribute('data-foo')).toBe(false);
});
it('should set className to empty string instead of null', () => {
const container = document.createElement('div');
ReactDOM.render(<div className="selected" />, container);
expect(container.firstChild.className).toBe('selected');
ReactDOM.render(<div className={null} />, container);
// className should be '', not 'null' or null (which becomes 'null' in
// some browsers)
expect(container.firstChild.className).toBe('');
expect(container.firstChild.getAttribute('class')).toBe(null);
});
it('should remove property properly for boolean properties', () => {
const container = document.createElement('div');
ReactDOM.render(<div hidden={true} />, container);
expect(container.firstChild.hasAttribute('hidden')).toBe(true);
ReactDOM.render(<div hidden={false} />, container);
expect(container.firstChild.hasAttribute('hidden')).toBe(false);
});
it('should always assign the value attribute for non-inputs', function() {
const container = document.createElement('div');
ReactDOM.render(<progress />, container);
spyOnDevAndProd(container.firstChild, 'setAttribute');
ReactDOM.render(<progress value={30} />, container);
ReactDOM.render(<progress value="30" />, container);
expect(container.firstChild.setAttribute).toHaveBeenCalledTimes(2);
});
it('should return the progress to intermediate state on null value', () => {
const container = document.createElement('div');
ReactDOM.render(<progress value={30} />, container);
ReactDOM.render(<progress value={null} />, container);
// Ensure we move progress back to an indeterminate state.
// Regression test for https://github.com/facebook/react/issues/6119
expect(container.firstChild.hasAttribute('value')).toBe(false);
});
// @gate enableCustomElementPropertySupport
it('custom element custom events lowercase', () => {
const oncustomevent = jest.fn();
function Test() {
return <my-custom-element oncustomevent={oncustomevent} />;
}
const container = document.createElement('div');
ReactDOM.render(<Test />, container);
container
.querySelector('my-custom-element')
.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
});
// @gate enableCustomElementPropertySupport
it('custom element custom events uppercase', () => {
const oncustomevent = jest.fn();
function Test() {
return <my-custom-element onCustomevent={oncustomevent} />;
}
const container = document.createElement('div');
ReactDOM.render(<Test />, container);
container
.querySelector('my-custom-element')
.dispatchEvent(new Event('Customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
});
// @gate enableCustomElementPropertySupport
it('custom element custom event with dash in name', () => {
const oncustomevent = jest.fn();
function Test() {
return <my-custom-element oncustom-event={oncustomevent} />;
}
const container = document.createElement('div');
ReactDOM.render(<Test />, container);
container
.querySelector('my-custom-element')
.dispatchEvent(new Event('custom-event'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
});
// @gate enableCustomElementPropertySupport
it('custom element remove event handler', () => {
const oncustomevent = jest.fn();
function Test(props) {
return <my-custom-element oncustomevent={props.handler} />;
}
const container = document.createElement('div');
ReactDOM.render(<Test handler={oncustomevent} />, container);
const customElement = container.querySelector('my-custom-element');
customElement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
ReactDOM.render(<Test handler={false} />, container);
// Make sure that the second render didn't create a new element. We want
// to make sure removeEventListener actually gets called on the same element.
expect(customElement).toBe(customElement);
customElement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
ReactDOM.render(<Test handler={oncustomevent} />, container);
customElement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(2);
const oncustomevent2 = jest.fn();
ReactDOM.render(<Test handler={oncustomevent2} />, container);
customElement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(2);
expect(oncustomevent2).toHaveBeenCalledTimes(1);
});
it('custom elements shouldnt have non-functions for on* attributes treated as event listeners', () => {
const container = document.createElement('div');
ReactDOM.render(
<my-custom-element
onstring={'hello'}
onobj={{hello: 'world'}}
onarray={['one', 'two']}
ontrue={true}
onfalse={false}
/>,
container,
);
const customElement = container.querySelector('my-custom-element');
expect(customElement.getAttribute('onstring')).toBe('hello');
expect(customElement.getAttribute('onobj')).toBe('[object Object]');
expect(customElement.getAttribute('onarray')).toBe('one,two');
expect(customElement.getAttribute('ontrue')).toBe('true');
expect(customElement.getAttribute('onfalse')).toBe('false');
// Dispatch the corresponding event names to make sure that nothing crashes.
customElement.dispatchEvent(new Event('string'));
customElement.dispatchEvent(new Event('obj'));
customElement.dispatchEvent(new Event('array'));
customElement.dispatchEvent(new Event('true'));
customElement.dispatchEvent(new Event('false'));
});
it('custom elements should still have onClick treated like regular elements', () => {
let syntheticClickEvent = null;
const syntheticEventHandler = jest.fn(
event => (syntheticClickEvent = event),
);
let nativeClickEvent = null;
const nativeEventHandler = jest.fn(event => (nativeClickEvent = event));
function Test() {
return <my-custom-element onClick={syntheticEventHandler} />;
}
const container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render(<Test />, container);
const customElement = container.querySelector('my-custom-element');
customElement.onclick = nativeEventHandler;
container.querySelector('my-custom-element').click();
expect(nativeEventHandler).toHaveBeenCalledTimes(1);
expect(syntheticEventHandler).toHaveBeenCalledTimes(1);
expect(syntheticClickEvent.nativeEvent).toBe(nativeClickEvent);
});
// @gate enableCustomElementPropertySupport
it('custom elements should allow custom events with capture event listeners', () => {
const oncustomeventCapture = jest.fn();
const oncustomevent = jest.fn();
function Test() {
return (
<my-custom-element
oncustomeventCapture={oncustomeventCapture}
oncustomevent={oncustomevent}>
<div />
</my-custom-element>
);
}
const container = document.createElement('div');
ReactDOM.render(<Test />, container);
container
.querySelector('my-custom-element > div')
.dispatchEvent(new Event('customevent', {bubbles: false}));
expect(oncustomeventCapture).toHaveBeenCalledTimes(1);
expect(oncustomevent).toHaveBeenCalledTimes(0);
});
it('innerHTML should not work on custom elements', () => {
const container = document.createElement('div');
ReactDOM.render(<my-custom-element innerHTML="foo" />, container);
const customElement = container.querySelector('my-custom-element');
expect(customElement.getAttribute('innerHTML')).toBe(null);
expect(customElement.hasChildNodes()).toBe(false);
// Render again to verify the update codepath doesn't accidentally let
// something through.
ReactDOM.render(<my-custom-element innerHTML="bar" />, container);
expect(customElement.getAttribute('innerHTML')).toBe(null);
expect(customElement.hasChildNodes()).toBe(false);
});
// @gate enableCustomElementPropertySupport
it('innerText should not work on custom elements', () => {
const container = document.createElement('div');
ReactDOM.render(<my-custom-element innerText="foo" />, container);
const customElement = container.querySelector('my-custom-element');
expect(customElement.getAttribute('innerText')).toBe(null);
expect(customElement.hasChildNodes()).toBe(false);
// Render again to verify the update codepath doesn't accidentally let
// something through.
ReactDOM.render(<my-custom-element innerText="bar" />, container);
expect(customElement.getAttribute('innerText')).toBe(null);
expect(customElement.hasChildNodes()).toBe(false);
});
// @gate enableCustomElementPropertySupport
it('textContent should not work on custom elements', () => {
const container = document.createElement('div');
ReactDOM.render(<my-custom-element textContent="foo" />, container);
const customElement = container.querySelector('my-custom-element');
expect(customElement.getAttribute('textContent')).toBe(null);
expect(customElement.hasChildNodes()).toBe(false);
// Render again to verify the update codepath doesn't accidentally let
// something through.
ReactDOM.render(<my-custom-element textContent="bar" />, container);
expect(customElement.getAttribute('textContent')).toBe(null);
expect(customElement.hasChildNodes()).toBe(false);
});
// @gate enableCustomElementPropertySupport
it('values should not be converted to booleans when assigning into custom elements', () => {
const container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render(<my-custom-element />, container);
const customElement = container.querySelector('my-custom-element');
customElement.foo = null;
// true => string
ReactDOM.render(<my-custom-element foo={true} />, container);
expect(customElement.foo).toBe(true);
ReactDOM.render(<my-custom-element foo="bar" />, container);
expect(customElement.foo).toBe('bar');
// false => string
ReactDOM.render(<my-custom-element foo={false} />, container);
expect(customElement.foo).toBe(false);
ReactDOM.render(<my-custom-element foo="bar" />, container);
expect(customElement.foo).toBe('bar');
// true => null
ReactDOM.render(<my-custom-element foo={true} />, container);
expect(customElement.foo).toBe(true);
ReactDOM.render(<my-custom-element foo={null} />, container);
expect(customElement.foo).toBe(null);
// false => null
ReactDOM.render(<my-custom-element foo={false} />, container);
expect(customElement.foo).toBe(false);
ReactDOM.render(<my-custom-element foo={null} />, container);
expect(customElement.foo).toBe(null);
});
// @gate enableCustomElementPropertySupport
it('custom element custom event handlers assign multiple types', () => {
const container = document.createElement('div');
document.body.appendChild(container);
const oncustomevent = jest.fn();
// First render with string
ReactDOM.render(<my-custom-element oncustomevent={'foo'} />, container);
const customelement = container.querySelector('my-custom-element');
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(0);
expect(customelement.oncustomevent).toBe(undefined);
expect(customelement.getAttribute('oncustomevent')).toBe('foo');
// string => event listener
ReactDOM.render(
<my-custom-element oncustomevent={oncustomevent} />,
container,
);
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
expect(customelement.oncustomevent).toBe(undefined);
expect(customelement.getAttribute('oncustomevent')).toBe(null);
// event listener => string
ReactDOM.render(<my-custom-element oncustomevent={'foo'} />, container);
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
expect(customelement.oncustomevent).toBe(undefined);
expect(customelement.getAttribute('oncustomevent')).toBe('foo');
// string => nothing
ReactDOM.render(<my-custom-element />, container);
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
expect(customelement.oncustomevent).toBe(undefined);
expect(customelement.getAttribute('oncustomevent')).toBe(null);
// nothing => event listener
ReactDOM.render(
<my-custom-element oncustomevent={oncustomevent} />,
container,
);
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(2);
expect(customelement.oncustomevent).toBe(undefined);
expect(customelement.getAttribute('oncustomevent')).toBe(null);
});
// @gate enableCustomElementPropertySupport
it('custom element custom event handlers assign multiple types with setter', () => {
const container = document.createElement('div');
document.body.appendChild(container);
const oncustomevent = jest.fn();
// First render with nothing
ReactDOM.render(<my-custom-element />, container);
const customelement = container.querySelector('my-custom-element');
// Install a setter to activate the `in` heuristic
Object.defineProperty(customelement, 'oncustomevent', {
set: function(x) {
this._oncustomevent = x;
},
get: function() {
return this._oncustomevent;
},
});
expect(customelement.oncustomevent).toBe(undefined);
// nothing => event listener
ReactDOM.render(
<my-custom-element oncustomevent={oncustomevent} />,
container,
);
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
expect(customelement.oncustomevent).toBe(null);
expect(customelement.getAttribute('oncustomevent')).toBe(null);
// event listener => string
ReactDOM.render(<my-custom-element oncustomevent={'foo'} />, container);
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(1);
expect(customelement.oncustomevent).toBe('foo');
expect(customelement.getAttribute('oncustomevent')).toBe(null);
// string => event listener
ReactDOM.render(
<my-custom-element oncustomevent={oncustomevent} />,
container,
);
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(2);
expect(customelement.oncustomevent).toBe(null);
expect(customelement.getAttribute('oncustomevent')).toBe(null);
// event listener => nothing
ReactDOM.render(<my-custom-element />, container);
customelement.dispatchEvent(new Event('customevent'));
expect(oncustomevent).toHaveBeenCalledTimes(2);
expect(customelement.oncustomevent).toBe(null);
expect(customelement.getAttribute('oncustomevent')).toBe(null);
});
// @gate enableCustomElementPropertySupport
it('assigning to a custom element property should not remove attributes', () => {
const container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render(<my-custom-element foo="one" />, container);
const customElement = container.querySelector('my-custom-element');
expect(customElement.getAttribute('foo')).toBe('one');
// Install a setter to activate the `in` heuristic
Object.defineProperty(customElement, 'foo', {
set: function(x) {
this._foo = x;
},
get: function() {
return this._foo;
},
});
ReactDOM.render(<my-custom-element foo="two" />, container);
expect(customElement.foo).toBe('two');
expect(customElement.getAttribute('foo')).toBe('one');
});
});
describe('deleteValueForProperty', () => {
it('should remove attributes for normal properties', () => {
const container = document.createElement('div');
ReactDOM.render(<div title="foo" />, container);
expect(container.firstChild.getAttribute('title')).toBe('foo');
ReactDOM.render(<div />, container);
expect(container.firstChild.getAttribute('title')).toBe(null);
});
it('should not remove attributes for special properties', () => {
const container = document.createElement('div');
ReactDOM.render(
<input type="text" value="foo" onChange={function() {}} />,
container,
);
if (disableInputAttributeSyncing) {
expect(container.firstChild.hasAttribute('value')).toBe(false);
} else {
expect(container.firstChild.getAttribute('value')).toBe('foo');
}
expect(container.firstChild.value).toBe('foo');
expect(() =>
ReactDOM.render(
<input type="text" onChange={function() {}} />,
container,
),
).toErrorDev(
'A component is changing a controlled input to be uncontrolled',
);
if (disableInputAttributeSyncing) {
expect(container.firstChild.hasAttribute('value')).toBe(false);
} else {
expect(container.firstChild.getAttribute('value')).toBe('foo');
}
expect(container.firstChild.value).toBe('foo');
});
it('should not remove attributes for custom component tag', () => {
const container = document.createElement('div');
ReactDOM.render(<my-icon size="5px" />, container);
expect(container.firstChild.getAttribute('size')).toBe('5px');
});
});
});