Skip to content

Commit ea45268

Browse files
Convert SyntheticEvent-test.js to createRoot
1 parent 7c79daf commit ea45268

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

packages/react-dom/src/events/__tests__/SyntheticEvent-test.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010
'use strict';
1111

1212
let React;
13-
let ReactDOM;
13+
let ReactDOMClient;
14+
let act;
1415

1516
describe('SyntheticEvent', () => {
1617
let container;
18+
let root;
1719

1820
beforeEach(() => {
1921
React = require('react');
20-
ReactDOM = require('react-dom');
22+
ReactDOMClient = require('react-dom/client');
23+
act = require('internal-test-utils').act;
2124

2225
container = document.createElement('div');
26+
root = ReactDOMClient.createRoot(container);
2327
document.body.appendChild(container);
2428
});
2529

@@ -28,7 +32,7 @@ describe('SyntheticEvent', () => {
2832
container = null;
2933
});
3034

31-
it('should be able to `preventDefault`', () => {
35+
it('should be able to `preventDefault`', async () => {
3236
let expectedCount = 0;
3337

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

4044
expectedCount++;
4145
};
42-
const node = ReactDOM.render(<div onClick={eventHandler} />, container);
46+
const nodeRef = React.createRef();
47+
await act(async () => {
48+
root.render(<div onClick={eventHandler} ref={nodeRef} />);
49+
});
50+
const node = nodeRef.current;
4351

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

51-
it('should be prevented if nativeEvent is prevented', () => {
59+
it('should be prevented if nativeEvent is prevented', async () => {
5260
let expectedCount = 0;
5361

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

5765
expectedCount++;
5866
};
59-
const node = ReactDOM.render(<div onClick={eventHandler} />, container);
67+
const nodeRef = React.createRef();
68+
await act(async () => {
69+
root.render(<div onClick={eventHandler} ref={nodeRef} />);
70+
});
71+
const node = nodeRef.current;
6072

6173
let event;
6274
event = document.createEvent('Event');
@@ -80,7 +92,7 @@ describe('SyntheticEvent', () => {
8092
expect(expectedCount).toBe(2);
8193
});
8294

83-
it('should be able to `stopPropagation`', () => {
95+
it('should be able to `stopPropagation`', async () => {
8496
let expectedCount = 0;
8597

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

91103
expectedCount++;
92104
};
93-
const node = ReactDOM.render(<div onClick={eventHandler} />, container);
105+
const nodeRef = React.createRef();
106+
await act(async () => {
107+
root.render(<div onClick={eventHandler} ref={nodeRef} />);
108+
});
109+
const node = nodeRef.current;
94110

95111
const event = document.createEvent('Event');
96112
event.initEvent('click', true, true);

0 commit comments

Comments
 (0)