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 SyntheticEvent-test.js to createRoot #28118

Merged
merged 1 commit into from
Jan 26, 2024
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
32 changes: 24 additions & 8 deletions packages/react-dom/src/events/__tests__/SyntheticEvent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
'use strict';

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

describe('SyntheticEvent', () => {
let container;
let root;

beforeEach(() => {
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
act = require('internal-test-utils').act;

container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
document.body.appendChild(container);
});

Expand All @@ -28,7 +32,7 @@ describe('SyntheticEvent', () => {
container = null;
});

it('should be able to `preventDefault`', () => {
it('should be able to `preventDefault`', async () => {
let expectedCount = 0;

const eventHandler = syntheticEvent => {
Expand All @@ -39,7 +43,11 @@ describe('SyntheticEvent', () => {

expectedCount++;
};
const node = ReactDOM.render(<div onClick={eventHandler} />, container);
const nodeRef = React.createRef();
await act(async () => {
root.render(<div onClick={eventHandler} ref={nodeRef} />);
});
const node = nodeRef.current;

const event = document.createEvent('Event');
event.initEvent('click', true, true);
Expand All @@ -48,15 +56,19 @@ describe('SyntheticEvent', () => {
expect(expectedCount).toBe(1);
});

it('should be prevented if nativeEvent is prevented', () => {
it('should be prevented if nativeEvent is prevented', async () => {
let expectedCount = 0;

const eventHandler = syntheticEvent => {
expect(syntheticEvent.isDefaultPrevented()).toBe(true);

expectedCount++;
};
const node = ReactDOM.render(<div onClick={eventHandler} />, container);
const nodeRef = React.createRef();
await act(async () => {
root.render(<div onClick={eventHandler} ref={nodeRef} />);
});
const node = nodeRef.current;

let event;
event = document.createEvent('Event');
Expand All @@ -80,7 +92,7 @@ describe('SyntheticEvent', () => {
expect(expectedCount).toBe(2);
});

it('should be able to `stopPropagation`', () => {
it('should be able to `stopPropagation`', async () => {
let expectedCount = 0;

const eventHandler = syntheticEvent => {
Expand All @@ -90,7 +102,11 @@ describe('SyntheticEvent', () => {

expectedCount++;
};
const node = ReactDOM.render(<div onClick={eventHandler} />, container);
const nodeRef = React.createRef();
await act(async () => {
root.render(<div onClick={eventHandler} ref={nodeRef} />);
});
const node = nodeRef.current;

const event = document.createEvent('Event');
event.initEvent('click', true, true);
Expand Down