@@ -9,6 +9,7 @@ import * as SchedulerMock from 'scheduler/unstable_mock';
9
9
import { diff } from 'jest-diff' ;
10
10
import { equals } from '@jest/expect-utils' ;
11
11
import enqueueTask from './enqueueTask' ;
12
+ import simulateBrowserEventDispatch from './simulateBrowserEventDispatch' ;
12
13
13
14
export { act } from './internalAct' ;
14
15
@@ -267,37 +268,37 @@ ${diff(expectedLog, actualLog)}
267
268
268
269
// Simulates dispatching events, waiting for microtasks in between.
269
270
// This matches the browser behavior, which will flush microtasks
270
- // between each event handler.
271
- export async function dispatchAndWaitForDiscrete (
271
+ // between each event handler. This will allow discrete events to
272
+ // flush between events across different event handlers.
273
+ export async function simulateEventDispatch (
272
274
node : Node ,
273
275
eventType : string ,
274
276
) : Promise < void > {
275
- assertYieldsWereCleared ( waitForDiscrete ) ;
276
-
277
- // Bubbling phase
278
- // Walk the path from the element to the document and dispatch
279
- // the event on each node, flushing microtasks in between.
277
+ // Ensure the node is in the document.
280
278
for ( let current = node ; current ; current = current . parentNode ) {
281
- const customEvent = new Event ( eventType , {
282
- bubbles : false ,
283
- cancelable : true ,
284
- } ) ;
285
-
286
- Object . defineProperty ( customEvent , 'eventPhase' , {
287
- // Avoid going through the capture/bubbling phases,
288
- // since we're doing it manually.
289
- value : Event . AT_TARGET ,
290
- } ) ;
291
-
292
- Object . defineProperty ( customEvent , 'target' , {
293
- // Override the target to the node on which we dispatched the event.
294
- value : node ,
295
- } ) ;
296
-
297
- // Dispatch the event on the target
298
- current . dispatchEvent ( customEvent ) ;
299
-
300
- // Flush microtasks
301
- await waitForMicrotasks ( ) ;
279
+ if ( current === document ) {
280
+ break ;
281
+ } else if ( current . parentNode == null ) {
282
+ return ;
283
+ }
284
+ }
285
+
286
+ const customEvent = new Event ( eventType , {
287
+ bubbles : true ,
288
+ } ) ;
289
+
290
+ Object . defineProperty ( customEvent , 'target' , {
291
+ // Override the target to the node on which we dispatched the event.
292
+ value : node ,
293
+ } ) ;
294
+
295
+ const impl = Object . getOwnPropertySymbols ( node ) [ 0 ] ;
296
+ const oldDispatch = node [ impl ] . dispatchEvent ;
297
+ try {
298
+ node [ impl ] . dispatchEvent = simulateBrowserEventDispatch ;
299
+
300
+ await node . dispatchEvent ( customEvent ) ;
301
+ } finally {
302
+ node [ impl ] . dispatchEvent = oldDispatch ;
302
303
}
303
304
}
0 commit comments