From 186f5004c2ab023c6992e611cd4286d41a8acc24 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 13 Mar 2019 13:32:12 +0100 Subject: [PATCH 1/2] test: Add test for shallow + forwardRef + hook --- .../ReactShallowRendererHooks-test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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); + }); }); From f5955512f94f71d1208c2e58fee440a852e39564 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 13 Mar 2019 13:36:48 +0100 Subject: [PATCH 2/2] fix(react-test-renderer): shallow forwardRef hooks --- .../src/ReactShallowRenderer.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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; }