1
+ import { act , fireEvent , render } from '@testing-library/react' ;
1
2
import * as React from 'react' ;
2
- import { render , act , fireEvent } from '@testing-library/react' ;
3
3
import Segmented from '../src' ;
4
4
5
5
jest . mock ( 'rc-motion/lib/util/motion' , ( ) => {
@@ -231,6 +231,12 @@ describe('rc-segmented', () => {
231
231
} ) ;
232
232
233
233
it ( 'render segmented with controlled mode' , ( ) => {
234
+ const offsetParentSpy = jest
235
+ . spyOn ( HTMLElement . prototype , 'offsetParent' , 'get' )
236
+ . mockImplementation ( ( ) => {
237
+ return container ;
238
+ } ) ;
239
+
234
240
const Demo = ( ) => {
235
241
const options = [ 'iOS' , 'Android' , 'Web3' ] ;
236
242
@@ -250,6 +256,7 @@ describe('rc-segmented', () => {
250
256
) ;
251
257
} ;
252
258
const { container } = render ( < Demo /> ) ;
259
+
253
260
fireEvent . click ( container . querySelectorAll ( '.rc-segmented-item-input' ) [ 0 ] ) ;
254
261
expect ( container . querySelector ( '.value' ) ?. textContent ) . toBe ( 'iOS' ) ;
255
262
@@ -284,10 +291,17 @@ describe('rc-segmented', () => {
284
291
285
292
// invalid changes: Should not active any item to make sure it's single source of truth
286
293
expect ( container . querySelector ( '.rc-segmented-item-selected' ) ) . toBeFalsy ( ) ;
294
+
295
+ offsetParentSpy . mockRestore ( ) ;
287
296
} ) ;
288
297
289
298
describe ( 'render segmented with CSSMotion' , ( ) => {
290
299
it ( 'basic' , ( ) => {
300
+ const offsetParentSpy = jest
301
+ . spyOn ( HTMLElement . prototype , 'offsetParent' , 'get' )
302
+ . mockImplementation ( ( ) => {
303
+ return container ;
304
+ } ) ;
291
305
const handleValueChange = jest . fn ( ) ;
292
306
const { container, asFragment } = render (
293
307
< Segmented
@@ -366,9 +380,16 @@ describe('rc-segmented', () => {
366
380
367
381
// thumb should disappear
368
382
expect ( container . querySelector ( '.rc-segmented-thumb' ) ) . toBeFalsy ( ) ;
383
+
384
+ offsetParentSpy . mockRestore ( ) ;
369
385
} ) ;
370
386
371
387
it ( 'quick switch' , ( ) => {
388
+ const offsetParentSpy = jest
389
+ . spyOn ( HTMLElement . prototype , 'offsetParent' , 'get' )
390
+ . mockImplementation ( ( ) => {
391
+ return container ;
392
+ } ) ;
372
393
const { container } = render (
373
394
< Segmented
374
395
options = { [ 'IOS' , 'Android' , 'Web3' ] }
@@ -403,6 +424,31 @@ describe('rc-segmented', () => {
403
424
'--thumb-active-left' : '0px' ,
404
425
'--thumb-active-width' : '62px' ,
405
426
} ) ;
427
+
428
+ offsetParentSpy . mockRestore ( ) ;
429
+ } ) ;
430
+
431
+ it ( 'stop animation early in hidden parent' , ( ) => {
432
+ const offsetParentSpy = jest
433
+ . spyOn ( HTMLElement . prototype , 'offsetParent' , 'get' )
434
+ . mockImplementation ( ( ) => null ) ;
435
+ const Demo = ( ) => {
436
+ const [ value , setValue ] = React . useState < string > ( 'iOS' ) ;
437
+ React . useEffect ( ( ) => setValue ( 'Web3' ) , [ ] ) ;
438
+ return < Segmented options = { [ 'iOS' , 'Android' , 'Web3' ] } value = { value } /> ;
439
+ } ;
440
+
441
+ const { container } = render ( < Demo /> ) ;
442
+
443
+ // stop animation early and place "selected" class
444
+ expect ( container . querySelectorAll ( '.rc-segmented-item' ) [ 2 ] ) . toHaveClass (
445
+ 'rc-segmented-item-selected' ,
446
+ ) ;
447
+
448
+ // thumb should disappear
449
+ expect ( container . querySelector ( '.rc-segmented-thumb' ) ) . toBeFalsy ( ) ;
450
+
451
+ offsetParentSpy . mockRestore ( ) ;
406
452
} ) ;
407
453
} ) ;
408
454
0 commit comments