From d49f12ab572d977cabe8e1a703b1d7a263e49b01 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Mon, 29 Jan 2024 09:10:34 +0100 Subject: [PATCH] Convert ReactDOMSelect to createRoot (#28142) --- .../src/__tests__/ReactDOMSelect-test.js | 1514 ++++++++++------- 1 file changed, 932 insertions(+), 582 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMSelect-test.js b/packages/react-dom/src/__tests__/ReactDOMSelect-test.js index 9df6b07e23f65..35aaba29a6a46 100644 --- a/packages/react-dom/src/__tests__/ReactDOMSelect-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMSelect-test.js @@ -19,8 +19,9 @@ Element.prototype.setAttribute = function (name, value) { describe('ReactDOMSelect', () => { let React; let ReactDOM; + let ReactDOMClient; let ReactDOMServer; - let ReactTestUtils; + let act; const noop = function () {}; @@ -28,11 +29,12 @@ describe('ReactDOMSelect', () => { 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; }); - it('should allow setting `defaultValue`', () => { + it('should allow setting `defaultValue`', async () => { const stub = ( {options}, - container, - ); + await act(() => { + root.render(); + }); + expect(node.value).toEqual('giraffe'); }); it('should not throw with `defaultValue` and without children', () => { const stub = @@ -71,17 +82,26 @@ describe('ReactDOMSelect', () => { ); const container = document.createElement('div'); - const node = ReactDOM.render(el, container); + const root = ReactDOMClient.createRoot(container); + + await act(() => { + root.render(el); + }); + + const node = container.firstChild; expect(node.value).toBe('giraffe'); node.value = 'monkey'; - ReactDOM.render(el, container); + await act(() => { + root.render(el); + }); + // Uncontrolled selects shouldn't change the value after first mounting expect(node.value).toEqual('monkey'); }); - it('should allow setting `defaultValue` with multiple', () => { + it('should allow setting `defaultValue` with multiple', async () => { const stub = ( - {options} - , - container, - ); + await act(() => { + root.render( + , + ); + }); expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(true); // giraffe expect(node.options[2].selected).toBe(true); // gorilla }); - it('should allow setting `value`', () => { + it('should allow setting `value`', async () => { const stub = ( - {options} - , - container, - ); + await act(() => { + root.render( + , + ); + }); + expect(node.value).toEqual('gorilla'); }); - it('should default to the first non-disabled option', () => { + it('should default to the first non-disabled option', async () => { const stub = ( ); const container = document.createElement('div'); - const node = ReactDOM.render(stub, container); + const root = ReactDOMClient.createRoot(container); + + await act(() => { + root.render(stub); + }); + + const node = container.firstChild; expect(node.options[0].selected).toBe(false); expect(node.options[2].selected).toBe(true); }); - it('should allow setting `value` to __proto__', () => { + it('should allow setting `value` to __proto__', async () => { const stub = ( - {options} - , - container, - ); + await act(() => { + root.render( + , + ); + }); + expect(node.value).toEqual('gorilla'); }); it('should not throw with `value` and without children', () => { const stub = @@ -191,26 +241,32 @@ describe('ReactDOMSelect', () => { ); const options = stub.props.children; const container = document.createElement('div'); - const node = ReactDOM.render(stub, container); + const root = ReactDOMClient.createRoot(container); + + await act(() => { + root.render(stub); + }); + + const node = container.firstChild; expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(true); // giraffe expect(node.options[2].selected).toBe(true); // gorilla - // Changing the `value` prop should change the selected options. - ReactDOM.render( - , - container, - ); + await act(() => { + root.render( + , + ); + }); expect(node.options[0].selected).toBe(true); // monkey expect(node.options[1].selected).toBe(false); // giraffe expect(node.options[2].selected).toBe(false); // gorilla }); - it('should allow setting `value` to __proto__ with multiple', () => { + it('should allow setting `value` to __proto__ with multiple', async () => { const stub = ( - {options} - , - container, - ); + await act(() => { + root.render( + , + ); + }); expect(node.options[0].selected).toBe(true); // monkey expect(node.options[1].selected).toBe(false); // __proto__ expect(node.options[2].selected).toBe(false); // gorilla }); - it('should not select other options automatically', () => { + it('should not select other options automatically', async () => { const stub = ( ); - const node = ReactTestUtils.renderIntoDocument(stub); + const container = document.createElement('div'); + const root = ReactDOMClient.createRoot(container); + + await act(() => { + root.render(stub); + }); + + const node = container.firstChild; expect(node.options[0].selected).toBe(false); // one expect(node.options[1].selected).toBe(false); // two expect(node.options[2].selected).toBe(true); // twelve }); - it('should reset child options selected when they are changed and `value` is set', () => { + it('should reset child options selected when they are changed and `value` is set', async () => { const stub = - - - - , - container, - ); + const node = container.firstChild; + + await act(() => { + root.render( + , + ); + }); expect(node.options[0].selected).toBe(true); // a expect(node.options[1].selected).toBe(true); // b expect(node.options[2].selected).toBe(false); // c }); - it('should allow setting `value` with `objectToString`', () => { + it('should allow setting `value` with `objectToString`', async () => { const objectToString = { animal: 'giraffe', toString: function () { @@ -289,7 +363,12 @@ describe('ReactDOMSelect', () => { ); const container = document.createElement('div'); - const node = ReactDOM.render(el, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(el); + }); + + const node = container.firstChild; expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(true); // giraffe @@ -305,14 +384,17 @@ describe('ReactDOMSelect', () => { ); - ReactDOM.render(el2, container); + + await act(() => { + root.render(el2); + }); expect(node.options[0].selected).toBe(true); // monkey expect(node.options[1].selected).toBe(false); // giraffe expect(node.options[2].selected).toBe(false); // gorilla }); - it('should allow switching to multiple', () => { + it('should allow switching to multiple', async () => { const stub = ( - {options} - , - container, - ); + await act(() => { + root.render( + , + ); + }); expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(true); // giraffe expect(node.options[2].selected).toBe(true); // gorilla }); - it('should allow switching from multiple', () => { + it('should allow switching from multiple', async () => { const stub = ( {options}, - container, - ); + await act(() => { + root.render(); + }); expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(false); // giraffe expect(node.options[2].selected).toBe(true); // gorilla }); - it('does not select an item when size is initially set to greater than 1', () => { + it('does not select an item when size is initially set to greater than 1', async () => { const stub = ( ); const container = document.createElement('div'); - const select = ReactDOM.render(stub, container); + const root = ReactDOMClient.createRoot(container); + + await act(() => { + root.render(stub); + }); + + const select = container.firstChild; expect(select.options[0].selected).toBe(false); expect(select.options[1].selected).toBe(false); @@ -388,7 +483,7 @@ describe('ReactDOMSelect', () => { expect(select.selectedIndex).toBe(-1); }); - it('should remember value when switching to uncontrolled', () => { + it('should remember value when switching to uncontrolled', async () => { const stub = ( {options}, container); + await act(() => { + root.render(); + }); expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(true); // giraffe expect(node.options[2].selected).toBe(false); // gorilla }); - it('should remember updated value when switching to uncontrolled', () => { + it('should remember updated value when switching to uncontrolled', async () => { const stub = ( - {options} - , - container, - ); + const node = container.firstChild; + await act(() => { + root.render( + , + ); + }); expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(false); // giraffe expect(node.options[2].selected).toBe(true); // gorilla - ReactDOM.render(, container); + await act(() => { + root.render(); + }); expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(false); // giraffe @@ -534,64 +643,71 @@ describe('ReactDOMSelect', () => { expect(options[2].selected).toBe(true); }); - it('should not control defaultValue if re-adding options', () => { + it('should not control defaultValue if re-adding options', async () => { const container = document.createElement('div'); - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + + await act(() => { + root.render( + , + ); + }); + + const node = container.firstChild; expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(true); // giraffe expect(node.options[2].selected).toBe(false); // gorilla - ReactDOM.render( - , - container, - ); + await act(() => { + root.render( + , + ); + }); expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(false); // gorilla - ReactDOM.render( - , - container, - ); + await act(() => { + root.render( + , + ); + }); expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(false); // giraffe expect(node.options[2].selected).toBe(false); // gorilla }); - it('should support options with dynamic children', () => { + it('should support options with dynamic children', async () => { const container = document.createElement('div'); let node; @@ -612,38 +728,51 @@ describe('ReactDOMSelect', () => { ); } - ReactDOM.render(, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + expect(node.options[0].selected).toBe(true); // monkey expect(node.options[1].selected).toBe(false); // giraffe expect(node.options[2].selected).toBe(false); // gorilla - ReactDOM.render(, container); + await act(() => { + root.render(); + }); + expect(node.options[0].selected).toBe(false); // monkey expect(node.options[1].selected).toBe(true); // giraffe expect(node.options[2].selected).toBe(false); // gorilla }); - it('should warn if value is null', () => { - expect(() => - ReactTestUtils.renderIntoDocument( - , - ), - ).toErrorDev( + it('should warn if value is null', async () => { + const container = document.createElement('div'); + const root = ReactDOMClient.createRoot(container); + await expect(async () => { + await act(() => { + root.render( + , + ); + }); + }).toErrorDev( '`value` prop on `select` should not be null. ' + 'Consider using an empty string to clear the component or `undefined` ' + 'for uncontrolled components.', ); - ReactTestUtils.renderIntoDocument( - , - ); + await act(() => { + root.render( + , + ); + }); }); - it('should warn if selected is set on