Convert SyntheticEvent-test.js to createRoot (#28118)
Matt Carroll committed
Jan 26, 2024 at 13:17 UTC
8f998bf93fdc417ea9e8f99dedfe3a7c3d6e5bbc
1 file changed
+24
-8
packages/react-dom/src/events/__tests__/SyntheticEvent-test.js
+24
-8
@@ -10,16 +10,20 @@
10
'use strict';
11
12
let React;
13
-let ReactDOM;
13
+let ReactDOMClient;
14
+let act;
15
16
describe('SyntheticEvent', () => {
17
let container;
18
+ let root;
19
20
beforeEach(() => {
21
React = require('react');
20
- ReactDOM = require('react-dom');
22
+ ReactDOMClient = require('react-dom/client');
23
+ act = require('internal-test-utils').act;
24
25
container = document.createElement('div');
26
+ root = ReactDOMClient.createRoot(container);
27
document.body.appendChild(container);
28
});
29
@@ -28,7 +32,7 @@ describe('SyntheticEvent', () => {
32
container = null;
33
});
34
31
- it('should be able to `preventDefault`', () => {
35
+ it('should be able to `preventDefault`', async () => {
36
let expectedCount = 0;
37
38
const eventHandler = syntheticEvent => {
@@ -39,7 +43,11 @@ describe('SyntheticEvent', () => {
43
44
expectedCount++;
45
};
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;
51
52
const event = document.createEvent('Event');
53
event.initEvent('click', true, true);
@@ -48,7 +56,7 @@ describe('SyntheticEvent', () => {
56
expect(expectedCount).toBe(1);
57
});
58
51
- it('should be prevented if nativeEvent is prevented', () => {
59
+ it('should be prevented if nativeEvent is prevented', async () => {
60
let expectedCount = 0;
61
62
const eventHandler = syntheticEvent => {
@@ -56,7 +64,11 @@ describe('SyntheticEvent', () => {
64
65
expectedCount++;
66
};
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;
72
73
let event;
74
event = document.createEvent('Event');
@@ -80,7 +92,7 @@ describe('SyntheticEvent', () => {
92
expect(expectedCount).toBe(2);
93
});
94
83
- it('should be able to `stopPropagation`', () => {
95
+ it('should be able to `stopPropagation`', async () => {
96
let expectedCount = 0;
97
98
const eventHandler = syntheticEvent => {
@@ -90,7 +102,11 @@ describe('SyntheticEvent', () => {
102
103
expectedCount++;
104
};
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;
110
111
const event = document.createEvent('Event');
112
event.initEvent('click', true, true);