From 586bb98992e1e02f65038c371b3530b0218e27d1 Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Tue, 9 Jan 2024 11:43:06 -0500 Subject: [PATCH] Convert DOMPropertyOperations-test to createRoot --- .../__tests__/DOMPropertyOperations-test.js | 741 +++++++++++------- 1 file changed, 468 insertions(+), 273 deletions(-) diff --git a/packages/react-dom/src/__tests__/DOMPropertyOperations-test.js b/packages/react-dom/src/__tests__/DOMPropertyOperations-test.js index bf523c4494e81..87885ca07b4a5 100644 --- a/packages/react-dom/src/__tests__/DOMPropertyOperations-test.js +++ b/packages/react-dom/src/__tests__/DOMPropertyOperations-test.js @@ -17,12 +17,14 @@ const { describe('DOMPropertyOperations', () => { let React; - let ReactDOM; + let ReactDOMClient; + let act; beforeEach(() => { jest.resetModules(); React = require('react'); - ReactDOM = require('react-dom'); + ReactDOMClient = require('react-dom/client'); + ({act} = require('internal-test-utils')); }); // Sets a value in a way that React doesn't see, @@ -37,25 +39,34 @@ describe('DOMPropertyOperations', () => { ).set; describe('setValueForProperty', () => { - it('should set values as properties by default', () => { + it('should set values as properties by default', async () => { const container = document.createElement('div'); - ReactDOM.render(
, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(
); + }); expect(container.firstChild.title).toBe('Tip!'); }); - it('should set values as attributes if necessary', () => { + it('should set values as attributes if necessary', async () => { const container = document.createElement('div'); - ReactDOM.render(
, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(
); + }); expect(container.firstChild.getAttribute('role')).toBe('#'); expect(container.firstChild.role).toBeUndefined(); }); - it('should set values as namespace attributes if necessary', () => { + it('should set values as namespace attributes if necessary', async () => { const container = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg', ); - ReactDOM.render(, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); expect( container.firstChild.getAttributeNS( 'http://www.w3.org/1999/xlink', @@ -64,23 +75,38 @@ describe('DOMPropertyOperations', () => { ).toBe('about:blank'); }); - it('should set values as boolean properties', () => { + it('should set values as boolean properties', async () => { const container = document.createElement('div'); - ReactDOM.render(
, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(
); + }); expect(container.firstChild.getAttribute('disabled')).toBe(''); - ReactDOM.render(
, container); + await act(() => { + root.render(
); + }); expect(container.firstChild.getAttribute('disabled')).toBe(''); - ReactDOM.render(
, container); + await act(() => { + root.render(
); + }); expect(container.firstChild.getAttribute('disabled')).toBe(null); - ReactDOM.render(
, container); - ReactDOM.render(
, container); + await act(() => { + root.render(
); + }); + await act(() => { + root.render(
); + }); expect(container.firstChild.getAttribute('disabled')).toBe(null); - ReactDOM.render(
, container); - ReactDOM.render(
, container); + await act(() => { + root.render(
); + }); + await act(() => { + root.render(
); + }); expect(container.firstChild.getAttribute('disabled')).toBe(null); }); - it('should convert attribute values to string first', () => { + it('should convert attribute values to string first', async () => { // Browsers default to this behavior, but some test environments do not. // This ensures that we have consistent behavior. const obj = { @@ -90,13 +116,19 @@ describe('DOMPropertyOperations', () => { }; const container = document.createElement('div'); - ReactDOM.render(
, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(
); + }); expect(container.firstChild.getAttribute('class')).toBe('css-class'); }); - it('should not remove empty attributes for special input properties', () => { + it('should not remove empty attributes for special input properties', async () => { const container = document.createElement('div'); - ReactDOM.render( {}} />, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( {}} />); + }); if (disableInputAttributeSyncing) { expect(container.firstChild.hasAttribute('value')).toBe(false); } else { @@ -105,79 +137,114 @@ describe('DOMPropertyOperations', () => { expect(container.firstChild.value).toBe(''); }); - it('should not remove empty attributes for special option properties', () => { + it('should not remove empty attributes for special option properties', async () => { const container = document.createElement('div'); - ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( + , + ); + }); // Regression test for https://github.com/facebook/react/issues/6219 expect(container.firstChild.firstChild.value).toBe(''); expect(container.firstChild.lastChild.value).toBe('filled'); }); - it('should remove for falsey boolean properties', () => { + it('should remove for falsey boolean properties', async () => { const container = document.createElement('div'); - ReactDOM.render(
, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(
); + }); expect(container.firstChild.hasAttribute('allowFullScreen')).toBe(false); }); - it('should remove when setting custom attr to null', () => { + it('should remove when setting custom attr to null', async () => { const container = document.createElement('div'); - ReactDOM.render(
, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(
); + }); expect(container.firstChild.hasAttribute('data-foo')).toBe(true); - ReactDOM.render(
, container); + await act(() => { + root.render(
); + }); expect(container.firstChild.hasAttribute('data-foo')).toBe(false); }); - it('should set className to empty string instead of null', () => { + it('should set className to empty string instead of null', async () => { const container = document.createElement('div'); - ReactDOM.render(
, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(
); + }); expect(container.firstChild.className).toBe('selected'); - ReactDOM.render(
, container); + await act(() => { + root.render(
); + }); // className should be '', not 'null' or null (which becomes 'null' in // some browsers) expect(container.firstChild.className).toBe(''); expect(container.firstChild.getAttribute('class')).toBe(null); }); - it('should remove property properly for boolean properties', () => { + it('should remove property properly for boolean properties', async () => { const container = document.createElement('div'); - ReactDOM.render(