1111
1212let React ;
1313let ReactDOM ;
14+ let ReactDOMClient ;
15+ let act ;
1416
1517describe ( 'EnterLeaveEventPlugin' , ( ) => {
1618 let container ;
@@ -20,6 +22,8 @@ describe('EnterLeaveEventPlugin', () => {
2022
2123 React = require ( 'react' ) ;
2224 ReactDOM = require ( 'react-dom' ) ;
25+ ReactDOMClient = require ( 'react-dom/client' ) ;
26+ act = require ( 'internal-test-utils' ) . act ;
2327
2428 // The container has to be attached for events to fire.
2529 container = document . createElement ( 'div' ) ;
@@ -31,7 +35,7 @@ describe('EnterLeaveEventPlugin', () => {
3135 container = null ;
3236 } ) ;
3337
34- it ( 'should set onMouseLeave relatedTarget properly in iframe' , ( ) => {
38+ it ( 'should set onMouseLeave relatedTarget properly in iframe' , async ( ) => {
3539 const iframe = document . createElement ( 'iframe' ) ;
3640 container . appendChild ( iframe ) ;
3741 const iframeDocument = iframe . contentDocument ;
@@ -41,30 +45,36 @@ describe('EnterLeaveEventPlugin', () => {
4145 iframeDocument . close ( ) ;
4246
4347 const leaveEvents = [ ] ;
44- const node = ReactDOM . render (
45- < div
46- onMouseLeave = { e => {
47- e . persist ( ) ;
48- leaveEvents . push ( e ) ;
49- } }
50- /> ,
48+ const root = ReactDOMClient . createRoot (
5149 iframeDocument . body . getElementsByTagName ( 'div' ) [ 0 ] ,
5250 ) ;
53-
54- node . dispatchEvent (
55- new MouseEvent ( 'mouseout' , {
56- bubbles : true ,
57- cancelable : true ,
58- relatedTarget : iframe . contentWindow ,
59- } ) ,
60- ) ;
51+ await act ( ( ) => {
52+ root . render (
53+ < div
54+ onMouseLeave = { e => {
55+ e . persist ( ) ;
56+ leaveEvents . push ( e ) ;
57+ } }
58+ /> ,
59+ ) ;
60+ } ) ;
61+ const node = iframeDocument . body . getElementsByTagName ( 'div' ) [ 0 ] . firstChild ;
62+ await act ( ( ) => {
63+ node . dispatchEvent (
64+ new MouseEvent ( 'mouseout' , {
65+ bubbles : true ,
66+ cancelable : true ,
67+ relatedTarget : iframe . contentWindow ,
68+ } ) ,
69+ ) ;
70+ } ) ;
6171
6272 expect ( leaveEvents . length ) . toBe ( 1 ) ;
6373 expect ( leaveEvents [ 0 ] . target ) . toBe ( node ) ;
6474 expect ( leaveEvents [ 0 ] . relatedTarget ) . toBe ( iframe . contentWindow ) ;
6575 } ) ;
6676
67- it ( 'should set onMouseEnter relatedTarget properly in iframe' , ( ) => {
77+ it ( 'should set onMouseEnter relatedTarget properly in iframe' , async ( ) => {
6878 const iframe = document . createElement ( 'iframe' ) ;
6979 container . appendChild ( iframe ) ;
7080 const iframeDocument = iframe . contentDocument ;
@@ -74,31 +84,37 @@ describe('EnterLeaveEventPlugin', () => {
7484 iframeDocument . close ( ) ;
7585
7686 const enterEvents = [ ] ;
77- const node = ReactDOM . render (
78- < div
79- onMouseEnter = { e => {
80- e . persist ( ) ;
81- enterEvents . push ( e ) ;
82- } }
83- /> ,
87+ const root = ReactDOMClient . createRoot (
8488 iframeDocument . body . getElementsByTagName ( 'div' ) [ 0 ] ,
8589 ) ;
86-
87- node . dispatchEvent (
88- new MouseEvent ( 'mouseover' , {
89- bubbles : true ,
90- cancelable : true ,
91- relatedTarget : null ,
92- } ) ,
93- ) ;
90+ await act ( ( ) => {
91+ root . render (
92+ < div
93+ onMouseEnter = { e => {
94+ e . persist ( ) ;
95+ enterEvents . push ( e ) ;
96+ } }
97+ /> ,
98+ ) ;
99+ } ) ;
100+ const node = iframeDocument . body . getElementsByTagName ( 'div' ) [ 0 ] . firstChild ;
101+ await act ( ( ) => {
102+ node . dispatchEvent (
103+ new MouseEvent ( 'mouseover' , {
104+ bubbles : true ,
105+ cancelable : true ,
106+ relatedTarget : null ,
107+ } ) ,
108+ ) ;
109+ } ) ;
94110
95111 expect ( enterEvents . length ) . toBe ( 1 ) ;
96112 expect ( enterEvents [ 0 ] . target ) . toBe ( node ) ;
97113 expect ( enterEvents [ 0 ] . relatedTarget ) . toBe ( iframe . contentWindow ) ;
98114 } ) ;
99115
100116 // Regression test for https://github.com/facebook/react/issues/10906.
101- it ( 'should find the common parent after updates' , ( ) => {
117+ it ( 'should find the common parent after updates' , async ( ) => {
102118 let parentEnterCalls = 0 ;
103119 let childEnterCalls = 0 ;
104120 let parent = null ;
@@ -117,26 +133,32 @@ describe('EnterLeaveEventPlugin', () => {
117133 }
118134 }
119135
120- ReactDOM . render ( < Parent /> , container ) ;
121- // The issue only reproduced on insertion during the first update.
122- ReactDOM . render ( < Parent showChild = { true } /> , container ) ;
136+ const root = ReactDOMClient . createRoot ( container ) ;
137+ await act ( ( ) => {
138+ root . render ( < Parent /> ) ;
139+ } ) ;
140+ await act ( ( ) => {
141+ root . render ( < Parent showChild = { true } /> ) ;
142+ } ) ;
123143
124144 // Enter from parent into the child.
125- parent . dispatchEvent (
126- new MouseEvent ( 'mouseout' , {
127- bubbles : true ,
128- cancelable : true ,
129- relatedTarget : parent . firstChild ,
130- } ) ,
131- ) ;
145+ await act ( ( ) => {
146+ parent . dispatchEvent (
147+ new MouseEvent ( 'mouseout' , {
148+ bubbles : true ,
149+ cancelable : true ,
150+ relatedTarget : parent . firstChild ,
151+ } ) ,
152+ ) ;
153+ } ) ;
132154
133155 // Entering a child should fire on the child, not on the parent.
134156 expect ( childEnterCalls ) . toBe ( 1 ) ;
135157 expect ( parentEnterCalls ) . toBe ( 0 ) ;
136158 } ) ;
137159
138160 // Test for https://github.com/facebook/react/issues/16763.
139- it ( 'should call mouseEnter once from sibling rendered inside a rendered component' , done => {
161+ it ( 'should call mouseEnter once from sibling rendered inside a rendered component in legacy roots ' , done => {
140162 const mockFn = jest . fn ( ) ;
141163
142164 class Parent extends React . Component {
@@ -186,7 +208,7 @@ describe('EnterLeaveEventPlugin', () => {
186208 ReactDOM . render ( < Parent /> , container ) ;
187209 } ) ;
188210
189- it ( 'should call mouseEnter when pressing a non tracked React node' , done => {
211+ it ( 'should call mouseEnter when pressing a non tracked React node in legacy root ' , done => {
190212 const mockFn = jest . fn ( ) ;
191213
192214 class Parent extends React . Component {
@@ -237,7 +259,7 @@ describe('EnterLeaveEventPlugin', () => {
237259 ReactDOM . render ( < Parent /> , container ) ;
238260 } ) ;
239261
240- it ( 'should work with portals outside of the root that has onMouseLeave' , ( ) => {
262+ it ( 'should work with portals outside of the root that has onMouseLeave' , async ( ) => {
241263 const divRef = React . createRef ( ) ;
242264 const onMouseLeave = jest . fn ( ) ;
243265
@@ -249,21 +271,27 @@ describe('EnterLeaveEventPlugin', () => {
249271 ) ;
250272 }
251273
252- ReactDOM . render ( < Component /> , container ) ;
274+ const root = ReactDOMClient . createRoot ( container ) ;
275+
276+ await act ( ( ) => {
277+ root . render ( < Component /> ) ;
278+ } ) ;
253279
254280 // Leave from the portal div
255- divRef . current . dispatchEvent (
256- new MouseEvent ( 'mouseout' , {
257- bubbles : true ,
258- cancelable : true ,
259- relatedTarget : document . body ,
260- } ) ,
261- ) ;
281+ await act ( ( ) => {
282+ divRef . current . dispatchEvent (
283+ new MouseEvent ( 'mouseout' , {
284+ bubbles : true ,
285+ cancelable : true ,
286+ relatedTarget : document . body ,
287+ } ) ,
288+ ) ;
289+ } ) ;
262290
263291 expect ( onMouseLeave ) . toHaveBeenCalledTimes ( 1 ) ;
264292 } ) ;
265293
266- it ( 'should work with portals that have onMouseEnter outside of the root ' , ( ) => {
294+ it ( 'should work with portals that have onMouseEnter outside of the root ' , async ( ) => {
267295 const divRef = React . createRef ( ) ;
268296 const otherDivRef = React . createRef ( ) ;
269297 const onMouseEnter = jest . fn ( ) ;
@@ -279,7 +307,11 @@ describe('EnterLeaveEventPlugin', () => {
279307 ) ;
280308 }
281309
282- ReactDOM . render ( < Component /> , container ) ;
310+ const root = ReactDOMClient . createRoot ( container ) ;
311+
312+ await act ( ( ) => {
313+ root . render ( < Component /> ) ;
314+ } ) ;
283315
284316 // Leave from the portal div
285317 divRef . current . dispatchEvent (
0 commit comments