Skip to content

Commit

Permalink
Convert SyntheticMouseEvent to createRoot (#28200)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored and gaearon committed Feb 3, 2024
1 parent 4f582a1 commit 1313715
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions packages/react-dom/src/events/__tests__/SyntheticMouseEvent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
'use strict';

let React;
let ReactDOM;
let ReactDOMClient;
let act;

describe('SyntheticMouseEvent', () => {
let container;

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');
Expand All @@ -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);
Expand All @@ -40,10 +42,11 @@ describe('SyntheticMouseEvent', () => {
events.push(event.movementX);
};

const node = ReactDOM.render(
<div onMouseMove={onMouseMove} onMouseDown={onMouseDown} />,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<div onMouseMove={onMouseMove} onMouseDown={onMouseDown} />);
});
const node = container.firstChild;

let event = new MouseEvent('mousemove', {
relatedTarget: null,
Expand All @@ -52,7 +55,9 @@ describe('SyntheticMouseEvent', () => {
screenY: 2,
});

node.dispatchEvent(event);
await act(() => {
node.dispatchEvent(event);
});

event = new MouseEvent('mousemove', {
relatedTarget: null,
Expand All @@ -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', {
Expand All @@ -71,15 +78,17 @@ describe('SyntheticMouseEvent', () => {
screenY: 65,
});

node.dispatchEvent(event);
await act(() => {
node.dispatchEvent(event);
});

expect(events.length).toBe(3);
expect(events[0]).toBe(0);
expect(events[1]).toBe(6);
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]);
Expand All @@ -94,15 +103,18 @@ describe('SyntheticMouseEvent', () => {
events.push(['down', true, event.movementX, event.movementY]);
};

const node = ReactDOM.render(
<div
onMouseMove={onMouseMove}
onMouseMoveCapture={onMouseMoveCapture}
onMouseDown={onMouseDown}
onMouseDownCapture={onMouseDownCapture}
/>,
container,
);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div
onMouseMove={onMouseMove}
onMouseMoveCapture={onMouseMoveCapture}
onMouseDown={onMouseDown}
onMouseDownCapture={onMouseDownCapture}
/>,
);
});
const node = container.firstChild;

let event = new MouseEvent('mousemove', {
relatedTarget: null,
Expand All @@ -111,7 +123,9 @@ describe('SyntheticMouseEvent', () => {
screenY: 2,
});

node.dispatchEvent(event);
await act(() => {
node.dispatchEvent(event);
});

event = new MouseEvent('mousemove', {
relatedTarget: null,
Expand All @@ -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', {
Expand All @@ -130,7 +146,9 @@ describe('SyntheticMouseEvent', () => {
screenY: 65,
});

node.dispatchEvent(event);
await act(() => {
node.dispatchEvent(event);
});

expect(events).toEqual([
['move', true, 0, 0],
Expand Down

0 comments on commit 1313715

Please sign in to comment.