Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert SyntheticMouseEvent to createRoot #28200

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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