10
10
'use strict' ;
11
11
12
12
let React ;
13
- let ReactDOM ;
13
+ let ReactDOMClient ;
14
+ let act ;
14
15
15
16
describe ( 'SyntheticMouseEvent' , ( ) => {
16
17
let container ;
17
18
18
19
beforeEach ( ( ) => {
19
20
jest . resetModules ( ) ;
20
21
React = require ( 'react' ) ;
21
- ReactDOM = require ( 'react-dom' ) ;
22
+ ReactDOMClient = require ( 'react-dom/client' ) ;
23
+ act = require ( 'internal-test-utils' ) . act ;
22
24
23
25
// The container has to be attached for events to fire.
24
26
container = document . createElement ( 'div' ) ;
@@ -30,7 +32,7 @@ describe('SyntheticMouseEvent', () => {
30
32
container = null ;
31
33
} ) ;
32
34
33
- it ( 'should only use values from movementX/Y when event type is mousemove' , ( ) => {
35
+ it ( 'should only use values from movementX/Y when event type is mousemove' , async ( ) => {
34
36
const events = [ ] ;
35
37
const onMouseMove = event => {
36
38
events . push ( event . movementX ) ;
@@ -40,10 +42,11 @@ describe('SyntheticMouseEvent', () => {
40
42
events . push ( event . movementX ) ;
41
43
} ;
42
44
43
- const node = ReactDOM . render (
44
- < div onMouseMove = { onMouseMove } onMouseDown = { onMouseDown } /> ,
45
- container ,
46
- ) ;
45
+ const root = ReactDOMClient . createRoot ( container ) ;
46
+ await act ( ( ) => {
47
+ root . render ( < div onMouseMove = { onMouseMove } onMouseDown = { onMouseDown } /> ) ;
48
+ } ) ;
49
+ const node = container . firstChild ;
47
50
48
51
let event = new MouseEvent ( 'mousemove' , {
49
52
relatedTarget : null ,
@@ -52,7 +55,9 @@ describe('SyntheticMouseEvent', () => {
52
55
screenY : 2 ,
53
56
} ) ;
54
57
55
- node . dispatchEvent ( event ) ;
58
+ await act ( ( ) => {
59
+ node . dispatchEvent ( event ) ;
60
+ } ) ;
56
61
57
62
event = new MouseEvent ( 'mousemove' , {
58
63
relatedTarget : null ,
@@ -61,7 +66,9 @@ describe('SyntheticMouseEvent', () => {
61
66
screenY : 8 ,
62
67
} ) ;
63
68
64
- node . dispatchEvent ( event ) ;
69
+ await act ( ( ) => {
70
+ node . dispatchEvent ( event ) ;
71
+ } ) ;
65
72
66
73
// Now trigger a mousedown event to see if movementX has changed back to 0
67
74
event = new MouseEvent ( 'mousedown' , {
@@ -71,15 +78,17 @@ describe('SyntheticMouseEvent', () => {
71
78
screenY : 65 ,
72
79
} ) ;
73
80
74
- node . dispatchEvent ( event ) ;
81
+ await act ( ( ) => {
82
+ node . dispatchEvent ( event ) ;
83
+ } ) ;
75
84
76
85
expect ( events . length ) . toBe ( 3 ) ;
77
86
expect ( events [ 0 ] ) . toBe ( 0 ) ;
78
87
expect ( events [ 1 ] ) . toBe ( 6 ) ;
79
88
expect ( events [ 2 ] ) . toBe ( 0 ) ; // mousedown event should have movementX at 0
80
89
} ) ;
81
90
82
- it ( 'should correctly calculate movementX/Y for capture phase' , ( ) => {
91
+ it ( 'should correctly calculate movementX/Y for capture phase' , async ( ) => {
83
92
const events = [ ] ;
84
93
const onMouseMove = event => {
85
94
events . push ( [ 'move' , false , event . movementX , event . movementY ] ) ;
@@ -94,15 +103,18 @@ describe('SyntheticMouseEvent', () => {
94
103
events . push ( [ 'down' , true , event . movementX , event . movementY ] ) ;
95
104
} ;
96
105
97
- const node = ReactDOM . render (
98
- < div
99
- onMouseMove = { onMouseMove }
100
- onMouseMoveCapture = { onMouseMoveCapture }
101
- onMouseDown = { onMouseDown }
102
- onMouseDownCapture = { onMouseDownCapture }
103
- /> ,
104
- container ,
105
- ) ;
106
+ const root = ReactDOMClient . createRoot ( container ) ;
107
+ await act ( ( ) => {
108
+ root . render (
109
+ < div
110
+ onMouseMove = { onMouseMove }
111
+ onMouseMoveCapture = { onMouseMoveCapture }
112
+ onMouseDown = { onMouseDown }
113
+ onMouseDownCapture = { onMouseDownCapture }
114
+ /> ,
115
+ ) ;
116
+ } ) ;
117
+ const node = container . firstChild ;
106
118
107
119
let event = new MouseEvent ( 'mousemove' , {
108
120
relatedTarget : null ,
@@ -111,7 +123,9 @@ describe('SyntheticMouseEvent', () => {
111
123
screenY : 2 ,
112
124
} ) ;
113
125
114
- node . dispatchEvent ( event ) ;
126
+ await act ( ( ) => {
127
+ node . dispatchEvent ( event ) ;
128
+ } ) ;
115
129
116
130
event = new MouseEvent ( 'mousemove' , {
117
131
relatedTarget : null ,
@@ -120,7 +134,9 @@ describe('SyntheticMouseEvent', () => {
120
134
screenY : 9 ,
121
135
} ) ;
122
136
123
- node . dispatchEvent ( event ) ;
137
+ await act ( ( ) => {
138
+ node . dispatchEvent ( event ) ;
139
+ } ) ;
124
140
125
141
// Now trigger a mousedown event to see if movementX has changed back to 0
126
142
event = new MouseEvent ( 'mousedown' , {
@@ -130,7 +146,9 @@ describe('SyntheticMouseEvent', () => {
130
146
screenY : 65 ,
131
147
} ) ;
132
148
133
- node . dispatchEvent ( event ) ;
149
+ await act ( ( ) => {
150
+ node . dispatchEvent ( event ) ;
151
+ } ) ;
134
152
135
153
expect ( events ) . toEqual ( [
136
154
[ 'move' , true , 0 , 0 ] ,
0 commit comments