From 1ee3cf53eb6b4bd814f4492f32f131155288a9b5 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Fri, 2 Feb 2024 09:14:34 +0100 Subject: [PATCH] Convert InvalidEventListeners to createRoot (#28180) --- .../__tests__/InvalidEventListeners-test.js | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/packages/react-dom/src/__tests__/InvalidEventListeners-test.js b/packages/react-dom/src/__tests__/InvalidEventListeners-test.js index c5df3e1196ff5..e7c9b5f9610ca 100644 --- a/packages/react-dom/src/__tests__/InvalidEventListeners-test.js +++ b/packages/react-dom/src/__tests__/InvalidEventListeners-test.js @@ -13,13 +13,15 @@ jest.mock('react-dom-bindings/src/events/isEventSupported'); describe('InvalidEventListeners', () => { let React; - let ReactDOM; + let ReactDOMClient; + let act; let container; beforeEach(() => { jest.resetModules(); React = require('react'); - ReactDOM = require('react-dom'); + ReactDOMClient = require('react-dom/client'); + act = require('internal-test-utils').act; container = document.createElement('div'); document.body.appendChild(container); @@ -30,13 +32,16 @@ describe('InvalidEventListeners', () => { container = null; }); - it('should prevent non-function listeners, at dispatch', () => { - let node; - expect(() => { - node = ReactDOM.render(
, container); + it('should prevent non-function listeners, at dispatch', async () => { + const root = ReactDOMClient.createRoot(container); + await expect(async () => { + await act(() => { + root.render(
); + }); }).toErrorDev( 'Expected `onClick` listener to be a function, instead got a value of `string` type.', ); + const node = container.firstChild; spyOnProd(console, 'error'); @@ -46,11 +51,13 @@ describe('InvalidEventListeners', () => { } window.addEventListener('error', handleWindowError); try { - node.dispatchEvent( - new MouseEvent('click', { - bubbles: true, - }), - ); + await act(() => { + node.dispatchEvent( + new MouseEvent('click', { + bubbles: true, + }), + ); + }); } finally { window.removeEventListener('error', handleWindowError); } @@ -77,12 +84,19 @@ describe('InvalidEventListeners', () => { } }); - it('should not prevent null listeners, at dispatch', () => { - const node = ReactDOM.render(
, container); - node.dispatchEvent( - new MouseEvent('click', { - bubbles: true, - }), - ); + it('should not prevent null listeners, at dispatch', async () => { + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(
); + }); + + const node = container.firstChild; + await act(() => { + node.dispatchEvent( + new MouseEvent('click', { + bubbles: true, + }), + ); + }); }); });