-
Notifications
You must be signed in to change notification settings - Fork 48k
/
Copy pathReactDOMComponent-test.js
2723 lines (2329 loc) · 92.3 KB
/
ReactDOMComponent-test.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* 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';
describe('ReactDOMComponent', () => {
let React;
let ReactTestUtils;
let ReactDOM;
let ReactDOMServer;
function normalizeCodeLocInfo(str) {
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
}
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');
});
describe('updateDOM', () => {
it('should handle className', () => {
const container = document.createElement('div');
ReactDOM.render(<div style={{}} />, container);
ReactDOM.render(<div className={'foo'} />, container);
expect(container.firstChild.className).toEqual('foo');
ReactDOM.render(<div className={'bar'} />, container);
expect(container.firstChild.className).toEqual('bar');
ReactDOM.render(<div className={null} />, container);
expect(container.firstChild.className).toEqual('');
});
it('should gracefully handle various style value types', () => {
const container = document.createElement('div');
ReactDOM.render(<div style={{}} />, container);
const stubStyle = container.firstChild.style;
// set initial style
const setup = {
display: 'block',
left: '1px',
top: 2,
fontFamily: 'Arial',
};
ReactDOM.render(<div style={setup} />, container);
expect(stubStyle.display).toEqual('block');
expect(stubStyle.left).toEqual('1px');
expect(stubStyle.top).toEqual('2px');
expect(stubStyle.fontFamily).toEqual('Arial');
// reset the style to their default state
const reset = {display: '', left: null, top: false, fontFamily: true};
ReactDOM.render(<div style={reset} />, container);
expect(stubStyle.display).toEqual('');
expect(stubStyle.left).toEqual('');
expect(stubStyle.top).toEqual('');
expect(stubStyle.fontFamily).toEqual('');
});
it('should not update styles when mutating a proxy style object', () => {
const styleStore = {
display: 'none',
fontFamily: 'Arial',
lineHeight: 1.2,
};
// We use a proxy style object so that we can mutate it even if it is
// frozen in DEV.
const styles = {
get display() {
return styleStore.display;
},
set display(v) {
styleStore.display = v;
},
get fontFamily() {
return styleStore.fontFamily;
},
set fontFamily(v) {
styleStore.fontFamily = v;
},
get lineHeight() {
return styleStore.lineHeight;
},
set lineHeight(v) {
styleStore.lineHeight = v;
},
};
const container = document.createElement('div');
ReactDOM.render(<div style={styles} />, container);
const stubStyle = container.firstChild.style;
stubStyle.display = styles.display;
stubStyle.fontFamily = styles.fontFamily;
styles.display = 'block';
ReactDOM.render(<div style={styles} />, container);
expect(stubStyle.display).toEqual('none');
expect(stubStyle.fontFamily).toEqual('Arial');
expect(stubStyle.lineHeight).toEqual('1.2');
styles.fontFamily = 'Helvetica';
ReactDOM.render(<div style={styles} />, container);
expect(stubStyle.display).toEqual('none');
expect(stubStyle.fontFamily).toEqual('Arial');
expect(stubStyle.lineHeight).toEqual('1.2');
styles.lineHeight = 0.5;
ReactDOM.render(<div style={styles} />, container);
expect(stubStyle.display).toEqual('none');
expect(stubStyle.fontFamily).toEqual('Arial');
expect(stubStyle.lineHeight).toEqual('1.2');
ReactDOM.render(<div style={undefined} />, container);
expect(stubStyle.display).toBe('');
expect(stubStyle.fontFamily).toBe('');
expect(stubStyle.lineHeight).toBe('');
});
it('should throw when mutating style objects', () => {
const style = {border: '1px solid black'};
class App extends React.Component {
state = {style: style};
render() {
return <div style={this.state.style}>asd</div>;
}
}
ReactTestUtils.renderIntoDocument(<App />);
if (__DEV__) {
expect(() => (style.position = 'absolute')).toThrow();
}
});
it('should warn for unknown prop', () => {
spyOnDev(console, 'error');
const container = document.createElement('div');
ReactDOM.render(<div foo={() => {}} />, container);
if (__DEV__) {
expect(console.error.calls.count(0)).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Invalid value for prop `foo` on <div> tag. Either remove it ' +
'from the element, or pass a string or number value to keep ' +
'it in the DOM. For details, see https://fb.me/react-attribute-behavior' +
'\n in div (at **)',
);
}
});
it('should group multiple unknown prop warnings together', () => {
spyOnDev(console, 'error');
const container = document.createElement('div');
ReactDOM.render(<div foo={() => {}} baz={() => {}} />, container);
if (__DEV__) {
expect(console.error.calls.count(0)).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Invalid values for props `foo`, `baz` on <div> tag. Either remove ' +
'them from the element, or pass a string or number value to keep ' +
'them in the DOM. For details, see https://fb.me/react-attribute-behavior' +
'\n in div (at **)',
);
}
});
it('should warn for onDblClick prop', () => {
spyOnDev(console, 'error');
const container = document.createElement('div');
ReactDOM.render(<div onDblClick={() => {}} />, container);
if (__DEV__) {
expect(console.error.calls.count(0)).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Invalid event handler property `onDblClick`. Did you mean `onDoubleClick`?\n in div (at **)',
);
}
});
it('should warn for unknown string event handlers', () => {
spyOnDev(console, 'error');
const container = document.createElement('div');
ReactDOM.render(<div onUnknown="alert("hack")" />, container);
expect(container.firstChild.hasAttribute('onUnknown')).toBe(false);
expect(container.firstChild.onUnknown).toBe(undefined);
ReactDOM.render(<div onunknown="alert("hack")" />, container);
expect(container.firstChild.hasAttribute('onunknown')).toBe(false);
expect(container.firstChild.onunknown).toBe(undefined);
ReactDOM.render(<div on-unknown="alert("hack")" />, container);
expect(container.firstChild.hasAttribute('on-unknown')).toBe(false);
expect(container.firstChild['on-unknown']).toBe(undefined);
if (__DEV__) {
expect(console.error.calls.count(0)).toBe(3);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Unknown event handler property `onUnknown`. It will be ignored.\n in div (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
'Warning: Unknown event handler property `onunknown`. It will be ignored.\n in div (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(2)[0])).toBe(
'Warning: Unknown event handler property `on-unknown`. It will be ignored.\n in div (at **)',
);
}
});
it('should warn for unknown function event handlers', () => {
spyOnDev(console, 'error');
const container = document.createElement('div');
ReactDOM.render(<div onUnknown={function() {}} />, container);
expect(container.firstChild.hasAttribute('onUnknown')).toBe(false);
expect(container.firstChild.onUnknown).toBe(undefined);
ReactDOM.render(<div onunknown={function() {}} />, container);
expect(container.firstChild.hasAttribute('onunknown')).toBe(false);
expect(container.firstChild.onunknown).toBe(undefined);
ReactDOM.render(<div on-unknown={function() {}} />, container);
expect(container.firstChild.hasAttribute('on-unknown')).toBe(false);
expect(container.firstChild['on-unknown']).toBe(undefined);
if (__DEV__) {
expect(console.error.calls.count(0)).toBe(3);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Unknown event handler property `onUnknown`. It will be ignored.\n in div (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
'Warning: Unknown event handler property `onunknown`. It will be ignored.\n in div (at **)',
);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(2)[0])).toBe(
'Warning: Unknown event handler property `on-unknown`. It will be ignored.\n in div (at **)',
);
}
});
it('should warn for badly cased React attributes', () => {
spyOnDev(console, 'error');
const container = document.createElement('div');
ReactDOM.render(<div CHILDREN="5" />, container);
expect(container.firstChild.getAttribute('CHILDREN')).toBe('5');
if (__DEV__) {
expect(console.error.calls.count(0)).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Invalid DOM property `CHILDREN`. Did you mean `children`?\n in div (at **)',
);
}
});
it('should not warn for "0" as a unitless style value', () => {
class Component extends React.Component {
render() {
return <div style={{margin: '0'}} />;
}
}
ReactTestUtils.renderIntoDocument(<Component />);
});
it('should warn nicely about NaN in style', () => {
spyOnDev(console, 'error');
const style = {fontSize: NaN};
const div = document.createElement('div');
ReactDOM.render(<span style={style} />, div);
ReactDOM.render(<span style={style} />, div);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toEqual(
'Warning: `NaN` is an invalid value for the `fontSize` css style property.' +
'\n in span (at **)',
);
}
});
it('should update styles if initially null', () => {
let styles = null;
const container = document.createElement('div');
ReactDOM.render(<div style={styles} />, container);
const stubStyle = container.firstChild.style;
styles = {display: 'block'};
ReactDOM.render(<div style={styles} />, container);
expect(stubStyle.display).toEqual('block');
});
it('should update styles if updated to null multiple times', () => {
let styles = null;
const container = document.createElement('div');
ReactDOM.render(<div style={styles} />, container);
styles = {display: 'block'};
const stubStyle = container.firstChild.style;
ReactDOM.render(<div style={styles} />, container);
expect(stubStyle.display).toEqual('block');
ReactDOM.render(<div style={null} />, container);
expect(stubStyle.display).toEqual('');
ReactDOM.render(<div style={styles} />, container);
expect(stubStyle.display).toEqual('block');
ReactDOM.render(<div style={null} />, container);
expect(stubStyle.display).toEqual('');
});
it('should allow named slot projection on both web components and regular DOM elements', () => {
const container = document.createElement('div');
ReactDOM.render(
<my-component>
<my-second-component slot="first" />
<button slot="second">Hello</button>
</my-component>,
container,
);
const lightDOM = container.firstChild.childNodes;
expect(lightDOM[0].getAttribute('slot')).toBe('first');
expect(lightDOM[1].getAttribute('slot')).toBe('second');
});
it('should skip reserved props on web components', () => {
const container = document.createElement('div');
ReactDOM.render(
<my-component
children={['foo']}
suppressContentEditableWarning={true}
suppressHydrationWarning={true}
/>,
container,
);
expect(container.firstChild.hasAttribute('children')).toBe(false);
expect(
container.firstChild.hasAttribute('suppressContentEditableWarning'),
).toBe(false);
expect(
container.firstChild.hasAttribute('suppressHydrationWarning'),
).toBe(false);
ReactDOM.render(
<my-component
children={['bar']}
suppressContentEditableWarning={false}
suppressHydrationWarning={false}
/>,
container,
);
expect(container.firstChild.hasAttribute('children')).toBe(false);
expect(
container.firstChild.hasAttribute('suppressContentEditableWarning'),
).toBe(false);
expect(
container.firstChild.hasAttribute('suppressHydrationWarning'),
).toBe(false);
});
it('should skip dangerouslySetInnerHTML on web components', () => {
const container = document.createElement('div');
ReactDOM.render(
<my-component dangerouslySetInnerHTML={{__html: 'hi'}} />,
container,
);
expect(container.firstChild.hasAttribute('dangerouslySetInnerHTML')).toBe(
false,
);
ReactDOM.render(
<my-component dangerouslySetInnerHTML={{__html: 'bye'}} />,
container,
);
expect(container.firstChild.hasAttribute('dangerouslySetInnerHTML')).toBe(
false,
);
});
it('should render null and undefined as empty but print other falsy values', () => {
const container = document.createElement('div');
ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: 'textContent'}} />,
container,
);
expect(container.textContent).toEqual('textContent');
ReactDOM.render(<div dangerouslySetInnerHTML={{__html: 0}} />, container);
expect(container.textContent).toEqual('0');
ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: false}} />,
container,
);
expect(container.textContent).toEqual('false');
ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: ''}} />,
container,
);
expect(container.textContent).toEqual('');
ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: null}} />,
container,
);
expect(container.textContent).toEqual('');
ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: undefined}} />,
container,
);
expect(container.textContent).toEqual('');
});
it('should remove attributes', () => {
const container = document.createElement('div');
ReactDOM.render(<img height="17" />, container);
expect(container.firstChild.hasAttribute('height')).toBe(true);
ReactDOM.render(<img />, container);
expect(container.firstChild.hasAttribute('height')).toBe(false);
});
it('should remove properties', () => {
const container = document.createElement('div');
ReactDOM.render(<div className="monkey" />, container);
expect(container.firstChild.className).toEqual('monkey');
ReactDOM.render(<div />, container);
expect(container.firstChild.className).toEqual('');
});
it('should not set null/undefined attributes', () => {
var container = document.createElement('div');
// Initial render.
ReactDOM.render(<img src={null} data-foo={undefined} />, container);
var node = container.firstChild;
expect(node.hasAttribute('src')).toBe(false);
expect(node.hasAttribute('data-foo')).toBe(false);
// Update in one direction.
ReactDOM.render(<img src={undefined} data-foo={null} />, container);
expect(node.hasAttribute('src')).toBe(false);
expect(node.hasAttribute('data-foo')).toBe(false);
// Update in another direction.
ReactDOM.render(<img src={null} data-foo={undefined} />, container);
expect(node.hasAttribute('src')).toBe(false);
expect(node.hasAttribute('data-foo')).toBe(false);
// Removal.
ReactDOM.render(<img />, container);
expect(node.hasAttribute('src')).toBe(false);
expect(node.hasAttribute('data-foo')).toBe(false);
// Addition.
ReactDOM.render(<img src={undefined} data-foo={null} />, container);
expect(node.hasAttribute('src')).toBe(false);
expect(node.hasAttribute('data-foo')).toBe(false);
});
it('should apply React-specific aliases to HTML elements', () => {
var container = document.createElement('div');
ReactDOM.render(<form acceptCharset="foo" />, container);
var node = container.firstChild;
// Test attribute initialization.
expect(node.getAttribute('accept-charset')).toBe('foo');
expect(node.hasAttribute('acceptCharset')).toBe(false);
// Test attribute update.
ReactDOM.render(<form acceptCharset="boo" />, container);
expect(node.getAttribute('accept-charset')).toBe('boo');
expect(node.hasAttribute('acceptCharset')).toBe(false);
// Test attribute removal by setting to null.
ReactDOM.render(<form acceptCharset={null} />, container);
expect(node.hasAttribute('accept-charset')).toBe(false);
expect(node.hasAttribute('acceptCharset')).toBe(false);
// Restore.
ReactDOM.render(<form acceptCharset="foo" />, container);
expect(node.getAttribute('accept-charset')).toBe('foo');
expect(node.hasAttribute('acceptCharset')).toBe(false);
// Test attribute removal by setting to undefined.
ReactDOM.render(<form acceptCharset={undefined} />, container);
expect(node.hasAttribute('accept-charset')).toBe(false);
expect(node.hasAttribute('acceptCharset')).toBe(false);
// Restore.
ReactDOM.render(<form acceptCharset="foo" />, container);
expect(node.getAttribute('accept-charset')).toBe('foo');
expect(node.hasAttribute('acceptCharset')).toBe(false);
// Test attribute removal.
ReactDOM.render(<form />, container);
expect(node.hasAttribute('accept-charset')).toBe(false);
expect(node.hasAttribute('acceptCharset')).toBe(false);
});
it('should apply React-specific aliases to SVG elements', () => {
var container = document.createElement('div');
ReactDOM.render(<svg arabicForm="foo" />, container);
var node = container.firstChild;
// Test attribute initialization.
expect(node.getAttribute('arabic-form')).toBe('foo');
expect(node.hasAttribute('arabicForm')).toBe(false);
// Test attribute update.
ReactDOM.render(<svg arabicForm="boo" />, container);
expect(node.getAttribute('arabic-form')).toBe('boo');
expect(node.hasAttribute('arabicForm')).toBe(false);
// Test attribute removal by setting to null.
ReactDOM.render(<svg arabicForm={null} />, container);
expect(node.hasAttribute('arabic-form')).toBe(false);
expect(node.hasAttribute('arabicForm')).toBe(false);
// Restore.
ReactDOM.render(<svg arabicForm="foo" />, container);
expect(node.getAttribute('arabic-form')).toBe('foo');
expect(node.hasAttribute('arabicForm')).toBe(false);
// Test attribute removal by setting to undefined.
ReactDOM.render(<svg arabicForm={undefined} />, container);
expect(node.hasAttribute('arabic-form')).toBe(false);
expect(node.hasAttribute('arabicForm')).toBe(false);
// Restore.
ReactDOM.render(<svg arabicForm="foo" />, container);
expect(node.getAttribute('arabic-form')).toBe('foo');
expect(node.hasAttribute('arabicForm')).toBe(false);
// Test attribute removal.
ReactDOM.render(<svg />, container);
expect(node.hasAttribute('arabic-form')).toBe(false);
expect(node.hasAttribute('arabicForm')).toBe(false);
});
it('should properly update custom attributes on custom elements', () => {
const container = document.createElement('div');
ReactDOM.render(<some-custom-element foo="bar" />, container);
ReactDOM.render(<some-custom-element bar="buzz" />, container);
const node = container.firstChild;
expect(node.hasAttribute('foo')).toBe(false);
expect(node.getAttribute('bar')).toBe('buzz');
});
it('should not apply React-specific aliases to custom elements', () => {
var container = document.createElement('div');
ReactDOM.render(<some-custom-element arabicForm="foo" />, container);
var node = container.firstChild;
// Should not get transformed to arabic-form as SVG would be.
expect(node.getAttribute('arabicForm')).toBe('foo');
expect(node.hasAttribute('arabic-form')).toBe(false);
// Test attribute update.
ReactDOM.render(<some-custom-element arabicForm="boo" />, container);
expect(node.getAttribute('arabicForm')).toBe('boo');
// Test attribute removal and addition.
ReactDOM.render(<some-custom-element acceptCharset="buzz" />, container);
// Verify the previous attribute was removed.
expect(node.hasAttribute('arabicForm')).toBe(false);
// Should not get transformed to accept-charset as HTML would be.
expect(node.getAttribute('acceptCharset')).toBe('buzz');
expect(node.hasAttribute('accept-charset')).toBe(false);
});
it('should clear a single style prop when changing `style`', () => {
let styles = {display: 'none', color: 'red'};
const container = document.createElement('div');
ReactDOM.render(<div style={styles} />, container);
const stubStyle = container.firstChild.style;
styles = {color: 'green'};
ReactDOM.render(<div style={styles} />, container);
expect(stubStyle.display).toEqual('');
expect(stubStyle.color).toEqual('green');
});
it('should reject attribute key injection attack on markup', () => {
spyOnDev(console, 'error');
for (let i = 0; i < 3; i++) {
const container = document.createElement('div');
const element = React.createElement(
'x-foo-component',
{'blah" onclick="beevil" noise="hi': 'selected'},
null,
);
ReactDOM.render(element, container);
}
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Invalid attribute name: `blah" onclick="beevil" noise="hi`',
);
}
});
it('should reject attribute key injection attack on update', () => {
spyOnDev(console, 'error');
for (let i = 0; i < 3; i++) {
const container = document.createElement('div');
const beforeUpdate = React.createElement('x-foo-component', {}, null);
ReactDOM.render(beforeUpdate, container);
const afterUpdate = React.createElement(
'x-foo-component',
{'blah" onclick="beevil" noise="hi': 'selected'},
null,
);
ReactDOM.render(afterUpdate, container);
}
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Invalid attribute name: `blah" onclick="beevil" noise="hi`',
);
}
});
it('should update arbitrary attributes for tags containing dashes', () => {
const container = document.createElement('div');
const beforeUpdate = React.createElement('x-foo-component', {}, null);
ReactDOM.render(beforeUpdate, container);
const afterUpdate = <x-foo-component myattr="myval" />;
ReactDOM.render(afterUpdate, container);
expect(container.childNodes[0].getAttribute('myattr')).toBe('myval');
});
it('should clear all the styles when removing `style`', () => {
const styles = {display: 'none', color: 'red'};
const container = document.createElement('div');
ReactDOM.render(<div style={styles} />, container);
const stubStyle = container.firstChild.style;
ReactDOM.render(<div />, container);
expect(stubStyle.display).toEqual('');
expect(stubStyle.color).toEqual('');
});
it('should update styles when `style` changes from null to object', () => {
const container = document.createElement('div');
const styles = {color: 'red'};
ReactDOM.render(<div style={styles} />, container);
ReactDOM.render(<div />, container);
ReactDOM.render(<div style={styles} />, container);
const stubStyle = container.firstChild.style;
expect(stubStyle.color).toEqual('red');
});
it('should not reset innerHTML for when children is null', () => {
const container = document.createElement('div');
ReactDOM.render(<div />, container);
container.firstChild.innerHTML = 'bonjour';
expect(container.firstChild.innerHTML).toEqual('bonjour');
ReactDOM.render(<div />, container);
expect(container.firstChild.innerHTML).toEqual('bonjour');
});
it('should reset innerHTML when switching from a direct text child to an empty child', () => {
const transitionToValues = [null, undefined, false];
transitionToValues.forEach(transitionToValue => {
const container = document.createElement('div');
ReactDOM.render(<div>bonjour</div>, container);
expect(container.firstChild.innerHTML).toEqual('bonjour');
ReactDOM.render(<div>{transitionToValue}</div>, container);
expect(container.firstChild.innerHTML).toEqual('');
});
});
it('should empty element when removing innerHTML', () => {
const container = document.createElement('div');
ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: ':)'}} />,
container,
);
expect(container.firstChild.innerHTML).toEqual(':)');
ReactDOM.render(<div />, container);
expect(container.firstChild.innerHTML).toEqual('');
});
it('should transition from string content to innerHTML', () => {
const container = document.createElement('div');
ReactDOM.render(<div>hello</div>, container);
expect(container.firstChild.innerHTML).toEqual('hello');
ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: 'goodbye'}} />,
container,
);
expect(container.firstChild.innerHTML).toEqual('goodbye');
});
it('should transition from innerHTML to string content', () => {
const container = document.createElement('div');
ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: 'bonjour'}} />,
container,
);
expect(container.firstChild.innerHTML).toEqual('bonjour');
ReactDOM.render(<div>adieu</div>, container);
expect(container.firstChild.innerHTML).toEqual('adieu');
});
it('should transition from innerHTML to children in nested el', () => {
const container = document.createElement('div');
ReactDOM.render(
<div>
<div dangerouslySetInnerHTML={{__html: 'bonjour'}} />
</div>,
container,
);
expect(container.textContent).toEqual('bonjour');
ReactDOM.render(
<div>
<div>
<span>adieu</span>
</div>
</div>,
container,
);
expect(container.textContent).toEqual('adieu');
});
it('should transition from children to innerHTML in nested el', () => {
const container = document.createElement('div');
ReactDOM.render(
<div>
<div>
<span>adieu</span>
</div>
</div>,
container,
);
expect(container.textContent).toEqual('adieu');
ReactDOM.render(
<div>
<div dangerouslySetInnerHTML={{__html: 'bonjour'}} />
</div>,
container,
);
expect(container.textContent).toEqual('bonjour');
});
it('should not incur unnecessary DOM mutations for attributes', () => {
const container = document.createElement('div');
ReactDOM.render(<div id="" />, container);
const node = container.firstChild;
const nodeSetAttribute = node.setAttribute;
node.setAttribute = jest.fn();
node.setAttribute.mockImplementation(nodeSetAttribute);
const nodeRemoveAttribute = node.removeAttribute;
node.removeAttribute = jest.fn();
node.removeAttribute.mockImplementation(nodeRemoveAttribute);
ReactDOM.render(<div id="" />, container);
expect(node.setAttribute.mock.calls.length).toBe(0);
expect(node.removeAttribute.mock.calls.length).toBe(0);
ReactDOM.render(<div id="foo" />, container);
expect(node.setAttribute.mock.calls.length).toBe(1);
expect(node.removeAttribute.mock.calls.length).toBe(0);
ReactDOM.render(<div id="foo" />, container);
expect(node.setAttribute.mock.calls.length).toBe(1);
expect(node.removeAttribute.mock.calls.length).toBe(0);
ReactDOM.render(<div />, container);
expect(node.setAttribute.mock.calls.length).toBe(1);
expect(node.removeAttribute.mock.calls.length).toBe(1);
ReactDOM.render(<div id="" />, container);
expect(node.setAttribute.mock.calls.length).toBe(2);
expect(node.removeAttribute.mock.calls.length).toBe(1);
ReactDOM.render(<div />, container);
expect(node.setAttribute.mock.calls.length).toBe(2);
expect(node.removeAttribute.mock.calls.length).toBe(2);
});
it('should not incur unnecessary DOM mutations for string properties', () => {
const container = document.createElement('div');
ReactDOM.render(<div value="" />, container);
const node = container.firstChild;
const nodeValueSetter = jest.genMockFn();
const oldSetAttribute = node.setAttribute.bind(node);
node.setAttribute = function(key, value) {
oldSetAttribute(key, value);
nodeValueSetter(key, value);
};
ReactDOM.render(<div value="foo" />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
ReactDOM.render(<div value="foo" />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
ReactDOM.render(<div />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
ReactDOM.render(<div value={null} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
ReactDOM.render(<div value="" />, container);
expect(nodeValueSetter.mock.calls.length).toBe(2);
ReactDOM.render(<div />, container);
expect(nodeValueSetter.mock.calls.length).toBe(2);
});
it('should not incur unnecessary DOM mutations for boolean properties', () => {
const container = document.createElement('div');
ReactDOM.render(<div checked={true} />, container);
const node = container.firstChild;
let nodeValue = true;
const nodeValueSetter = jest.fn();
Object.defineProperty(node, 'checked', {
get: function() {
return nodeValue;
},
set: nodeValueSetter.mockImplementation(function(newValue) {
nodeValue = newValue;
}),
});
ReactDOM.render(<div checked={true} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(0);
ReactDOM.render(<div />, container);
expect(nodeValueSetter.mock.calls.length).toBe(1);
ReactDOM.render(<div checked={false} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(2);
ReactDOM.render(<div checked={true} />, container);
expect(nodeValueSetter.mock.calls.length).toBe(3);
});
it('should ignore attribute whitelist for elements with the "is" attribute', () => {
const container = document.createElement('div');
ReactDOM.render(<button is="test" cowabunga="chevynova" />, container);
expect(container.firstChild.hasAttribute('cowabunga')).toBe(true);
});
it('should warn about non-string "is" attribute', () => {
spyOnDev(console, 'error');
const container = document.createElement('div');
ReactDOM.render(<button is={function() {}} />, container);
if (__DEV__) {
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'Received a `function` for a string attribute `is`. If this is expected, cast ' +
'the value to a string.',
);
}
});
it('should not update when switching between null/undefined', () => {
const container = document.createElement('div');
const node = ReactDOM.render(<div />, container);
const setter = jest.fn();
node.setAttribute = setter;
ReactDOM.render(<div dir={null} />, container);
ReactDOM.render(<div dir={undefined} />, container);
ReactDOM.render(<div />, container);
expect(setter.mock.calls.length).toBe(0);
ReactDOM.render(<div dir="ltr" />, container);
expect(setter.mock.calls.length).toBe(1);
});
it('handles multiple child updates without interference', () => {
// This test might look like it's just testing ReactMultiChild but the
// last bug in this was actually in DOMChildrenOperations so this test
// needs to be in some DOM-specific test file.
const container = document.createElement('div');
// ABCD
ReactDOM.render(
<div>
<div key="one">
<div key="A">A</div>
<div key="B">B</div>
</div>
<div key="two">
<div key="C">C</div>
<div key="D">D</div>
</div>
</div>,
container,
);
// BADC
ReactDOM.render(
<div>
<div key="one">
<div key="B">B</div>
<div key="A">A</div>
</div>
<div key="two">
<div key="D">D</div>
<div key="C">C</div>
</div>
</div>,
container,
);
expect(container.textContent).toBe('BADC');
});
});
describe('createOpenTagMarkup', () => {
function quoteRegexp(str) {
return (str + '').replace(/([.?*+\^$\[\]\\(){}|-])/g, '\\$1');
}
function toHaveAttribute(actual, expected) {
const [attr, value] = expected;
let re = '(?:^|\\s)' + attr + '=[\\\'"]';
if (typeof value !== 'undefined') {
re += quoteRegexp(value) + '[\\\'"]';
}
return new RegExp(re).test(actual);
}
function genMarkup(props) {
return ReactDOMServer.renderToString(<div {...props} />);
}
it('should generate the correct markup with className', () => {
expect(toHaveAttribute(genMarkup({className: 'a'}), ['class', 'a']));
expect(toHaveAttribute(genMarkup({className: 'a b'}), ['class', 'a b']));
expect(toHaveAttribute(genMarkup({className: ''}), ['class', '']));
});
it('should escape style names and values', () => {
expect(
toHaveAttribute(
genMarkup({
style: {'b&ckground': '<3'},
}),
['style', 'b&ckground:<3;'],
),
);
});
});
describe('createContentMarkup', () => {
function quoteRegexp(str) {
return (str + '').replace(/([.?*+\^$\[\]\\(){}|-])/g, '\\$1');
}
function genMarkup(props) {
return ReactDOMServer.renderToString(<div {...props} />);
}
function toHaveInnerhtml(actual, expected) {
const re = '^' + quoteRegexp(expected) + '$';
return new RegExp(re).test(actual);
}
it('should handle dangerouslySetInnerHTML', () => {
const innerHTML = {__html: 'testContent'};
expect(
toHaveInnerhtml(
genMarkup({dangerouslySetInnerHTML: innerHTML}),
'testContent',
),
);
});
});
describe('mountComponent', () => {
let mountComponent;
beforeEach(() => {
mountComponent = function(props) {
const container = document.createElement('div');
ReactDOM.render(<div {...props} />, container);
};
});
it('should work error event on <source> element', () => {
spyOnDevAndProd(console, 'log');
const container = document.createElement('div');