/
ReactServerRendering-test.js
1122 lines (978 loc) · 31.8 KB
/
ReactServerRendering-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) 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
* @jest-environment node
*/
'use strict';
let React;
let ReactDOMServer;
let PropTypes;
let ReactCurrentDispatcher;
function normalizeCodeLocInfo(str) {
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
}
describe('ReactDOMServer', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
PropTypes = require('prop-types');
ReactDOMServer = require('react-dom/server');
ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher;
});
describe('renderToString', () => {
it('should generate simple markup', () => {
const response = ReactDOMServer.renderToString(<span>hello world</span>);
expect(response).toMatch(
new RegExp('<span data-reactroot=""' + '>hello world</span>'),
);
});
it('should generate simple markup for self-closing tags', () => {
const response = ReactDOMServer.renderToString(<img />);
expect(response).toMatch(new RegExp('<img data-reactroot=""' + '/>'));
});
it('should generate comment markup for component returns null', () => {
class NullComponent extends React.Component {
render() {
return null;
}
}
const response = ReactDOMServer.renderToString(<NullComponent />);
expect(response).toBe('');
});
// TODO: Test that listeners are not registered onto any document/container.
it('should render composite components', () => {
class Parent extends React.Component {
render() {
return (
<div>
<Child name="child" />
</div>
);
}
}
class Child extends React.Component {
render() {
return <span>My name is {this.props.name}</span>;
}
}
const response = ReactDOMServer.renderToString(<Parent />);
expect(response).toMatch(
new RegExp(
'<div ' +
'data-reactroot' +
'=""' +
'>' +
'<span' +
'>' +
'My name is <!-- -->child' +
'</span>' +
'</div>',
),
);
});
it('should only execute certain lifecycle methods', () => {
function runTest() {
const lifecycle = [];
class TestComponent extends React.Component {
constructor(props) {
super(props);
lifecycle.push('getInitialState');
this.state = {name: 'TestComponent'};
}
UNSAFE_componentWillMount() {
lifecycle.push('componentWillMount');
}
componentDidMount() {
lifecycle.push('componentDidMount');
}
render() {
lifecycle.push('render');
return <span>Component name: {this.state.name}</span>;
}
UNSAFE_componentWillUpdate() {
lifecycle.push('componentWillUpdate');
}
componentDidUpdate() {
lifecycle.push('componentDidUpdate');
}
shouldComponentUpdate() {
lifecycle.push('shouldComponentUpdate');
}
UNSAFE_componentWillReceiveProps() {
lifecycle.push('componentWillReceiveProps');
}
componentWillUnmount() {
lifecycle.push('componentWillUnmount');
}
}
const response = ReactDOMServer.renderToString(<TestComponent />);
expect(response).toMatch(
new RegExp(
'<span ' +
'data-reactroot' +
'=""' +
'>' +
'Component name: <!-- -->TestComponent' +
'</span>',
),
);
expect(lifecycle).toEqual([
'getInitialState',
'componentWillMount',
'render',
]);
}
runTest();
});
it('should throw with silly args', () => {
expect(
ReactDOMServer.renderToString.bind(ReactDOMServer, {x: 123}),
).toThrowError(
'Objects are not valid as a React child (found: object with keys {x})',
);
});
it('should throw prop mapping error for an <iframe /> with invalid props', () => {
let caughtErr;
try {
ReactDOMServer.renderToString(<iframe style="border:none;" />);
} catch (err) {
caughtErr = err;
}
expect(caughtErr).not.toBe(undefined);
expect(normalizeCodeLocInfo(caughtErr.message)).toContain(
'The `style` prop expects a mapping from style properties to values, not ' +
"a string. For example, style={{marginRight: spacing + 'em'}} when using JSX." +
(__DEV__ ? '\n in iframe (at **)' : ''),
);
});
it('should not crash on poisoned hasOwnProperty', () => {
let html;
expect(
() =>
(html = ReactDOMServer.renderToString(
<div hasOwnProperty="poison">
<span unknown="test" />
</div>,
)),
).toWarnDev(['React does not recognize the `hasOwnProperty` prop']);
expect(html).toContain('<span unknown="test">');
});
});
describe('renderToStaticMarkup', () => {
it('should not put checksum and React ID on components', () => {
class NestedComponent extends React.Component {
render() {
return <div>inner text</div>;
}
}
class TestComponent extends React.Component {
render() {
return (
<span>
<NestedComponent />
</span>
);
}
}
const response = ReactDOMServer.renderToStaticMarkup(<TestComponent />);
expect(response).toBe('<span><div>inner text</div></span>');
});
it('should not put checksum and React ID on text components', () => {
class TestComponent extends React.Component {
render() {
return (
<span>
{'hello'} {'world'}
</span>
);
}
}
const response = ReactDOMServer.renderToStaticMarkup(<TestComponent />);
expect(response).toBe('<span>hello world</span>');
});
it('should not use comments for empty nodes', () => {
class TestComponent extends React.Component {
render() {
return null;
}
}
const response = ReactDOMServer.renderToStaticMarkup(<TestComponent />);
expect(response).toBe('');
});
it('should only execute certain lifecycle methods', () => {
function runTest() {
const lifecycle = [];
class TestComponent extends React.Component {
constructor(props) {
super(props);
lifecycle.push('getInitialState');
this.state = {name: 'TestComponent'};
}
UNSAFE_componentWillMount() {
lifecycle.push('componentWillMount');
}
componentDidMount() {
lifecycle.push('componentDidMount');
}
render() {
lifecycle.push('render');
return <span>Component name: {this.state.name}</span>;
}
UNSAFE_componentWillUpdate() {
lifecycle.push('componentWillUpdate');
}
componentDidUpdate() {
lifecycle.push('componentDidUpdate');
}
shouldComponentUpdate() {
lifecycle.push('shouldComponentUpdate');
}
UNSAFE_componentWillReceiveProps() {
lifecycle.push('componentWillReceiveProps');
}
componentWillUnmount() {
lifecycle.push('componentWillUnmount');
}
}
const response = ReactDOMServer.renderToStaticMarkup(<TestComponent />);
expect(response).toBe('<span>Component name: TestComponent</span>');
expect(lifecycle).toEqual([
'getInitialState',
'componentWillMount',
'render',
]);
}
runTest();
});
it('should throw with silly args', () => {
expect(
ReactDOMServer.renderToStaticMarkup.bind(ReactDOMServer, {x: 123}),
).toThrowError(
'Objects are not valid as a React child (found: object with keys {x})',
);
});
it('allows setState in componentWillMount without using DOM', () => {
class Component extends React.Component {
UNSAFE_componentWillMount() {
this.setState({text: 'hello, world'});
}
render() {
return <div>{this.state.text}</div>;
}
}
const markup = ReactDOMServer.renderToStaticMarkup(<Component />);
expect(markup).toContain('hello, world');
});
it('allows setState in componentWillMount with custom constructor', () => {
class Component extends React.Component {
constructor() {
super();
this.state = {text: 'default state'};
}
UNSAFE_componentWillMount() {
this.setState({text: 'hello, world'});
}
render() {
return <div>{this.state.text}</div>;
}
}
const markup = ReactDOMServer.renderToStaticMarkup(<Component />);
expect(markup).toContain('hello, world');
});
it('renders with props when using custom constructor', () => {
class Component extends React.Component {
constructor() {
super();
}
render() {
return <div>{this.props.text}</div>;
}
}
const markup = ReactDOMServer.renderToStaticMarkup(
<Component text="hello, world" />,
);
expect(markup).toContain('hello, world');
});
it('renders with context when using custom constructor', () => {
class Component extends React.Component {
constructor() {
super();
}
render() {
return <div>{this.context.text}</div>;
}
}
Component.contextTypes = {
text: PropTypes.string.isRequired,
};
class ContextProvider extends React.Component {
getChildContext() {
return {
text: 'hello, world',
};
}
render() {
return this.props.children;
}
}
ContextProvider.childContextTypes = {
text: PropTypes.string,
};
const markup = ReactDOMServer.renderToStaticMarkup(
<ContextProvider>
<Component />
</ContextProvider>,
);
expect(markup).toContain('hello, world');
});
it('renders with new context API', () => {
const Context = React.createContext(0);
function Consumer(props) {
return (
<Context.Consumer>{value => 'Result: ' + value}</Context.Consumer>
);
}
const Indirection = React.Fragment;
function App(props) {
return (
<Context.Provider value={props.value}>
<Context.Provider value={2}>
<Consumer />
</Context.Provider>
<Indirection>
<Indirection>
<Consumer />
<Context.Provider value={3}>
<Consumer />
</Context.Provider>
</Indirection>
</Indirection>
<Consumer />
</Context.Provider>
);
}
const markup = ReactDOMServer.renderToStaticMarkup(<App value={1} />);
// Extract the numbers rendered by the consumers
const results = markup.match(/\d+/g).map(Number);
expect(results).toEqual([2, 1, 3, 1]);
});
it('renders with dispatcher.readContext mechanism', () => {
const Context = React.createContext(0);
function readContext(context) {
return ReactCurrentDispatcher.current.readContext(context);
}
function Consumer(props) {
return 'Result: ' + readContext(Context);
}
const Indirection = React.Fragment;
function App(props) {
return (
<Context.Provider value={props.value}>
<Context.Provider value={2}>
<Consumer />
</Context.Provider>
<Indirection>
<Indirection>
<Consumer />
<Context.Provider value={3}>
<Consumer />
</Context.Provider>
</Indirection>
</Indirection>
<Consumer />
</Context.Provider>
);
}
const markup = ReactDOMServer.renderToStaticMarkup(<App value={1} />);
// Extract the numbers rendered by the consumers
const results = markup.match(/\d+/g).map(Number);
expect(results).toEqual([2, 1, 3, 1]);
});
it('renders context API, reentrancy', () => {
const Context = React.createContext(0);
function Consumer(props) {
return (
<Context.Consumer>{value => 'Result: ' + value}</Context.Consumer>
);
}
let reentrantMarkup;
function Reentrant() {
reentrantMarkup = ReactDOMServer.renderToStaticMarkup(
<App value={1} reentrant={false} />,
);
return null;
}
const Indirection = React.Fragment;
function App(props) {
return (
<Context.Provider value={props.value}>
{props.reentrant && <Reentrant />}
<Context.Provider value={2}>
<Consumer />
</Context.Provider>
<Indirection>
<Indirection>
<Consumer />
<Context.Provider value={3}>
<Consumer />
</Context.Provider>
</Indirection>
</Indirection>
<Consumer />
</Context.Provider>
);
}
const markup = ReactDOMServer.renderToStaticMarkup(
<App value={1} reentrant={true} />,
);
// Extract the numbers rendered by the consumers
const results = markup.match(/\d+/g).map(Number);
const reentrantResults = reentrantMarkup.match(/\d+/g).map(Number);
expect(results).toEqual([2, 1, 3, 1]);
expect(reentrantResults).toEqual([2, 1, 3, 1]);
});
it('renders components with different batching strategies', () => {
class StaticComponent extends React.Component {
render() {
const staticContent = ReactDOMServer.renderToStaticMarkup(
<div>
<img src="foo-bar.jpg" />
</div>,
);
return <div dangerouslySetInnerHTML={{__html: staticContent}} />;
}
}
class Component extends React.Component {
UNSAFE_componentWillMount() {
this.setState({text: 'hello, world'});
}
render() {
return <div>{this.state.text}</div>;
}
}
expect(
ReactDOMServer.renderToString.bind(
ReactDOMServer,
<div>
<StaticComponent />
<Component />
</div>,
),
).not.toThrow();
});
it('renders synchronously resolved lazy component', () => {
const LazyFoo = React.lazy(() => ({
then(resolve) {
resolve({
default: function Foo({id}) {
return <div id={id}>lazy</div>;
},
});
},
}));
expect(ReactDOMServer.renderToStaticMarkup(<LazyFoo id="foo" />)).toEqual(
'<div id="foo">lazy</div>',
);
});
it('throws error from synchronously rejected lazy component', () => {
const LazyFoo = React.lazy(() => ({
then(resolve, reject) {
reject(new Error('Bad lazy'));
},
}));
expect(() => ReactDOMServer.renderToStaticMarkup(<LazyFoo />)).toThrow(
'Bad lazy',
);
});
});
describe('renderToNodeStream', () => {
it('should generate simple markup', () => {
const SuccessfulElement = React.createElement(() => <img />);
const response = ReactDOMServer.renderToNodeStream(SuccessfulElement);
expect(response.read().toString()).toMatch(
new RegExp('<img data-reactroot=""' + '/>'),
);
});
it('should handle errors correctly', () => {
const FailingElement = React.createElement(() => {
throw new Error('An Error');
});
const response = ReactDOMServer.renderToNodeStream(FailingElement);
return new Promise(resolve => {
response.once('error', () => {
resolve();
});
expect(response.read()).toBeNull();
});
});
});
describe('renderToStaticNodeStream', () => {
it('should generate simple markup', () => {
const SuccessfulElement = React.createElement(() => <img />);
const response = ReactDOMServer.renderToStaticNodeStream(
SuccessfulElement,
);
expect(response.read().toString()).toMatch(new RegExp('<img' + '/>'));
});
it('should handle errors correctly', () => {
const FailingElement = React.createElement(() => {
throw new Error('An Error');
});
const response = ReactDOMServer.renderToStaticNodeStream(FailingElement);
return new Promise(resolve => {
response.once('error', () => {
resolve();
});
expect(response.read()).toBeNull();
});
});
});
it('warns with a no-op when an async setState is triggered', () => {
class Foo extends React.Component {
UNSAFE_componentWillMount() {
this.setState({text: 'hello'});
setTimeout(() => {
this.setState({text: 'error'});
});
}
render() {
return <div onClick={() => {}}>{this.state.text}</div>;
}
}
ReactDOMServer.renderToString(<Foo />);
expect(() => jest.runOnlyPendingTimers()).toWarnDev(
'Warning: setState(...): Can only update a mounting component.' +
' This usually means you called setState() outside componentWillMount() on the server.' +
' This is a no-op.\n\nPlease check the code for the Foo component.',
{withoutStack: true},
);
const markup = ReactDOMServer.renderToStaticMarkup(<Foo />);
expect(markup).toBe('<div>hello</div>');
// No additional warnings are expected
jest.runOnlyPendingTimers();
});
it('warns with a no-op when an async forceUpdate is triggered', () => {
class Baz extends React.Component {
UNSAFE_componentWillMount() {
this.forceUpdate();
setTimeout(() => {
this.forceUpdate();
});
}
render() {
return <div onClick={() => {}} />;
}
}
ReactDOMServer.renderToString(<Baz />);
expect(() => jest.runOnlyPendingTimers()).toWarnDev(
'Warning: forceUpdate(...): Can only update a mounting component. ' +
'This usually means you called forceUpdate() outside componentWillMount() on the server. ' +
'This is a no-op.\n\nPlease check the code for the Baz component.',
{withoutStack: true},
);
const markup = ReactDOMServer.renderToStaticMarkup(<Baz />);
expect(markup).toBe('<div></div>');
});
if (!__EXPERIMENTAL__) {
it('throws for unsupported types on the server', () => {
expect(() => {
ReactDOMServer.renderToString(<React.Suspense />);
}).toThrow('ReactDOMServer does not yet support Suspense.');
async function fakeImport(result) {
return {default: result};
}
expect(() => {
const LazyFoo = React.lazy(() =>
fakeImport(
new Promise(resolve =>
resolve(function Foo() {
return <div />;
}),
),
),
);
ReactDOMServer.renderToString(<LazyFoo />);
}).toThrow('ReactDOMServer does not yet support lazy-loaded components.');
});
it('throws when suspending on the server', () => {
function AsyncFoo() {
throw new Promise(() => {});
}
expect(() => {
ReactDOMServer.renderToString(<AsyncFoo />);
}).toThrow('ReactDOMServer does not yet support Suspense.');
});
}
it('does not get confused by throwing null', () => {
function Bad() {
// eslint-disable-next-line no-throw-literal
throw null;
}
let didError;
let error;
try {
ReactDOMServer.renderToString(<Bad />);
} catch (err) {
didError = true;
error = err;
}
expect(didError).toBe(true);
expect(error).toBe(null);
});
it('does not get confused by throwing undefined', () => {
function Bad() {
// eslint-disable-next-line no-throw-literal
throw undefined;
}
let didError;
let error;
try {
ReactDOMServer.renderToString(<Bad />);
} catch (err) {
didError = true;
error = err;
}
expect(didError).toBe(true);
expect(error).toBe(undefined);
});
it('does not get confused by throwing a primitive', () => {
function Bad() {
// eslint-disable-next-line no-throw-literal
throw 'foo';
}
let didError;
let error;
try {
ReactDOMServer.renderToString(<Bad />);
} catch (err) {
didError = true;
error = err;
}
expect(didError).toBe(true);
expect(error).toBe('foo');
});
it('should throw (in dev) when children are mutated during render', () => {
function Wrapper(props) {
props.children[1] = <p key={1} />; // Mutation is illegal
return <div>{props.children}</div>;
}
if (__DEV__) {
expect(() => {
ReactDOMServer.renderToStaticMarkup(
<Wrapper>
<span key={0} />
<span key={1} />
<span key={2} />
</Wrapper>,
);
}).toThrowError(/Cannot assign to read only property.*/);
} else {
expect(
ReactDOMServer.renderToStaticMarkup(
<Wrapper>
<span key={0} />
<span key={1} />
<span key={2} />
</Wrapper>,
),
).toContain('<p>');
}
});
it('warns about lowercase html but not in svg tags', () => {
function CompositeG(props) {
// Make sure namespace passes through composites
return <g>{props.children}</g>;
}
expect(() =>
ReactDOMServer.renderToStaticMarkup(
<div>
<inPUT />
<svg>
<CompositeG>
<linearGradient />
<foreignObject>
{/* back to HTML */}
<iFrame />
</foreignObject>
</CompositeG>
</svg>
</div>,
),
).toWarnDev([
'Warning: <inPUT /> is using incorrect casing. ' +
'Use PascalCase for React components, ' +
'or lowercase for HTML elements.',
// linearGradient doesn't warn
'Warning: <iFrame /> is using incorrect casing. ' +
'Use PascalCase for React components, ' +
'or lowercase for HTML elements.',
]);
});
it('should warn about contentEditable and children', () => {
expect(() =>
ReactDOMServer.renderToString(<div contentEditable={true} children="" />),
).toWarnDev(
'Warning: A component is `contentEditable` and contains `children` ' +
'managed by React. It is now your responsibility to guarantee that ' +
'none of those nodes are unexpectedly modified or duplicated. This ' +
'is probably not intentional.\n in div (at **)',
);
});
it('should warn when server rendering a class with a render method that does not extend React.Component', () => {
class ClassWithRenderNotExtended {
render() {
return <div />;
}
}
expect(() => {
expect(() =>
ReactDOMServer.renderToString(<ClassWithRenderNotExtended />),
).toThrow(TypeError);
}).toWarnDev(
'Warning: The <ClassWithRenderNotExtended /> component appears to have a render method, ' +
"but doesn't extend React.Component. This is likely to cause errors. " +
'Change ClassWithRenderNotExtended to extend React.Component instead.',
{withoutStack: true},
);
// Test deduplication
expect(() => {
ReactDOMServer.renderToString(<ClassWithRenderNotExtended />);
}).toThrow(TypeError);
});
// We're just testing importing, not using it.
// It is important because even isomorphic components may import it.
it('can import react-dom in Node environment', () => {
if (
typeof requestAnimationFrame !== 'undefined' ||
global.hasOwnProperty('requestAnimationFrame') ||
typeof requestIdleCallback !== 'undefined' ||
global.hasOwnProperty('requestIdleCallback') ||
typeof window !== 'undefined' ||
global.hasOwnProperty('window')
) {
// Don't remove this. This test is specifically checking
// what happens when they *don't* exist. It's useless otherwise.
throw new Error('Expected this test to run in a Node environment.');
}
jest.resetModules();
expect(() => {
require('react-dom');
}).not.toThrow();
});
it('includes a useful stack in warnings', () => {
function A() {
return null;
}
function B() {
return (
<font>
<C>
<span ariaTypo="no" />
</C>
</font>
);
}
class C extends React.Component {
render() {
return <b>{this.props.children}</b>;
}
}
function Child() {
return [<A key="1" />, <B key="2" />, <span ariaTypo2="no" />];
}
function App() {
return (
<div>
<section />
<span>
<Child />
</span>
</div>
);
}
expect(() => ReactDOMServer.renderToString(<App />)).toWarnDev([
'Invalid ARIA attribute `ariaTypo`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in span (at **)\n' +
' in b (at **)\n' +
' in C (at **)\n' +
' in font (at **)\n' +
' in B (at **)\n' +
' in Child (at **)\n' +
' in span (at **)\n' +
' in div (at **)\n' +
' in App (at **)',
'Invalid ARIA attribute `ariaTypo2`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in span (at **)\n' +
' in Child (at **)\n' +
' in span (at **)\n' +
' in div (at **)\n' +
' in App (at **)',
]);
});
it('reports stacks with re-entrant renderToString() calls', () => {
function Child2(props) {
return <span ariaTypo3="no">{props.children}</span>;
}
function App2() {
return (
<Child2>
{ReactDOMServer.renderToString(<blink ariaTypo2="no" />)}
</Child2>
);
}
function Child() {
return (
<span ariaTypo4="no">{ReactDOMServer.renderToString(<App2 />)}</span>
);
}
function App() {
return (
<div>
<span ariaTypo="no" />
<Child />
<font ariaTypo5="no" />
</div>
);
}
expect(() => ReactDOMServer.renderToString(<App />)).toWarnDev([
// ReactDOMServer(App > div > span)
'Invalid ARIA attribute `ariaTypo`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in span (at **)\n' +
' in div (at **)\n' +
' in App (at **)',
// ReactDOMServer(App > div > Child) >>> ReactDOMServer(App2) >>> ReactDOMServer(blink)
'Invalid ARIA attribute `ariaTypo2`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in blink (at **)',
// ReactDOMServer(App > div > Child) >>> ReactDOMServer(App2 > Child2 > span)
'Invalid ARIA attribute `ariaTypo3`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in span (at **)\n' +
' in Child2 (at **)\n' +
' in App2 (at **)',
// ReactDOMServer(App > div > Child > span)
'Invalid ARIA attribute `ariaTypo4`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in span (at **)\n' +
' in Child (at **)\n' +
' in div (at **)\n' +
' in App (at **)',
// ReactDOMServer(App > div > font)
'Invalid ARIA attribute `ariaTypo5`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in font (at **)\n' +
' in div (at **)\n' +
' in App (at **)',
]);
});