From fc99528043adb7ad1a3645fbb384c4e4162d0c3a Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Tue, 30 Jan 2024 21:12:29 +0100 Subject: [PATCH] Convert SimpleEventPlugin to createRoot --- .../__tests__/SimpleEventPlugin-test.js | 154 +++++++++++------- 1 file changed, 91 insertions(+), 63 deletions(-) diff --git a/packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js b/packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js index 5479f5192d1fb..4cd9c8f27e6f5 100644 --- a/packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js +++ b/packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js @@ -11,7 +11,6 @@ describe('SimpleEventPlugin', function () { let React; - let ReactDOM; let ReactDOMClient; let Scheduler; let act; @@ -21,8 +20,10 @@ describe('SimpleEventPlugin', function () { let assertLog; let waitForAll; - function expectClickThru(element) { - element.click(); + async function expectClickThru(element) { + await act(() => { + element.click(); + }); expect(onClick).toHaveBeenCalledTimes(1); } @@ -31,23 +32,27 @@ describe('SimpleEventPlugin', function () { expect(onClick).toHaveBeenCalledTimes(0); } - function mounted(element) { + async function mounted(element) { container = document.createElement('div'); document.body.appendChild(container); - element = ReactDOM.render(element, container); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(element); + }); + element = container.firstChild; return element; } beforeEach(function () { jest.resetModules(); React = require('react'); - ReactDOM = require('react-dom'); ReactDOMClient = require('react-dom/client'); Scheduler = require('scheduler'); const InternalTestUtils = require('internal-test-utils'); assertLog = InternalTestUtils.assertLog; waitForAll = InternalTestUtils.waitForAll; + act = InternalTestUtils.act; onClick = jest.fn(); }); @@ -59,13 +64,13 @@ describe('SimpleEventPlugin', function () { } }); - it('A non-interactive tags click when disabled', function () { + it('A non-interactive tags click when disabled', async function () { const element =
; - expectClickThru(mounted(element)); + await expectClickThru(await mounted(element)); }); - it('A non-interactive tags clicks bubble when disabled', function () { - const element = mounted( + it('A non-interactive tags clicks bubble when disabled', async function () { + const element = await mounted(
, @@ -75,8 +80,8 @@ describe('SimpleEventPlugin', function () { expect(onClick).toHaveBeenCalledTimes(1); }); - it('does not register a click when clicking a child of a disabled element', function () { - const element = mounted( + it('does not register a click when clicking a child of a disabled element', async function () { + const element = await mounted( , @@ -87,8 +92,8 @@ describe('SimpleEventPlugin', function () { expect(onClick).toHaveBeenCalledTimes(0); }); - it('triggers click events for children of disabled elements', function () { - const element = mounted( + it('triggers click events for children of disabled elements', async function () { + const element = await mounted( , @@ -99,8 +104,8 @@ describe('SimpleEventPlugin', function () { expect(onClick).toHaveBeenCalledTimes(1); }); - it('triggers parent captured click events when target is a child of a disabled elements', function () { - const element = mounted( + it('triggers parent captured click events when target is a child of a disabled elements', async function () { + const element = await mounted(
, @@ -127,68 +132,76 @@ describe('SimpleEventPlugin', function () { ['button', 'input', 'select', 'textarea'].forEach(function (tagName) { describe(tagName, function () { - it('should forward clicks when it starts out not disabled', () => { + it('should forward clicks when it starts out not disabled', async () => { const element = React.createElement(tagName, { onClick: onClick, }); - expectClickThru(mounted(element)); + await expectClickThru(await mounted(element)); }); - it('should not forward clicks when it starts out disabled', () => { + it('should not forward clicks when it starts out disabled', async () => { const element = React.createElement(tagName, { onClick: onClick, disabled: true, }); - expectNoClickThru(mounted(element)); + await expectNoClickThru(await mounted(element)); }); - it('should forward clicks when it becomes not disabled', () => { + it('should forward clicks when it becomes not disabled', async () => { container = document.createElement('div'); document.body.appendChild(container); - let element = ReactDOM.render( - React.createElement(tagName, {onClick: onClick, disabled: true}), - container, - ); - element = ReactDOM.render( - React.createElement(tagName, {onClick: onClick}), - container, - ); - expectClickThru(element); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( + React.createElement(tagName, {onClick: onClick, disabled: true}), + ); + }); + await act(() => { + root.render(React.createElement(tagName, {onClick: onClick})); + }); + const element = container.firstChild; + await expectClickThru(element); }); - it('should not forward clicks when it becomes disabled', () => { + it('should not forward clicks when it becomes disabled', async () => { container = document.createElement('div'); document.body.appendChild(container); - let element = ReactDOM.render( - React.createElement(tagName, {onClick: onClick}), - container, - ); - element = ReactDOM.render( - React.createElement(tagName, {onClick: onClick, disabled: true}), - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(React.createElement(tagName, {onClick: onClick})); + }); + await act(() => { + root.render( + React.createElement(tagName, {onClick: onClick, disabled: true}), + ); + }); + const element = container.firstChild; expectNoClickThru(element); }); - it('should work correctly if the listener is changed', () => { + it('should work correctly if the listener is changed', async () => { container = document.createElement('div'); document.body.appendChild(container); - let element = ReactDOM.render( - React.createElement(tagName, {onClick: onClick, disabled: true}), - container, - ); - element = ReactDOM.render( - React.createElement(tagName, {onClick: onClick, disabled: false}), - container, - ); - expectClickThru(element); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( + React.createElement(tagName, {onClick: onClick, disabled: true}), + ); + }); + await act(() => { + root.render( + React.createElement(tagName, {onClick: onClick, disabled: false}), + ); + }); + const element = container.firstChild; + await expectClickThru(element); }); }); }); - it('batches updates that occur as a result of a nested event dispatch', () => { + it('batches updates that occur as a result of a nested event dispatch', async () => { container = document.createElement('div'); document.body.appendChild(container); @@ -226,12 +239,17 @@ describe('SimpleEventPlugin', function () { ); } - ReactDOM.render(