@@ -6,7 +6,9 @@ import OptionList from '../src/OptionList';
6
6
import SelectContext from '../src/SelectContext' ;
7
7
import { fillFieldNames , flattenOptions } from '../src/utils/valueUtil' ;
8
8
import { injectRunAllTimers } from './utils/common' ;
9
- import { createEvent , fireEvent , render } from '@testing-library/react' ;
9
+ import { createEvent , fireEvent , render , waitFor } from '@testing-library/react' ;
10
+ import Select from '../src' ;
11
+ import { spyElementPrototypes } from 'rc-util/lib/test/domHook' ;
10
12
11
13
jest . mock ( '../src/utils/platformUtil' ) ;
12
14
@@ -459,4 +461,74 @@ describe('OptionList', () => {
459
461
} ) ;
460
462
expect ( onActiveValue ) . not . toHaveBeenCalledWith ( '3' , expect . anything ( ) , expect . anything ( ) ) ;
461
463
} ) ;
464
+
465
+ describe ( 'List.ScrollBar' , ( ) => {
466
+ let mockElement ;
467
+ let boundingRect = {
468
+ top : 0 ,
469
+ bottom : 0 ,
470
+ width : 100 ,
471
+ height : 50 ,
472
+ } ;
473
+
474
+ beforeAll ( ( ) => {
475
+ mockElement = spyElementPrototypes ( HTMLElement , {
476
+ offsetHeight : {
477
+ get : ( ) => 100 ,
478
+ } ,
479
+ clientHeight : {
480
+ get : ( ) => 50 ,
481
+ } ,
482
+ getBoundingClientRect : ( ) => boundingRect ,
483
+ offsetParent : {
484
+ get : ( ) => document . body ,
485
+ } ,
486
+ } ) ;
487
+ } ) ;
488
+
489
+ afterAll ( ( ) => {
490
+ mockElement . mockRestore ( ) ;
491
+ } ) ;
492
+
493
+ beforeEach ( ( ) => {
494
+ boundingRect = {
495
+ top : 0 ,
496
+ bottom : 0 ,
497
+ width : 100 ,
498
+ height : 50 ,
499
+ } ;
500
+ jest . useFakeTimers ( ) ;
501
+ } ) ;
502
+
503
+ afterEach ( ( ) => {
504
+ jest . useRealTimers ( ) ;
505
+ } ) ;
506
+
507
+ it ( 'should show scrollbar when showScrollBar is true' , async ( ) => {
508
+ const options = Array . from ( { length : 10 } , ( _ , index ) => ( {
509
+ label : `${ index + 1 } ` ,
510
+ value : `${ index + 1 } ` ,
511
+ } ) ) ;
512
+
513
+ const { container } = render ( < Select open showScrollBar options = { options } /> ) ;
514
+
515
+ await waitFor ( ( ) => {
516
+ const scrollbarElement = container . querySelector ( '.rc-virtual-list-scrollbar-visible' ) ;
517
+ expect ( scrollbarElement ) . not . toBeNull ( ) ;
518
+ } ) ;
519
+ } ) ;
520
+ it ( 'not have scrollbar when showScrollBar is false' , async ( ) => {
521
+ const options = Array . from ( { length : 10 } , ( _ , index ) => ( {
522
+ label : `${ index + 1 } ` ,
523
+ value : `${ index + 1 } ` ,
524
+ } ) ) ;
525
+
526
+ const { container } = render ( < Select open showScrollBar = { false } options = { options } /> ) ;
527
+
528
+ await waitFor ( ( ) => {
529
+ const scrollbarElement = container . querySelector ( '.rc-virtual-list-scrollbar-visible' ) ;
530
+ expect ( scrollbarElement ) . toBeNull ( ) ;
531
+ } ) ;
532
+ } ) ;
533
+ } ) ;
462
534
} ) ;
0 commit comments