Skip to content

Commit

Permalink
Added unit tests for creating an element with a ref in a constructor.…
Browse files Browse the repository at this point in the history
… Only set ReactCurrentOwner.current in dev mode when the component has no constructor.
  • Loading branch information
iansu committed Aug 14, 2017
1 parent b4a3e3b commit 7c175dc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
2 changes: 2 additions & 0 deletions scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ src/renderers/__tests__/refs-test.js
* attaches, detaches from fiber component with stack layer
* attaches, detaches from stack component with fiber layer
* attaches and detaches root refs
* throws an error when __DEV__ = true
* throws an error when __DEV__ = false

src/renderers/art/__tests__/ReactART-test.js
* should have the correct lifecycle state
Expand Down
71 changes: 71 additions & 0 deletions src/renderers/__tests__/refs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,74 @@ describe('root level refs', () => {
}
});
});

describe('creating element with ref in constructor', () => {
class RefTest extends React.Component {
constructor(props) {
super(props);
this.p = <p ref="p">Hello!</p>;
}

render() {
return <div>{this.p}</div>;
}
}

var devErrorMessage =
'addComponentAsRefTo(...): Only a ReactOwner can have refs. You might ' +
"be adding a ref to a component that was not created inside a component's " +
'`render` method, or you have multiple copies of React loaded ' +
'(details: https://fb.me/react-refs-must-have-owner).';

var prodErrorMessage =
'Minified React error #119; visit ' +
'http://facebook.github.io/react/docs/error-decoder.html?invariant=119 for the full message ' +
'or use the non-minified dev environment for full errors and additional helpful warnings.';

var fiberDevErrorMessage =
'Element ref was specified as a string (p) but no owner was ' +
'set. You may have multiple copies of React loaded. ' +
'(details: https://fb.me/react-refs-must-have-owner).';

var fiberProdErrorMessage =
'Minified React error #149; visit ' +
'http://facebook.github.io/react/docs/error-decoder.html?invariant=149&args[]=p ' +
'for the full message or use the non-minified dev environment for full errors and additional ' +
'helpful warnings.';

it('throws an error when __DEV__ = true', () => {
ReactTestUtils = require('react-dom/test-utils');

var originalDev = __DEV__;
__DEV__ = true;

try {
expect(function() {
ReactTestUtils.renderIntoDocument(<RefTest />);
}).toThrowError(
ReactDOMFeatureFlags.useFiber ? fiberDevErrorMessage : devErrorMessage,
);
} finally {
__DEV__ = originalDev;
}
});

it('throws an error when __DEV__ = false', () => {
ReactTestUtils = require('react-dom/test-utils');

var originalDev = __DEV__;
__DEV__ = false;

try {
expect(function() {
ReactTestUtils.renderIntoDocument(<RefTest />);
}).toThrowError(
ReactDOMFeatureFlags.useFiber
? fiberProdErrorMessage
: prodErrorMessage,
);
} finally {
__DEV__ = originalDev;
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ var ReactCompositeComponent = {
publicContext,
updateQueue,
) {
if (__DEV__) {
if (__DEV__ && !doConstruct) {
ReactCurrentOwner.current = this;
ReactDebugCurrentFrame.getCurrentStack =
ReactDebugCurrentStack.getStackAddendum;
Expand Down

0 comments on commit 7c175dc

Please sign in to comment.