@@ -476,3 +476,74 @@ describe('root level refs', () => {
476
476
}
477
477
} ) ;
478
478
} ) ;
479
+
480
+ describe ( 'creating element with ref in constructor' , ( ) => {
481
+ class RefTest extends React . Component {
482
+ constructor ( props ) {
483
+ super ( props ) ;
484
+ this . p = < p ref = "p" > Hello!</ p > ;
485
+ }
486
+
487
+ render ( ) {
488
+ return < div > { this . p } </ div > ;
489
+ }
490
+ }
491
+
492
+ var devErrorMessage =
493
+ 'addComponentAsRefTo(...): Only a ReactOwner can have refs. You might ' +
494
+ "be adding a ref to a component that was not created inside a component's " +
495
+ '`render` method, or you have multiple copies of React loaded ' +
496
+ '(details: https://fb.me/react-refs-must-have-owner).' ;
497
+
498
+ var prodErrorMessage =
499
+ 'Minified React error #119; visit ' +
500
+ 'http://facebook.github.io/react/docs/error-decoder.html?invariant=119 for the full message ' +
501
+ 'or use the non-minified dev environment for full errors and additional helpful warnings.' ;
502
+
503
+ var fiberDevErrorMessage =
504
+ 'Element ref was specified as a string (p) but no owner was ' +
505
+ 'set. You may have multiple copies of React loaded. ' +
506
+ '(details: https://fb.me/react-refs-must-have-owner).' ;
507
+
508
+ var fiberProdErrorMessage =
509
+ 'Minified React error #149; visit ' +
510
+ 'http://facebook.github.io/react/docs/error-decoder.html?invariant=149&args[]=p ' +
511
+ 'for the full message or use the non-minified dev environment for full errors and additional ' +
512
+ 'helpful warnings.' ;
513
+
514
+ it ( 'throws an error when __DEV__ = true' , ( ) => {
515
+ ReactTestUtils = require ( 'react-dom/test-utils' ) ;
516
+
517
+ var originalDev = __DEV__ ;
518
+ __DEV__ = true ;
519
+
520
+ try {
521
+ expect ( function ( ) {
522
+ ReactTestUtils . renderIntoDocument ( < RefTest /> ) ;
523
+ } ) . toThrowError (
524
+ ReactDOMFeatureFlags . useFiber ? fiberDevErrorMessage : devErrorMessage ,
525
+ ) ;
526
+ } finally {
527
+ __DEV__ = originalDev ;
528
+ }
529
+ } ) ;
530
+
531
+ it ( 'throws an error when __DEV__ = false' , ( ) => {
532
+ ReactTestUtils = require ( 'react-dom/test-utils' ) ;
533
+
534
+ var originalDev = __DEV__ ;
535
+ __DEV__ = false ;
536
+
537
+ try {
538
+ expect ( function ( ) {
539
+ ReactTestUtils . renderIntoDocument ( < RefTest /> ) ;
540
+ } ) . toThrowError (
541
+ ReactDOMFeatureFlags . useFiber
542
+ ? fiberProdErrorMessage
543
+ : prodErrorMessage ,
544
+ ) ;
545
+ } finally {
546
+ __DEV__ = originalDev ;
547
+ }
548
+ } ) ;
549
+ } ) ;
0 commit comments