@@ -483,4 +483,52 @@ describe('<Combobox>', () => {
483
483
)
484
484
) . toBeInTheDocument ( ) ;
485
485
} ) ;
486
+ it ( 'For filterMethod=includes, when gh is typed matches one of menuList, country filtered menuList should show only strings including gh. Results in 2' , async ( ) => {
487
+ const { container } = render (
488
+ < Combobox menuList = { menuList } filterMethod = "includes" />
489
+ ) ;
490
+
491
+ fireEvent . change (
492
+ container . querySelector ( 'input.form-control.dropdown-toggle' ) ! ,
493
+ { target : { value : 'gh' } }
494
+ ) ;
495
+ expect ( container . querySelector ( 'input' ) ?. value ) . toEqual ( 'gh' ) ;
496
+
497
+ fireEvent . click (
498
+ container . querySelector ( 'input.form-control.dropdown-toggle' ) !
499
+ ) ;
500
+ await waitFor ( ( ) => {
501
+ expect ( container . querySelector ( 'ul.dropdown-menu' ) ) . toBeInTheDocument ( ) ;
502
+ const dropdownItem = container . querySelectorAll ( "li>button.dropdown-item" )
503
+ expect ( dropdownItem . length ) . toEqual ( 2 ) ;
504
+ expect ( dropdownItem [ 0 ] . textContent ) . toEqual ( 'Afghanistan' ) ;
505
+ expect ( dropdownItem [ 1 ] . textContent ) . toEqual ( 'Ghana' ) ;
506
+ } ) ;
507
+ } ) ;
508
+ it ( 'For custom filterMethod, custom filter behaviour is applied instead' , async ( ) => {
509
+ const customFilter = ( inputValue : string , menuItems : string [ ] ) => {
510
+ const filtered = menuItems . filter ( ( n ) => {
511
+ const nLowerCase = n . toLowerCase ( ) ;
512
+ const valueLower = inputValue . toLowerCase ( ) ;
513
+ return nLowerCase . endsWith ( valueLower ) ;
514
+ } ) ;
515
+ return filtered ;
516
+ } ;
517
+ const { container } = render (
518
+ < Combobox menuList = { [ "apple" , "orange" , "banana" ] } filterMethod = { customFilter } />
519
+ ) ;
520
+
521
+ fireEvent . change (
522
+ container . querySelector ( 'input.form-control.dropdown-toggle' ) ! ,
523
+ { target : { value : 'e' } }
524
+ ) ;
525
+ expect ( container . querySelector ( 'input' ) ?. value ) . toEqual ( 'e' ) ;
526
+ await waitFor ( ( ) => {
527
+ expect ( container . querySelector ( 'ul.dropdown-menu' ) ) . toBeInTheDocument ( ) ;
528
+ const dropdownItem = container . querySelectorAll ( "li>button.dropdown-item" )
529
+ expect ( dropdownItem . length ) . toEqual ( 2 ) ;
530
+ expect ( dropdownItem [ 0 ] . textContent ) . toEqual ( 'apple' ) ;
531
+ expect ( dropdownItem [ 1 ] . textContent ) . toEqual ( 'orange' ) ;
532
+ } ) ;
533
+ } ) ;
486
534
} ) ;
0 commit comments