From 7c175dc6cea3356e16f734c094eb627713ad2686 Mon Sep 17 00:00:00 2001 From: Ian Sutherland Date: Mon, 14 Aug 2017 17:19:11 -0600 Subject: [PATCH] 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. --- scripts/fiber/tests-passing.txt | 2 + src/renderers/__tests__/refs-test.js | 71 +++++++++++++++++++ .../reconciler/ReactCompositeComponent.js | 2 +- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/scripts/fiber/tests-passing.txt b/scripts/fiber/tests-passing.txt index 6135f4f38c8b..71306da17e9a 100644 --- a/scripts/fiber/tests-passing.txt +++ b/scripts/fiber/tests-passing.txt @@ -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 diff --git a/src/renderers/__tests__/refs-test.js b/src/renderers/__tests__/refs-test.js index 6e4c8141f837..6f16f2fd34e4 100644 --- a/src/renderers/__tests__/refs-test.js +++ b/src/renderers/__tests__/refs-test.js @@ -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 =

Hello!

; + } + + render() { + return
{this.p}
; + } + } + + 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(); + }).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(); + }).toThrowError( + ReactDOMFeatureFlags.useFiber + ? fiberProdErrorMessage + : prodErrorMessage, + ); + } finally { + __DEV__ = originalDev; + } + }); +}); diff --git a/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js b/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js index f85aa6ed4980..52a4b08c8881 100644 --- a/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js +++ b/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js @@ -402,7 +402,7 @@ var ReactCompositeComponent = { publicContext, updateQueue, ) { - if (__DEV__) { + if (__DEV__ && !doConstruct) { ReactCurrentOwner.current = this; ReactDebugCurrentFrame.getCurrentStack = ReactDebugCurrentStack.getStackAddendum;