10
10
'use strict' ;
11
11
12
12
let React ;
13
- let ReactDOM ;
13
+ let ReactDOMClient ;
14
+ let act ;
14
15
15
16
describe ( 'SyntheticEvent' , ( ) => {
16
17
let container ;
18
+ let root ;
17
19
18
20
beforeEach ( ( ) => {
19
21
React = require ( 'react' ) ;
20
- ReactDOM = require ( 'react-dom' ) ;
22
+ ReactDOMClient = require ( 'react-dom/client' ) ;
23
+ act = require ( 'internal-test-utils' ) . act ;
21
24
22
25
container = document . createElement ( 'div' ) ;
26
+ root = ReactDOMClient . createRoot ( container ) ;
23
27
document . body . appendChild ( container ) ;
24
28
} ) ;
25
29
@@ -28,7 +32,7 @@ describe('SyntheticEvent', () => {
28
32
container = null ;
29
33
} ) ;
30
34
31
- it ( 'should be able to `preventDefault`' , ( ) => {
35
+ it ( 'should be able to `preventDefault`' , async ( ) => {
32
36
let expectedCount = 0 ;
33
37
34
38
const eventHandler = syntheticEvent => {
@@ -39,7 +43,11 @@ describe('SyntheticEvent', () => {
39
43
40
44
expectedCount ++ ;
41
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 ;
43
51
44
52
const event = document . createEvent ( 'Event' ) ;
45
53
event . initEvent ( 'click' , true , true ) ;
@@ -48,15 +56,19 @@ describe('SyntheticEvent', () => {
48
56
expect ( expectedCount ) . toBe ( 1 ) ;
49
57
} ) ;
50
58
51
- it ( 'should be prevented if nativeEvent is prevented' , ( ) => {
59
+ it ( 'should be prevented if nativeEvent is prevented' , async ( ) => {
52
60
let expectedCount = 0 ;
53
61
54
62
const eventHandler = syntheticEvent => {
55
63
expect ( syntheticEvent . isDefaultPrevented ( ) ) . toBe ( true ) ;
56
64
57
65
expectedCount ++ ;
58
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 ;
60
72
61
73
let event ;
62
74
event = document . createEvent ( 'Event' ) ;
@@ -80,7 +92,7 @@ describe('SyntheticEvent', () => {
80
92
expect ( expectedCount ) . toBe ( 2 ) ;
81
93
} ) ;
82
94
83
- it ( 'should be able to `stopPropagation`' , ( ) => {
95
+ it ( 'should be able to `stopPropagation`' , async ( ) => {
84
96
let expectedCount = 0 ;
85
97
86
98
const eventHandler = syntheticEvent => {
@@ -90,7 +102,11 @@ describe('SyntheticEvent', () => {
90
102
91
103
expectedCount ++ ;
92
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 ;
94
110
95
111
const event = document . createEvent ( 'Event' ) ;
96
112
event . initEvent ( 'click' , true , true ) ;
0 commit comments