1
1
import { act , cleanup , fireEvent , render } from '@testing-library/react' ;
2
2
import { spyElementPrototypes } from 'rc-util/lib/test/domHook' ;
3
3
import React from 'react' ;
4
- import type { TriggerProps } from '../src' ;
4
+ import type { TriggerProps , TriggerRef } from '../src' ;
5
5
import Trigger from '../src' ;
6
6
import { awaitFakeTimer } from './util' ;
7
7
@@ -14,6 +14,8 @@ export const triggerResize = (target: Element) => {
14
14
} ;
15
15
16
16
describe ( 'Trigger.Align' , ( ) => {
17
+ let targetVisible = true ;
18
+
17
19
beforeAll ( ( ) => {
18
20
spyElementPrototypes ( HTMLDivElement , {
19
21
getBoundingClientRect : ( ) => ( {
@@ -23,9 +25,16 @@ describe('Trigger.Align', () => {
23
25
height : 100 ,
24
26
} ) ,
25
27
} ) ;
28
+
29
+ spyElementPrototypes ( HTMLElement , {
30
+ offsetParent : {
31
+ get : ( ) => ( targetVisible ? document . body : null ) ,
32
+ } ,
33
+ } ) ;
26
34
} ) ;
27
35
28
36
beforeEach ( ( ) => {
37
+ targetVisible = true ;
29
38
jest . useFakeTimers ( ) ;
30
39
} ) ;
31
40
@@ -130,4 +139,41 @@ describe('Trigger.Align', () => {
130
139
document . querySelector ( '.rc-trigger-popup-placement-top' ) ,
131
140
) . toBeTruthy ( ) ;
132
141
} ) ;
142
+
143
+ it ( 'invisible should not align' , async ( ) => {
144
+ const onPopupAlign = jest . fn ( ) ;
145
+ const triggerRef = React . createRef < TriggerRef > ( ) ;
146
+
147
+ render (
148
+ < Trigger
149
+ popupVisible
150
+ popup = { < span className = "bamboo" /> }
151
+ popupAlign = { { } }
152
+ onPopupAlign = { onPopupAlign }
153
+ ref = { triggerRef }
154
+ >
155
+ < span />
156
+ </ Trigger > ,
157
+ ) ;
158
+
159
+ await awaitFakeTimer ( ) ;
160
+
161
+ expect ( onPopupAlign ) . toHaveBeenCalled ( ) ;
162
+ onPopupAlign . mockReset ( ) ;
163
+
164
+ for ( let i = 0 ; i < 10 ; i += 1 ) {
165
+ triggerRef . current ! . forceAlign ( ) ;
166
+
167
+ await awaitFakeTimer ( ) ;
168
+ expect ( onPopupAlign ) . toHaveBeenCalled ( ) ;
169
+ onPopupAlign . mockReset ( ) ;
170
+ }
171
+
172
+ // Make invisible
173
+ targetVisible = false ;
174
+
175
+ triggerRef . current ! . forceAlign ( ) ;
176
+ await awaitFakeTimer ( ) ;
177
+ expect ( onPopupAlign ) . not . toHaveBeenCalled ( ) ;
178
+ } ) ;
133
179
} ) ;
0 commit comments