From bed88852f362ffccab0d21a529abcf09db16f8bd Mon Sep 17 00:00:00 2001 From: Jack Pope Date: Thu, 11 Apr 2024 12:04:49 -0400 Subject: [PATCH] Add failing test --- .../src/__tests__/ReactComponent-test.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/react-dom/src/__tests__/ReactComponent-test.js b/packages/react-dom/src/__tests__/ReactComponent-test.js index 42dae06370edd..0fb81ba7366f4 100644 --- a/packages/react-dom/src/__tests__/ReactComponent-test.js +++ b/packages/react-dom/src/__tests__/ReactComponent-test.js @@ -129,6 +129,50 @@ describe('ReactComponent', () => { } }); + // @gate !disableStringRefs + it('should support accessing string refs from parent components', async () => { + spyOnDev(console, 'error').mockImplementation(() => {}); + + let refVal; + class Child extends React.Component { + componentDidUpdate() { + refVal = this.props.contextRef(); + } + + render() { + if (this.props.show) { + return
child
; + } + } + } + + class Parent extends React.Component { + render() { + return ( +
+ this.refs.root} + show={this.props.showChild} + /> +
+ ); + } + } + + const container = document.createElement('div'); + const root = ReactDOMClient.createRoot(container); + + await act(() => { + root.render(); + }); + + expect(refVal).toBe(undefined); + await act(() => { + root.render(); + }); + expect(refVal).toBe(container.querySelector('#test-root')); + }); + // @gate !disableStringRefs it('should support string refs on owned components', async () => { const innerObj = {};