Skip to content

Commit 7f6b940

Browse files
iansugaearon
authored andcommitted
Added unit tests for creating an element with a ref in a constructor. Only set ReactCurrentOwner.current in dev mode when the component has no constructor. (#10025)
1 parent b4a3e3b commit 7f6b940

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

scripts/fiber/tests-passing.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ src/renderers/__tests__/refs-test.js
626626
* attaches, detaches from fiber component with stack layer
627627
* attaches, detaches from stack component with fiber layer
628628
* attaches and detaches root refs
629+
* throws an error when __DEV__ = true
630+
* throws an error when __DEV__ = false
629631

630632
src/renderers/art/__tests__/ReactART-test.js
631633
* should have the correct lifecycle state

src/renderers/__tests__/refs-test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,74 @@ describe('root level refs', () => {
476476
}
477477
});
478478
});
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+
});

src/renderers/shared/stack/reconciler/ReactCompositeComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ var ReactCompositeComponent = {
402402
publicContext,
403403
updateQueue,
404404
) {
405-
if (__DEV__) {
405+
if (__DEV__ && !doConstruct) {
406406
ReactCurrentOwner.current = this;
407407
ReactDebugCurrentFrame.getCurrentStack =
408408
ReactDebugCurrentStack.getStackAddendum;

0 commit comments

Comments
 (0)