From 7ebf75d7c418cf0721de0fe07efd4c9ea257285d Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 14 Feb 2024 19:52:53 +0100 Subject: [PATCH] Test that we don't call ref --- .../__tests__/ReactFunctionComponent-test.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactFunctionComponent-test.js b/packages/react-dom/src/__tests__/ReactFunctionComponent-test.js index e1692ddcd1904..08406bdd72d04 100644 --- a/packages/react-dom/src/__tests__/ReactFunctionComponent-test.js +++ b/packages/react-dom/src/__tests__/ReactFunctionComponent-test.js @@ -232,6 +232,7 @@ describe('ReactFunctionComponent', () => { ' in ParentUsingStringRef (at **)', ); + // No additional warnings should be logged const container = document.createElement('div'); const root = ReactDOMClient.createRoot(container); await act(() => { @@ -239,21 +240,17 @@ describe('ReactFunctionComponent', () => { }); }); - it('should warn when given a function ref', async () => { + it('should warn when given a function ref and ignore them', async () => { function Indirection(props) { return
{props.children}
; } + const ref = jest.fn(); class ParentUsingFunctionRef extends React.Component { render() { return ( - { - expect(arg).toBe(null); - }} - /> + ); } @@ -276,7 +273,9 @@ describe('ReactFunctionComponent', () => { ' in Indirection (at **)\n' + ' in ParentUsingFunctionRef (at **)', ); + expect(ref).not.toHaveBeenCalled(); + // No additional warnings should be logged const container = document.createElement('div'); const root = ReactDOMClient.createRoot(container); await act(() => { @@ -306,9 +305,9 @@ describe('ReactFunctionComponent', () => { }).toErrorDev('Warning: Function components cannot be given refs.'); // Should be deduped (offending element is on the same line): instance1.forceUpdate(); + // Should also be deduped (offending element is on the same line): let container = document.createElement('div'); let root = ReactDOMClient.createRoot(container); - await act(() => { root.render(); }); @@ -335,6 +334,7 @@ describe('ReactFunctionComponent', () => { }).toErrorDev('Warning: Function components cannot be given refs.'); // Should be deduped (same internal instance, no additional warnings) instance2.forceUpdate(); + // Could not be differentiated (since owner is anonymous and no source location) container = document.createElement('div'); root = ReactDOMClient.createRoot(container); await act(() => { @@ -362,6 +362,7 @@ describe('ReactFunctionComponent', () => { }).toErrorDev('Warning: Function components cannot be given refs.'); // Should be deduped (same owner name, no additional warnings): instance3.forceUpdate(); + // Should also be deduped (same owner name, no additional warnings): container = document.createElement('div'); root = ReactDOMClient.createRoot(container); await act(() => {