@@ -187,4 +187,137 @@ describe('Trigger.Align', () => {
187
187
} ) ;
188
188
} ) ;
189
189
} ) ;
190
+
191
+ describe ( 'flip when scroll' , ( ) => {
192
+ const popupRect = {
193
+ width : 100 ,
194
+ height : 100 ,
195
+ } ;
196
+
197
+ /**
198
+ * 模拟有滚动条时
199
+ * popupRect的x,y值等于popupElement相对与target的位置减去target相对与视口的位置
200
+ * 假设popupElement相对与target的位置x,y均为-1000
201
+ *
202
+ * 重置pupupElement位置 https://github.com/react-component/trigger/blob/e6fa971f97196ea791d0799f25c318c9d8c0ae0f/src/hooks/useAlign.ts#L137-L139
203
+ * 获取popupRect https://github.com/react-component/trigger/blob/e6fa971f97196ea791d0799f25c318c9d8c0ae0f/src/hooks/useAlign.ts#L159
204
+ */
205
+ Object . defineProperty ( popupRect , 'x' , {
206
+ get ( ) {
207
+ return - 1000 - spanRect . x ;
208
+ }
209
+ } ) ;
210
+
211
+ Object . defineProperty ( popupRect , 'y' , {
212
+ get ( ) {
213
+ return - 1000 - spanRect . y ;
214
+ }
215
+ } ) ;
216
+
217
+ beforeAll ( ( ) => {
218
+ spyElementPrototypes ( HTMLDivElement , {
219
+ getBoundingClientRect ( ) {
220
+ return popupRect ;
221
+ }
222
+ } ) ;
223
+ } ) ;
224
+
225
+ describe ( 'not flip if cant' , ( ) => {
226
+ const list = [
227
+ {
228
+ placement : 'right' ,
229
+ x : 10 ,
230
+ className : '.rc-trigger-popup-placement-right' ,
231
+ } ,
232
+ {
233
+ placement : 'left' ,
234
+ x : 90 ,
235
+ className : '.rc-trigger-popup-placement-left' ,
236
+ } ,
237
+ {
238
+ placement : 'top' ,
239
+ y : 90 ,
240
+ className : '.rc-trigger-popup-placement-top' ,
241
+ } ,
242
+ {
243
+ placement : 'bottom' ,
244
+ y : 10 ,
245
+ className : '.rc-trigger-popup-placement-bottom' ,
246
+ } ,
247
+ ] ;
248
+
249
+ list . forEach ( ( { placement, x = 0 , y = 0 , className } ) => {
250
+ it ( placement , async ( ) => {
251
+ spanRect . x = x ;
252
+ spanRect . y = y ;
253
+
254
+ render (
255
+ < Trigger
256
+ popupVisible
257
+ popupPlacement = { placement }
258
+ builtinPlacements = { builtinPlacements }
259
+ popup = { < strong > trigger</ strong > }
260
+ >
261
+ < span className = "target" />
262
+ </ Trigger > ,
263
+ ) ;
264
+
265
+ await act ( async ( ) => {
266
+ await Promise . resolve ( ) ;
267
+ } ) ;
268
+
269
+ expect ( document . querySelector ( className ) ) . toBeTruthy ( ) ;
270
+ } ) ;
271
+ } ) ;
272
+ } ) ;
273
+
274
+ describe ( 'flip if can' , ( ) => {
275
+ const list = [
276
+ {
277
+ placement : 'right' ,
278
+ x : 90 ,
279
+ className : '.rc-trigger-popup-placement-left' ,
280
+ } ,
281
+ {
282
+ placement : 'left' ,
283
+ x : 10 ,
284
+ className : '.rc-trigger-popup-placement-right' ,
285
+ } ,
286
+ {
287
+ placement : 'top' ,
288
+ y : 10 ,
289
+ className : '.rc-trigger-popup-placement-bottom' ,
290
+ } ,
291
+ {
292
+ placement : 'bottom' ,
293
+ y : 90 ,
294
+ className : '.rc-trigger-popup-placement-top' ,
295
+ } ,
296
+ ] ;
297
+
298
+ list . forEach ( ( { placement, x = 0 , y = 0 , className } ) => {
299
+ it ( placement , async ( ) => {
300
+ spanRect . x = x ;
301
+ spanRect . y = y ;
302
+
303
+ render (
304
+ < Trigger
305
+ popupVisible
306
+ popupPlacement = { placement }
307
+ builtinPlacements = { builtinPlacements }
308
+ popup = { < strong > trigger</ strong > }
309
+ >
310
+ < span className = "target" />
311
+ </ Trigger > ,
312
+ ) ;
313
+
314
+ await act ( async ( ) => {
315
+ await Promise . resolve ( ) ;
316
+ } ) ;
317
+
318
+ expect ( document . querySelector ( className ) ) . toBeTruthy ( ) ;
319
+ } ) ;
320
+ } ) ;
321
+ } ) ;
322
+ } ) ;
190
323
} ) ;
0 commit comments