1
+ /* eslint-disable max-classes-per-file */
2
+
1
3
import React from 'react' ;
4
+ import ReactDOM from 'react-dom' ;
2
5
import { mount } from 'enzyme' ;
3
6
import { act } from 'react-dom/test-utils' ;
4
7
import { spyElementPrototypes } from 'rc-util/lib/test/domHook' ;
5
8
import Portal from 'rc-util/lib/Portal' ;
6
9
import Trigger from '../src' ;
7
-
8
- const autoAdjustOverflow = {
9
- adjustX : 1 ,
10
- adjustY : 1 ,
11
- } ;
12
-
13
- const targetOffsetG = [ 0 , 0 ] ;
14
-
15
- export const placementAlignMap = {
16
- left : {
17
- points : [ 'cr' , 'cl' ] ,
18
- overflow : autoAdjustOverflow ,
19
- offset : [ - 3 , 0 ] ,
20
- targetOffsetG,
21
- } ,
22
- right : {
23
- points : [ 'cl' , 'cr' ] ,
24
- overflow : autoAdjustOverflow ,
25
- offset : [ 3 , 0 ] ,
26
- targetOffsetG,
27
- } ,
28
- top : {
29
- points : [ 'bc' , 'tc' ] ,
30
- overflow : autoAdjustOverflow ,
31
- offset : [ 0 , - 3 ] ,
32
- targetOffsetG,
33
- } ,
34
- bottom : {
35
- points : [ 'tc' , 'bc' ] ,
36
- overflow : autoAdjustOverflow ,
37
- offset : [ 0 , 3 ] ,
38
- targetOffsetG,
39
- } ,
40
- topLeft : {
41
- points : [ 'bl' , 'tl' ] ,
42
- overflow : autoAdjustOverflow ,
43
- offset : [ 0 , - 3 ] ,
44
- targetOffsetG,
45
- } ,
46
- topRight : {
47
- points : [ 'br' , 'tr' ] ,
48
- overflow : autoAdjustOverflow ,
49
- offset : [ 0 , - 3 ] ,
50
- targetOffsetG,
51
- } ,
52
- bottomRight : {
53
- points : [ 'tr' , 'br' ] ,
54
- overflow : autoAdjustOverflow ,
55
- offset : [ 0 , 3 ] ,
56
- targetOffsetG,
57
- } ,
58
- bottomLeft : {
59
- points : [ 'tl' , 'bl' ] ,
60
- overflow : autoAdjustOverflow ,
61
- offset : [ 0 , 3 ] ,
62
- targetOffsetG,
63
- } ,
64
- } ;
10
+ import { placementAlignMap } from './util' ;
65
11
66
12
describe ( 'Trigger.Basic' , ( ) => {
67
13
beforeEach ( ( ) => {
@@ -147,12 +93,7 @@ describe('Trigger.Basic', () => {
147
93
) ;
148
94
149
95
wrapper . trigger ( ) ;
150
- expect (
151
- wrapper
152
- . find ( 'Popup' )
153
- . find ( '.x-content' )
154
- . text ( ) ,
155
- ) . toBe ( 'tooltip2' ) ;
96
+ expect ( wrapper . find ( 'Popup' ) . find ( '.x-content' ) . text ( ) ) . toBe ( 'tooltip2' ) ;
156
97
157
98
wrapper . trigger ( ) ;
158
99
expect ( wrapper . isHidden ( ) ) . toBeTruthy ( ) ;
@@ -173,12 +114,7 @@ describe('Trigger.Basic', () => {
173
114
) ;
174
115
175
116
wrapper . trigger ( ) ;
176
- expect (
177
- wrapper
178
- . find ( 'Popup' )
179
- . find ( '.x-content' )
180
- . text ( ) ,
181
- ) . toBe ( 'tooltip3' ) ;
117
+ expect ( wrapper . find ( 'Popup' ) . find ( '.x-content' ) . text ( ) ) . toBe ( 'tooltip3' ) ;
182
118
183
119
wrapper . trigger ( ) ;
184
120
expect ( wrapper . isHidden ( ) ) . toBeTruthy ( ) ;
@@ -509,7 +445,7 @@ describe('Trigger.Basic', () => {
509
445
} ) ;
510
446
511
447
describe ( 'stretch' , ( ) => {
512
- const createTrigger = stretch =>
448
+ const createTrigger = ( stretch ) =>
513
449
mount (
514
450
< Trigger
515
451
action = { [ 'click' ] }
@@ -542,7 +478,7 @@ describe('Trigger.Basic', () => {
542
478
domSpy . mockRestore ( ) ;
543
479
} ) ;
544
480
545
- [ 'width' , 'height' , 'minWidth' , 'minHeight' ] . forEach ( prop => {
481
+ [ 'width' , 'height' , 'minWidth' , 'minHeight' ] . forEach ( ( prop ) => {
546
482
it ( prop , ( ) => {
547
483
const wrapper = createTrigger ( prop ) ;
548
484
@@ -643,7 +579,7 @@ describe('Trigger.Basic', () => {
643
579
< div >
644
580
< button
645
581
type = "button"
646
- onMouseDown = { e => {
582
+ onMouseDown = { ( e ) => {
647
583
e . preventDefault ( ) ;
648
584
e . stopPropagation ( ) ;
649
585
} }
@@ -687,7 +623,7 @@ describe('Trigger.Basic', () => {
687
623
688
624
describe ( 'getContainer' , ( ) => {
689
625
it ( 'not trigger when dom not ready' , ( ) => {
690
- const getPopupContainer = jest . fn ( node => node . parentElement ) ;
626
+ const getPopupContainer = jest . fn ( ( node ) => node . parentElement ) ;
691
627
692
628
function Demo ( ) {
693
629
return (
@@ -740,4 +676,65 @@ describe('Trigger.Basic', () => {
740
676
wrapper . unmount ( ) ;
741
677
} ) ;
742
678
} ) ;
679
+
680
+ // https://github.com/ant-design/ant-design/issues/30116
681
+ it ( 'createPortal should also work with stopPropagation' , ( ) => {
682
+ const root = document . createElement ( 'div' ) ;
683
+ document . body . appendChild ( root ) ;
684
+
685
+ const div = document . createElement ( 'div' ) ;
686
+ document . body . appendChild ( div ) ;
687
+
688
+ const OuterContent = ( { container } ) => {
689
+ return ReactDOM . createPortal (
690
+ < button
691
+ onMouseDown = { ( e ) => {
692
+ e . stopPropagation ( ) ;
693
+ } }
694
+ >
695
+ Stop Pop
696
+ </ button > ,
697
+ container ,
698
+ ) ;
699
+ } ;
700
+
701
+ const Demo = ( ) => {
702
+ return (
703
+ < Trigger
704
+ action = { [ 'click' ] }
705
+ popup = {
706
+ < strong className = "x-content" >
707
+ tooltip2
708
+ < OuterContent container = { div } />
709
+ </ strong >
710
+ }
711
+ >
712
+ < div className = "target" > click</ div >
713
+ </ Trigger >
714
+ ) ;
715
+ } ;
716
+
717
+ const wrapper = mount ( < Demo /> , { attachTo : root } ) ;
718
+
719
+ wrapper . find ( '.target' ) . simulate ( 'click' ) ;
720
+ expect ( wrapper . isHidden ( ) ) . toBeFalsy ( ) ;
721
+
722
+ // Click should not close
723
+ wrapper . find ( 'button' ) . simulate ( 'mouseDown' ) ;
724
+
725
+ // Mock document mouse click event
726
+ act ( ( ) => {
727
+ const mouseEvent = new MouseEvent ( 'mousedown' ) ;
728
+ document . dispatchEvent ( mouseEvent ) ;
729
+ wrapper . update ( ) ;
730
+ } ) ;
731
+
732
+ wrapper . update ( ) ;
733
+ expect ( wrapper . isHidden ( ) ) . toBeFalsy ( ) ;
734
+
735
+ wrapper . unmount ( ) ;
736
+
737
+ document . body . removeChild ( div ) ;
738
+ document . body . removeChild ( root ) ;
739
+ } ) ;
743
740
} ) ;
0 commit comments