14
14
var React ;
15
15
var ReactDOM ;
16
16
var ReactDOMServer ;
17
- var ReactDOMFeatureFlags ;
18
17
var ReactTestUtils ;
19
18
19
+ var ReactDOMFeatureFlags = require ( 'ReactDOMFeatureFlags' ) ;
20
+
20
21
describe ( 'ReactComponent' , ( ) => {
21
22
function normalizeCodeLocInfo ( str ) {
22
23
return str && str . replace ( / \( a t .+ ?: \d + \) / g, '(at **)' ) ;
@@ -26,7 +27,6 @@ describe('ReactComponent', () => {
26
27
React = require ( 'react' ) ;
27
28
ReactDOM = require ( 'react-dom' ) ;
28
29
ReactDOMServer = require ( 'react-dom/server' ) ;
29
- ReactDOMFeatureFlags = require ( 'ReactDOMFeatureFlags' ) ;
30
30
ReactTestUtils = require ( 'react-dom/test-utils' ) ;
31
31
} ) ;
32
32
@@ -493,4 +493,78 @@ describe('ReactComponent', () => {
493
493
' in Foo (at **)' ,
494
494
) ;
495
495
} ) ;
496
+
497
+ if ( ReactDOMFeatureFlags . useFiber ) {
498
+ describe ( 'with new features' , ( ) => {
499
+ beforeEach ( ( ) => {
500
+ require ( 'ReactFeatureFlags' ) . disableNewFiberFeatures = false ;
501
+ } ) ;
502
+
503
+ it ( 'warns on function as a return value from a function' , ( ) => {
504
+ function Foo ( ) {
505
+ return Foo ;
506
+ }
507
+ spyOn ( console , 'error' ) ;
508
+ var container = document . createElement ( 'div' ) ;
509
+ ReactDOM . render ( < Foo /> , container ) ;
510
+ expectDev ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
511
+ expectDev ( normalizeCodeLocInfo ( console . error . calls . argsFor ( 0 ) [ 0 ] ) ) . toBe (
512
+ 'Warning: Functions are not valid as a React child. This may happen if ' +
513
+ 'you return a Component instead of <Component /> from render. ' +
514
+ 'Or maybe you meant to call this function rather than return it.\n' +
515
+ ' in Foo (at **)' ,
516
+ ) ;
517
+ } ) ;
518
+
519
+ it ( 'warns on function as a return value from a class' , ( ) => {
520
+ class Foo extends React . Component {
521
+ render ( ) {
522
+ return Foo ;
523
+ }
524
+ }
525
+ spyOn ( console , 'error' ) ;
526
+ var container = document . createElement ( 'div' ) ;
527
+ ReactDOM . render ( < Foo /> , container ) ;
528
+ expectDev ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
529
+ expectDev ( normalizeCodeLocInfo ( console . error . calls . argsFor ( 0 ) [ 0 ] ) ) . toBe (
530
+ 'Warning: Functions are not valid as a React child. This may happen if ' +
531
+ 'you return a Component instead of <Component /> from render. ' +
532
+ 'Or maybe you meant to call this function rather than return it.\n' +
533
+ ' in Foo (at **)' ,
534
+ ) ;
535
+ } ) ;
536
+
537
+ it ( 'warns on function as a child to host component' , ( ) => {
538
+ function Foo ( ) {
539
+ return < div > < span > { Foo } </ span > </ div > ;
540
+ }
541
+ spyOn ( console , 'error' ) ;
542
+ var container = document . createElement ( 'div' ) ;
543
+ ReactDOM . render ( < Foo /> , container ) ;
544
+ expectDev ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
545
+ expectDev ( normalizeCodeLocInfo ( console . error . calls . argsFor ( 0 ) [ 0 ] ) ) . toBe (
546
+ 'Warning: Functions are not valid as a React child. This may happen if ' +
547
+ 'you return a Component instead of <Component /> from render. ' +
548
+ 'Or maybe you meant to call this function rather than return it.\n' +
549
+ ' in span (at **)\n' +
550
+ ' in div (at **)\n' +
551
+ ' in Foo (at **)' ,
552
+ ) ;
553
+ } ) ;
554
+
555
+ it ( 'does not warn for function-as-a-child that gets resolved' , ( ) => {
556
+ function Bar ( props ) {
557
+ return props . children ( ) ;
558
+ }
559
+ function Foo ( ) {
560
+ return < Bar > { ( ) => 'Hello' } </ Bar > ;
561
+ }
562
+ spyOn ( console , 'error' ) ;
563
+ var container = document . createElement ( 'div' ) ;
564
+ ReactDOM . render ( < Foo /> , container ) ;
565
+ expect ( container . innerHTML ) . toBe ( 'Hello' ) ;
566
+ expectDev ( console . error . calls . count ( ) ) . toBe ( 0 ) ;
567
+ } ) ;
568
+ } ) ;
569
+ }
496
570
} ) ;
0 commit comments