Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1347b4a

Browse files
iansusophiebits
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. (facebook#10025)
nhunzaker: I've added an additional line to the ref test that sets the NODE_ENV invironment variable. This allows the test to pass.
1 parent 8af9dbf commit 1347b4a

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ var ReactCompositeComponent = {
372372
publicContext,
373373
updateQueue,
374374
) {
375-
if (__DEV__) {
375+
if (__DEV__ && !doConstruct) {
376376
ReactCurrentOwner.current = this;
377377
try {
378378
return this._constructComponentWithoutOwner(

src/renderers/shared/stack/reconciler/__tests__/refs-test.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
var React = require('React');
1515
var ReactTestUtils = require('ReactTestUtils');
16+
var ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
1617

1718
var reactComponentExpect = require('reactComponentExpect');
1819

@@ -283,3 +284,78 @@ describe('ref swapping', () => {
283284
__DEV__ = originalDev;
284285
});
285286
});
287+
288+
describe('creating element with ref in constructor', () => {
289+
class RefTest extends React.Component {
290+
constructor(props) {
291+
super(props);
292+
this.p = <p ref="p">Hello!</p>;
293+
}
294+
295+
render() {
296+
return <div>{this.p}</div>;
297+
}
298+
}
299+
300+
var devErrorMessage =
301+
'addComponentAsRefTo(...): Only a ReactOwner can have refs. You might ' +
302+
"be adding a ref to a component that was not created inside a component's " +
303+
'`render` method, or you have multiple copies of React loaded ' +
304+
'(details: https://fb.me/react-refs-must-have-owner).';
305+
306+
var prodErrorMessage =
307+
'Minified React error #119; visit ' +
308+
'http://facebook.github.io/react/docs/error-decoder.html?invariant=119 for the full message ' +
309+
'or use the non-minified dev environment for full errors and additional helpful warnings.';
310+
311+
var fiberDevErrorMessage =
312+
'Element ref was specified as a string (p) but no owner was ' +
313+
'set. You may have multiple copies of React loaded. ' +
314+
'(details: https://fb.me/react-refs-must-have-owner).';
315+
316+
var fiberProdErrorMessage =
317+
'Minified React error #149; visit ' +
318+
'http://facebook.github.io/react/docs/error-decoder.html?invariant=149&args[]=p ' +
319+
'for the full message or use the non-minified dev environment for full errors and additional ' +
320+
'helpful warnings.';
321+
322+
it('throws an error when __DEV__ = true', () => {
323+
ReactTestUtils = require('ReactTestUtils');
324+
325+
var originalDev = __DEV__;
326+
__DEV__ = true;
327+
328+
try {
329+
expect(function() {
330+
ReactTestUtils.renderIntoDocument(<RefTest />);
331+
}).toThrowError(
332+
ReactDOMFeatureFlags.useFiber ? fiberDevErrorMessage : devErrorMessage,
333+
);
334+
} finally {
335+
__DEV__ = originalDev;
336+
}
337+
});
338+
339+
it('throws an error when __DEV__ = false', () => {
340+
ReactTestUtils = require('ReactTestUtils');
341+
342+
var originalDev = __DEV__;
343+
var originalEnv = process.env.NODE_ENV;
344+
345+
__DEV__ = false;
346+
process.env.NODE_ENV = 'production';
347+
348+
try {
349+
expect(function() {
350+
ReactTestUtils.renderIntoDocument(<RefTest />);
351+
}).toThrowError(
352+
ReactDOMFeatureFlags.useFiber
353+
? fiberProdErrorMessage
354+
: prodErrorMessage,
355+
);
356+
} finally {
357+
__DEV__ = originalDev;
358+
process.env.NODE_ENV = originalEnv;
359+
}
360+
});
361+
});

0 commit comments

Comments
 (0)