From 174b42006fe7fffdbce25a673cd300536266a080 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Sat, 27 Jan 2024 11:42:38 +0100 Subject: [PATCH 1/3] Convert ReactDOMTextarea to createRoot --- .../src/__tests__/ReactDOMTextarea-test.js | 826 ++++++++++++------ 1 file changed, 548 insertions(+), 278 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js b/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js index 810452c64a313..adc6ba415a7c3 100644 --- a/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js @@ -13,9 +13,9 @@ function emptyFunction() {} describe('ReactDOMTextarea', () => { let React; - let ReactDOM; + let ReactDOMClient; let ReactDOMServer; - let ReactTestUtils; + let act; let renderTextarea; @@ -25,15 +25,16 @@ describe('ReactDOMTextarea', () => { jest.resetModules(); React = require('react'); - ReactDOM = require('react-dom'); + ReactDOMClient = require('react-dom/client'); ReactDOMServer = require('react-dom/server'); - ReactTestUtils = require('react-dom/test-utils'); + act = require('internal-test-utils').act; - renderTextarea = function (component, container) { - if (!container) { - container = document.createElement('div'); - } - const node = ReactDOM.render(component, container); + renderTextarea = async function (component, container, root) { + await act(() => { + root.render(component); + }); + + const node = container.firstChild; // Fixing jsdom's quirky behavior -- in reality, the parser should strip // off the leading newline but we need to do it by hand here. @@ -46,93 +47,143 @@ describe('ReactDOMTextarea', () => { jest.restoreAllMocks(); }); - it('should allow setting `defaultValue`', () => { + it('should allow setting `defaultValue`', async () => { const container = document.createElement('div'); - const node = renderTextarea(, container); + await act(() => { + root.render(); + }); expect(node.value).toBe('0'); }); - it('should not incur unnecessary DOM mutations', () => { + it('should not incur unnecessary DOM mutations', async () => { const container = document.createElement('div'); - ReactDOM.render(; + const root = ReactDOMClient.createRoot(container); let node; - expect(() => { - node = renderTextarea(stub, container); + await expect(async () => { + node = await renderTextarea( + , + container, + root, + ); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on , container); + + await act(() => { + root.render(); + }); + expect(node.value).toEqual(''); }); } if (ReactFeatureFlags.disableTextareaChildren) { - it('should receive defaultValue and still ignore children content', () => { + it('should receive defaultValue and still ignore children content', async () => { + const container = document.createElement('div'); + const root = ReactDOMClient.createRoot(container); let node; - expect(() => { - node = renderTextarea( + await expect(async () => { + node = await renderTextarea( , + container, + root, ); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on ; + const root = ReactDOMClient.createRoot(container); let node; - expect(() => { - node = renderTextarea(stub, container); + await expect(async () => { + node = await renderTextarea( + , + container, + root, + ); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on , container); + await act(() => { + root.render(); + }); + expect(node.value).toEqual('giraffe'); }); } - it('should keep value when switching to uncontrolled element if not changed', () => { + it('should keep value when switching to uncontrolled element if not changed', async () => { const container = document.createElement('div'); - - const node = renderTextarea( + const root = ReactDOMClient.createRoot(container); + const node = await renderTextarea( ); + await expect(async () => { + node = await renderTextarea(, container, root); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on ); + await expect(async () => { + node = await renderTextarea(, container, root); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on ); + await expect(async () => { + node = await renderTextarea( + , + container, + root, + ); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on ); + await expect(async () => { + node = await renderTextarea( + , + container, + root, + ); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on ); + await expect(async () => { + node = await renderTextarea( + , + container, + root, + ); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on ); + await expect(async () => { + node = await renderTextarea( + , + container, + root, + ); }).toErrorDev( 'Use the `defaultValue` or `value` props instead of setting children on , - ), - ).toThrow(', + ); + }); + }).rejects.toThrow(', - )), - ).not.toThrow(); - }).toErrorDev( + container, + root, + )))(), + ).resolves.not.toThrow(); + }).toErrorDev([ 'Use the `defaultValue` or `value` props instead of setting children on , - container, - ), - ).toErrorDev('Use the `defaultValue` or `value` props'); + const root = ReactDOMClient.createRoot(container); + await expect(async () => { + await act(() => { + root.render(); + }); + }).toErrorDev('Use the `defaultValue` or `value` props'); const node = container.firstChild; expect(node.value).toBe(''); }); - it('treats updated Symbol value as an empty string', () => { + it('treats updated Symbol value as an empty string', async () => { const container = document.createElement('div'); - ReactDOM.render(, - container, - ), - ).toErrorDev('Use the `defaultValue` or `value` props'); + await expect(async () => { + const root = ReactDOMClient.createRoot(container); + + await act(() => { + root.render(); + }); + }).toErrorDev('Use the `defaultValue` or `value` props'); const node = container.firstChild; expect(node.value).toBe(''); }); - it('treats updated function value as an empty string', () => { + it('treats updated function value as an empty string', async () => { const container = document.createElement('div'); - ReactDOM.render(