diff --git a/packages/react-test-renderer/src/ReactShallowRenderer.js b/packages/react-test-renderer/src/ReactShallowRenderer.js index 7472556c68954..a2775eb2c446c 100644 --- a/packages/react-test-renderer/src/ReactShallowRenderer.js +++ b/packages/react-test-renderer/src/ReactShallowRenderer.js @@ -521,9 +521,7 @@ class ReactShallowRenderer { if (this._instance) { this._updateClassComponent(element, this._context); } else { - if (isForwardRef(element)) { - this._rendered = element.type.render(element.props, element.ref); - } else if (shouldConstruct(element.type)) { + if (shouldConstruct(element.type)) { this._instance = new element.type( element.props, this._context, @@ -565,11 +563,15 @@ class ReactShallowRenderer { ReactCurrentDispatcher.current = this._dispatcher; this._prepareToUseHooks(element.type); try { - this._rendered = element.type.call( - undefined, - element.props, - this._context, - ); + if (isForwardRef(element)) { + this._rendered = element.type.render(element.props, element.ref); + } else { + this._rendered = element.type.call( + undefined, + element.props, + this._context, + ); + } } finally { ReactCurrentDispatcher.current = prevDispatcher; } diff --git a/packages/react-test-renderer/src/__tests__/ReactShallowRendererHooks-test.js b/packages/react-test-renderer/src/__tests__/ReactShallowRendererHooks-test.js index da352a971e160..29fa94433db2c 100644 --- a/packages/react-test-renderer/src/__tests__/ReactShallowRendererHooks-test.js +++ b/packages/react-test-renderer/src/__tests__/ReactShallowRendererHooks-test.js @@ -304,4 +304,22 @@ describe('ReactShallowRenderer with hooks', () => { , ); }); + + it('should work with with forwardRef + any hook', () => { + const SomeComponent = React.forwardRef((props, ref) => { + const randomNumberRef = React.useRef({number: Math.random()}); + + return ( +
+

The random number is: {randomNumberRef.current.number}

+
+ ); + }); + + const shallowRenderer = createRenderer(); + let firstResult = shallowRenderer.render(); + let secondResult = shallowRenderer.render(); + + expect(firstResult).toEqual(secondResult); + }); });