diff --git a/packages/react-dom/src/events/__tests__/SyntheticMouseEvent-test.js b/packages/react-dom/src/events/__tests__/SyntheticMouseEvent-test.js index cb927354f64d3..a20886a3e2c04 100644 --- a/packages/react-dom/src/events/__tests__/SyntheticMouseEvent-test.js +++ b/packages/react-dom/src/events/__tests__/SyntheticMouseEvent-test.js @@ -10,7 +10,8 @@ 'use strict'; let React; -let ReactDOM; +let ReactDOMClient; +let act; describe('SyntheticMouseEvent', () => { let container; @@ -18,7 +19,8 @@ describe('SyntheticMouseEvent', () => { beforeEach(() => { jest.resetModules(); React = require('react'); - ReactDOM = require('react-dom'); + ReactDOMClient = require('react-dom/client'); + act = require('internal-test-utils').act; // The container has to be attached for events to fire. container = document.createElement('div'); @@ -30,7 +32,7 @@ describe('SyntheticMouseEvent', () => { container = null; }); - it('should only use values from movementX/Y when event type is mousemove', () => { + it('should only use values from movementX/Y when event type is mousemove', async () => { const events = []; const onMouseMove = event => { events.push(event.movementX); @@ -40,10 +42,11 @@ describe('SyntheticMouseEvent', () => { events.push(event.movementX); }; - const node = ReactDOM.render( -
, - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render(); + }); + const node = container.firstChild; let event = new MouseEvent('mousemove', { relatedTarget: null, @@ -52,7 +55,9 @@ describe('SyntheticMouseEvent', () => { screenY: 2, }); - node.dispatchEvent(event); + await act(() => { + node.dispatchEvent(event); + }); event = new MouseEvent('mousemove', { relatedTarget: null, @@ -61,7 +66,9 @@ describe('SyntheticMouseEvent', () => { screenY: 8, }); - node.dispatchEvent(event); + await act(() => { + node.dispatchEvent(event); + }); // Now trigger a mousedown event to see if movementX has changed back to 0 event = new MouseEvent('mousedown', { @@ -71,7 +78,9 @@ describe('SyntheticMouseEvent', () => { screenY: 65, }); - node.dispatchEvent(event); + await act(() => { + node.dispatchEvent(event); + }); expect(events.length).toBe(3); expect(events[0]).toBe(0); @@ -79,7 +88,7 @@ describe('SyntheticMouseEvent', () => { expect(events[2]).toBe(0); // mousedown event should have movementX at 0 }); - it('should correctly calculate movementX/Y for capture phase', () => { + it('should correctly calculate movementX/Y for capture phase', async () => { const events = []; const onMouseMove = event => { events.push(['move', false, event.movementX, event.movementY]); @@ -94,15 +103,18 @@ describe('SyntheticMouseEvent', () => { events.push(['down', true, event.movementX, event.movementY]); }; - const node = ReactDOM.render( - , - container, - ); + const root = ReactDOMClient.createRoot(container); + await act(() => { + root.render( + , + ); + }); + const node = container.firstChild; let event = new MouseEvent('mousemove', { relatedTarget: null, @@ -111,7 +123,9 @@ describe('SyntheticMouseEvent', () => { screenY: 2, }); - node.dispatchEvent(event); + await act(() => { + node.dispatchEvent(event); + }); event = new MouseEvent('mousemove', { relatedTarget: null, @@ -120,7 +134,9 @@ describe('SyntheticMouseEvent', () => { screenY: 9, }); - node.dispatchEvent(event); + await act(() => { + node.dispatchEvent(event); + }); // Now trigger a mousedown event to see if movementX has changed back to 0 event = new MouseEvent('mousedown', { @@ -130,7 +146,9 @@ describe('SyntheticMouseEvent', () => { screenY: 65, }); - node.dispatchEvent(event); + await act(() => { + node.dispatchEvent(event); + }); expect(events).toEqual([ ['move', true, 0, 0],