11import { act , cleanup , fireEvent , render } from '@testing-library/react' ;
22import { spyElementPrototypes } from 'rc-util/lib/test/domHook' ;
33import React from 'react' ;
4- import type { TriggerProps } from '../src' ;
4+ import type { TriggerProps , TriggerRef } from '../src' ;
55import Trigger from '../src' ;
66import { awaitFakeTimer } from './util' ;
77
@@ -14,6 +14,8 @@ export const triggerResize = (target: Element) => {
1414} ;
1515
1616describe ( 'Trigger.Align' , ( ) => {
17+ let targetVisible = true ;
18+
1719 beforeAll ( ( ) => {
1820 spyElementPrototypes ( HTMLDivElement , {
1921 getBoundingClientRect : ( ) => ( {
@@ -23,9 +25,16 @@ describe('Trigger.Align', () => {
2325 height : 100 ,
2426 } ) ,
2527 } ) ;
28+
29+ spyElementPrototypes ( HTMLElement , {
30+ offsetParent : {
31+ get : ( ) => ( targetVisible ? document . body : null ) ,
32+ } ,
33+ } ) ;
2634 } ) ;
2735
2836 beforeEach ( ( ) => {
37+ targetVisible = true ;
2938 jest . useFakeTimers ( ) ;
3039 } ) ;
3140
@@ -130,4 +139,41 @@ describe('Trigger.Align', () => {
130139 document . querySelector ( '.rc-trigger-popup-placement-top' ) ,
131140 ) . toBeTruthy ( ) ;
132141 } ) ;
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+ } ) ;
133179} ) ;
0 commit comments