diff --git a/packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js b/packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js index ae27973a0c603..fab10a36e16d1 100644 --- a/packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js +++ b/packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js @@ -83,7 +83,7 @@ describe('ChangeEventPlugin', () => { // keep track of the "current" value and only fire events when it changes. // See https://github.com/facebook/react/pull/5746. - it('should consider initial text value to be current', () => { + it('should consider initial text value to be current', async () => { let called = 0; function cb(e) { @@ -91,10 +91,12 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + + const node = container.firstChild; node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); @@ -102,7 +104,7 @@ describe('ChangeEventPlugin', () => { expect(called).toBe(0); }); - it('should consider initial text value to be current (capture)', () => { + it('should consider initial text value to be current (capture)', async () => { let called = 0; function cb(e) { @@ -110,10 +112,14 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( + , + ); + }); + + const node = container.firstChild; node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); @@ -121,7 +127,7 @@ describe('ChangeEventPlugin', () => { expect(called).toBe(0); }); - it('should not invoke a change event for textarea same value', () => { + it('should not invoke a change event for textarea same value', async () => { let called = 0; function cb(e) { @@ -129,17 +135,19 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + + const node = container.firstChild; node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); // There should be no React change events because the value stayed the same. expect(called).toBe(0); }); - it('should not invoke a change event for textarea same value (capture)', () => { + it('should not invoke a change event for textarea same value (capture)', async () => { let called = 0; function cb(e) { @@ -147,17 +155,19 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + + const node = container.firstChild; node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); // There should be no React change events because the value stayed the same. expect(called).toBe(0); }); - it('should consider initial checkbox checked=true to be current', () => { + it('should consider initial checkbox checked=true to be current', async () => { let called = 0; function cb(e) { @@ -165,10 +175,14 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( + , + ); + }); + + const node = container.firstChild; // Secretly, set `checked` to false, so that dispatching the `click` will // make it `true` again. Thus, at the time of the event, React should not @@ -181,7 +195,7 @@ describe('ChangeEventPlugin', () => { expect(called).toBe(0); }); - it('should consider initial checkbox checked=false to be current', () => { + it('should consider initial checkbox checked=false to be current', async () => { let called = 0; function cb(e) { @@ -189,10 +203,14 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( + , + ); + }); + + const node = container.firstChild; // Secretly, set `checked` to true, so that dispatching the `click` will // make it `false` again. Thus, at the time of the event, React should not @@ -205,7 +223,7 @@ describe('ChangeEventPlugin', () => { expect(called).toBe(0); }); - it('should fire change for checkbox input', () => { + it('should fire change for checkbox input', async () => { let called = 0; function cb(e) { @@ -213,10 +231,12 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + + const node = container.firstChild; expect(node.checked).toBe(false); node.dispatchEvent( @@ -235,7 +255,7 @@ describe('ChangeEventPlugin', () => { expect(called).toBe(2); }); - it('should not fire change setting the value programmatically', () => { + it('should not fire change setting the value programmatically', async () => { let called = 0; function cb(e) { @@ -243,10 +263,12 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const input = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + + const input = container.firstChild; // Set it programmatically. input.value = 'bar'; @@ -271,7 +293,7 @@ describe('ChangeEventPlugin', () => { expect(called).toBe(1); }); - it('should not distinguish equal string and number values', () => { + it('should not distinguish equal string and number values', async () => { let called = 0; function cb(e) { @@ -279,10 +301,12 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const input = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + + const input = container.firstChild; // When we set `value` as a property, React updates the "current" value // that it tracks internally. The "current" value is later used to determine @@ -296,7 +320,7 @@ describe('ChangeEventPlugin', () => { }); // See a similar input test above for a detailed description of why. - it('should not fire change when setting checked programmatically', () => { + it('should not fire change when setting checked programmatically', async () => { let called = 0; function cb(e) { @@ -304,10 +328,14 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const input = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( + , + ); + }); + + const input = container.firstChild; // Set the value, updating the "current" value that React tracks to true. input.checked = true; @@ -326,13 +354,20 @@ describe('ChangeEventPlugin', () => { expect(called).toBe(1); }); - it('should unmount', () => { - const input = ReactDOM.render(, container); + it('should unmount', async () => { + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + + const input = container.firstChild; - ReactDOM.unmountComponentAtNode(container); + await act(() => { + root.unmount(); + }); }); - it('should only fire change for checked radio button once', () => { + it('should only fire change for checked radio button once', async () => { let called = 0; function cb(e) { @@ -340,10 +375,12 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const input = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + + const input = container.firstChild; setUntrackedChecked.call(input, true); input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true})); @@ -351,7 +388,7 @@ describe('ChangeEventPlugin', () => { expect(called).toBe(1); }); - it('should track radio button cousins in a group', () => { + it('should track radio button cousins in a group', async () => { let called1 = 0; let called2 = 0; @@ -365,13 +402,17 @@ describe('ChangeEventPlugin', () => { expect(e.type).toBe('change'); } - const div = ReactDOM.render( -