@@ -224,6 +224,95 @@ describe('DOMModernPluginEventSystem', () => {
224
224
expect ( log [ 5 ] ) . toEqual ( [ 'bubble' , buttonElement ] ) ;
225
225
} ) ;
226
226
227
+ it ( 'handle propagation of click events between disjointed roots #2' , ( ) => {
228
+ const buttonRef = React . createRef ( ) ;
229
+ const button2Ref = React . createRef ( ) ;
230
+ const divRef = React . createRef ( ) ;
231
+ const spanRef = React . createRef ( ) ;
232
+ const log = [ ] ;
233
+ const onClick = jest . fn ( e => log . push ( [ 'bubble' , e . currentTarget ] ) ) ;
234
+ const onClickCapture = jest . fn ( e =>
235
+ log . push ( [ 'capture' , e . currentTarget ] ) ,
236
+ ) ;
237
+
238
+ function Child ( ) {
239
+ return (
240
+ < div
241
+ ref = { divRef }
242
+ onClick = { onClick }
243
+ onClickCapture = { onClickCapture } >
244
+ Click me!
245
+ </ div >
246
+ ) ;
247
+ }
248
+
249
+ function Parent ( ) {
250
+ return (
251
+ < button
252
+ ref = { button2Ref }
253
+ onClick = { onClick }
254
+ onClickCapture = { onClickCapture }
255
+ />
256
+ ) ;
257
+ }
258
+
259
+ function GrandParent ( ) {
260
+ return (
261
+ < button
262
+ ref = { buttonRef }
263
+ onClick = { onClick }
264
+ onClickCapture = { onClickCapture } >
265
+ < span ref = { spanRef } />
266
+ </ button >
267
+ ) ;
268
+ }
269
+
270
+ // We make a wrapper with an inner container that we
271
+ // render to. So it looks like <div><span></span></div>
272
+ // We then render to all three:
273
+ // - container
274
+ // - parentContainer
275
+ // - childContainer
276
+
277
+ const parentContainer = document . createElement ( 'div' ) ;
278
+ const childContainer = document . createElement ( 'div' ) ;
279
+
280
+ ReactDOM . render ( < GrandParent /> , container ) ;
281
+ ReactDOM . render ( < Parent /> , parentContainer ) ;
282
+ ReactDOM . render ( < Child /> , childContainer ) ;
283
+
284
+ parentContainer . appendChild ( childContainer ) ;
285
+ spanRef . current . appendChild ( parentContainer ) ;
286
+
287
+ // Inside <GrandParent />
288
+ const buttonElement = buttonRef . current ;
289
+ dispatchClickEvent ( buttonElement ) ;
290
+ expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
291
+ expect ( onClickCapture ) . toHaveBeenCalledTimes ( 1 ) ;
292
+ expect ( log [ 0 ] ) . toEqual ( [ 'capture' , buttonElement ] ) ;
293
+ expect ( log [ 1 ] ) . toEqual ( [ 'bubble' , buttonElement ] ) ;
294
+
295
+ // Inside <Child />
296
+ const divElement = divRef . current ;
297
+ dispatchClickEvent ( divElement ) ;
298
+ expect ( onClick ) . toHaveBeenCalledTimes ( 3 ) ;
299
+ expect ( onClickCapture ) . toHaveBeenCalledTimes ( 3 ) ;
300
+ expect ( log [ 2 ] ) . toEqual ( [ 'capture' , divElement ] ) ;
301
+ expect ( log [ 3 ] ) . toEqual ( [ 'bubble' , divElement ] ) ;
302
+ expect ( log [ 4 ] ) . toEqual ( [ 'capture' , buttonElement ] ) ;
303
+ expect ( log [ 5 ] ) . toEqual ( [ 'bubble' , buttonElement ] ) ;
304
+
305
+ // Inside <Parent />
306
+ const buttonElement2 = button2Ref . current ;
307
+ dispatchClickEvent ( buttonElement2 ) ;
308
+ expect ( onClick ) . toHaveBeenCalledTimes ( 5 ) ;
309
+ expect ( onClickCapture ) . toHaveBeenCalledTimes ( 5 ) ;
310
+ expect ( log [ 6 ] ) . toEqual ( [ 'capture' , buttonElement2 ] ) ;
311
+ expect ( log [ 7 ] ) . toEqual ( [ 'bubble' , buttonElement2 ] ) ;
312
+ expect ( log [ 8 ] ) . toEqual ( [ 'capture' , buttonElement ] ) ;
313
+ expect ( log [ 9 ] ) . toEqual ( [ 'bubble' , buttonElement ] ) ;
314
+ } ) ;
315
+
227
316
it ( 'handle propagation of click events between disjointed comment roots' , ( ) => {
228
317
const buttonRef = React . createRef ( ) ;
229
318
const divRef = React . createRef ( ) ;
0 commit comments