Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating element with ref in constructor does not throw error in dev mode (#9635) #10025

Merged
merged 1 commit into from Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/fiber/tests-passing.txt
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
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;
}
});
});
Expand Up @@ -402,7 +402,7 @@ var ReactCompositeComponent = {
publicContext,
updateQueue,
) {
if (__DEV__) {
if (__DEV__ && !doConstruct) {
ReactCurrentOwner.current = this;
ReactDebugCurrentFrame.getCurrentStack =
ReactDebugCurrentStack.getStackAddendum;
Expand Down