@@ -436,47 +436,64 @@ describe('getInputProps', () => {
436
436
} )
437
437
438
438
describe ( 'event handlers' , ( ) => {
439
- test ( 'on change should open the menu and keep the input value' , async ( ) => {
440
- renderCombobox ( )
439
+ describe ( 'on change' , ( ) => {
440
+ test ( 'should open the menu and keep the input value' , async ( ) => {
441
+ renderCombobox ( )
441
442
442
- await changeInputValue ( 'california' )
443
+ await changeInputValue ( 'california' )
443
444
444
- expect ( getItems ( ) ) . toHaveLength ( items . length )
445
- expect ( getInput ( ) ) . toHaveValue ( 'california' )
446
- } )
445
+ expect ( getItems ( ) ) . toHaveLength ( items . length )
446
+ expect ( getInput ( ) ) . toHaveValue ( 'california' )
447
+ } )
447
448
448
- test ( 'on change should remove the highlightedIndex' , async ( ) => {
449
- renderCombobox ( { initialHighlightedIndex : 2 } )
449
+ test ( 'should remove the highlightedIndex' , async ( ) => {
450
+ renderCombobox ( { initialHighlightedIndex : 2 } )
450
451
451
- await changeInputValue ( 'california' )
452
+ await changeInputValue ( 'california' )
452
453
453
- expect ( getInput ( ) ) . toHaveAttribute ( 'aria-activedescendant' , '' )
454
- } )
454
+ expect ( getInput ( ) ) . toHaveAttribute ( 'aria-activedescendant' , '' )
455
+ } )
455
456
456
- test ( 'on change should reset to defaultHighlightedIndex' , async ( ) => {
457
- const defaultHighlightedIndex = 2
458
- renderCombobox ( { defaultHighlightedIndex} )
457
+ test ( 'should reset to defaultHighlightedIndex' , async ( ) => {
458
+ const defaultHighlightedIndex = 2
459
+ renderCombobox ( { defaultHighlightedIndex} )
459
460
460
- await changeInputValue ( 'a' )
461
+ await changeInputValue ( 'a' )
461
462
462
- expect ( getInput ( ) ) . toHaveAttribute (
463
- 'aria-activedescendant' ,
464
- defaultIds . getItemId ( defaultHighlightedIndex ) ,
465
- )
463
+ expect ( getInput ( ) ) . toHaveAttribute (
464
+ 'aria-activedescendant' ,
465
+ defaultIds . getItemId ( defaultHighlightedIndex ) ,
466
+ )
466
467
467
- await keyDownOnInput ( '{ArrowDown}' )
468
+ await keyDownOnInput ( '{ArrowDown}' )
468
469
469
- expect ( getInput ( ) ) . toHaveAttribute (
470
- 'aria-activedescendant' ,
471
- defaultIds . getItemId ( defaultHighlightedIndex + 1 ) ,
472
- )
470
+ expect ( getInput ( ) ) . toHaveAttribute (
471
+ 'aria-activedescendant' ,
472
+ defaultIds . getItemId ( defaultHighlightedIndex + 1 ) ,
473
+ )
473
474
474
- await changeInputValue ( 'a' )
475
+ await changeInputValue ( 'a' )
475
476
476
- expect ( getInput ( ) ) . toHaveAttribute (
477
- 'aria-activedescendant' ,
478
- defaultIds . getItemId ( defaultHighlightedIndex ) ,
479
- )
477
+ expect ( getInput ( ) ) . toHaveAttribute (
478
+ 'aria-activedescendant' ,
479
+ defaultIds . getItemId ( defaultHighlightedIndex ) ,
480
+ )
481
+ } )
482
+
483
+ test ( 'should not reset to defaultHighlightedIndex if disabled' , async ( ) => {
484
+ const defaultHighlightedIndex = 2
485
+ renderCombobox ( {
486
+ defaultHighlightedIndex,
487
+ isItemDisabled ( _item , index ) {
488
+ return index === defaultHighlightedIndex
489
+ } ,
490
+ } )
491
+
492
+ await keyDownOnInput ( '{ArrowDown}' )
493
+ await changeInputValue ( 'a' )
494
+
495
+ expect ( getInput ( ) ) . toHaveAttribute ( 'aria-activedescendant' , '' )
496
+ } )
480
497
} )
481
498
482
499
describe ( 'on key down' , ( ) => {
@@ -579,11 +596,14 @@ describe('getInputProps', () => {
579
596
580
597
test ( 'initialHighlightedIndex is ignored if item is disabled' , async ( ) => {
581
598
const initialHighlightedIndex = 2
599
+ const isItemDisabled = jest
600
+ . fn ( )
601
+ . mockImplementation (
602
+ item => items . indexOf ( item ) === initialHighlightedIndex ,
603
+ )
582
604
renderCombobox ( {
583
605
initialHighlightedIndex,
584
- isItemDisabled ( item ) {
585
- return items . indexOf ( item ) === initialHighlightedIndex
586
- } ,
606
+ isItemDisabled,
587
607
} )
588
608
589
609
await keyDownOnInput ( '{ArrowUp}' )
@@ -592,6 +612,7 @@ describe('getInputProps', () => {
592
612
'aria-activedescendant' ,
593
613
defaultIds . getItemId ( items . length - 1 ) ,
594
614
)
615
+ expect ( isItemDisabled . mock . calls . slice ( 0 , 2 ) ) . toMatchSnapshot ( )
595
616
} )
596
617
597
618
test ( 'initialHighlightedIndex is ignored and defaultHighlightedIndex is chosen if enabled' , async ( ) => {
@@ -615,11 +636,14 @@ describe('getInputProps', () => {
615
636
616
637
test ( 'defaultHighlightedIndex is ignored if item is disabled' , async ( ) => {
617
638
const defaultHighlightedIndex = 2
639
+ const isItemDisabled = jest
640
+ . fn ( )
641
+ . mockImplementation (
642
+ item => items . indexOf ( item ) === defaultHighlightedIndex ,
643
+ )
618
644
renderCombobox ( {
619
645
defaultHighlightedIndex,
620
- isItemDisabled ( item ) {
621
- return items . indexOf ( item ) === defaultHighlightedIndex
622
- } ,
646
+ isItemDisabled,
623
647
} )
624
648
625
649
await keyDownOnInput ( '{ArrowUp}' )
@@ -628,6 +652,7 @@ describe('getInputProps', () => {
628
652
'aria-activedescendant' ,
629
653
defaultIds . getItemId ( items . length - 1 ) ,
630
654
)
655
+ expect ( isItemDisabled . mock . calls . slice ( 0 , 3 ) ) . toMatchSnapshot ( )
631
656
} )
632
657
633
658
test ( 'both defaultHighlightedIndex and initialHighlightedIndex are ignored if items are disabled' , async ( ) => {
@@ -901,11 +926,14 @@ describe('getInputProps', () => {
901
926
902
927
test ( 'initialHighlightedIndex is ignored if item is disabled' , async ( ) => {
903
928
const initialHighlightedIndex = 2
929
+ const isItemDisabled = jest
930
+ . fn ( )
931
+ . mockImplementation (
932
+ item => items . indexOf ( item ) === initialHighlightedIndex ,
933
+ )
904
934
renderCombobox ( {
905
935
initialHighlightedIndex,
906
- isItemDisabled ( item ) {
907
- return items . indexOf ( item ) === initialHighlightedIndex
908
- } ,
936
+ isItemDisabled,
909
937
} )
910
938
911
939
await keyDownOnInput ( '{ArrowDown}' )
@@ -914,6 +942,7 @@ describe('getInputProps', () => {
914
942
'aria-activedescendant' ,
915
943
defaultIds . getItemId ( 0 ) ,
916
944
)
945
+ expect ( isItemDisabled . mock . calls . slice ( 0 , 2 ) ) . toMatchSnapshot ( )
917
946
} )
918
947
919
948
test ( 'initialHighlightedIndex is ignored and defaultHighlightedIndex is chosen if enabled' , async ( ) => {
@@ -937,11 +966,14 @@ describe('getInputProps', () => {
937
966
938
967
test ( 'defaultHighlightedIndex is ignored if item is disabled' , async ( ) => {
939
968
const defaultHighlightedIndex = 2
969
+ const isItemDisabled = jest
970
+ . fn ( )
971
+ . mockImplementation (
972
+ item => items . indexOf ( item ) === defaultHighlightedIndex ,
973
+ )
940
974
renderCombobox ( {
941
975
defaultHighlightedIndex,
942
- isItemDisabled ( item ) {
943
- return items . indexOf ( item ) === defaultHighlightedIndex
944
- } ,
976
+ isItemDisabled,
945
977
} )
946
978
947
979
await keyDownOnInput ( '{ArrowDown}' )
@@ -950,6 +982,7 @@ describe('getInputProps', () => {
950
982
'aria-activedescendant' ,
951
983
defaultIds . getItemId ( 0 ) ,
952
984
)
985
+ expect ( isItemDisabled . mock . calls . slice ( 0 , 3 ) ) . toMatchSnapshot ( )
953
986
} )
954
987
955
988
test ( 'both defaultHighlightedIndex and initialHighlightedIndex are ignored if items are disabled' , async ( ) => {
@@ -1859,7 +1892,7 @@ describe('getInputProps', () => {
1859
1892
renderCombobox ( {
1860
1893
initialIsOpen : true ,
1861
1894
initialHighlightedIndex,
1862
- environment : undefined
1895
+ environment : undefined ,
1863
1896
} )
1864
1897
1865
1898
await tab ( )
@@ -1967,19 +2000,26 @@ describe('getInputProps', () => {
1967
2000
)
1968
2001
} )
1969
2002
1970
-
1971
2003
test ( 'initialHighlightedIndex is ignored if item is disabled' , async ( ) => {
1972
2004
const initialHighlightedIndex = 2
2005
+ const isItemDisabled = jest
2006
+ . fn ( )
2007
+ . mockImplementation (
2008
+ item => items . indexOf ( item ) === initialHighlightedIndex ,
2009
+ )
1973
2010
renderCombobox ( {
1974
2011
initialHighlightedIndex,
1975
- isItemDisabled ( item ) {
1976
- return items . indexOf ( item ) === initialHighlightedIndex
1977
- } ,
2012
+ isItemDisabled,
1978
2013
} )
1979
2014
1980
2015
await clickOnInput ( )
1981
2016
1982
2017
expect ( getInput ( ) ) . toHaveAttribute ( 'aria-activedescendant' , '' )
2018
+ expect ( isItemDisabled ) . toHaveBeenNthCalledWith (
2019
+ 1 ,
2020
+ items [ initialHighlightedIndex ] ,
2021
+ initialHighlightedIndex ,
2022
+ )
1983
2023
} )
1984
2024
1985
2025
test ( 'initialHighlightedIndex is ignored and defaultHighlightedIndex is chosen if enabled' , async ( ) => {
@@ -2003,16 +2043,24 @@ describe('getInputProps', () => {
2003
2043
2004
2044
test ( 'defaultHighlightedIndex is ignored if item is disabled' , async ( ) => {
2005
2045
const defaultHighlightedIndex = 2
2046
+ const isItemDisabled = jest
2047
+ . fn ( )
2048
+ . mockImplementation (
2049
+ item => items . indexOf ( item ) === defaultHighlightedIndex ,
2050
+ )
2006
2051
renderCombobox ( {
2007
2052
defaultHighlightedIndex,
2008
- isItemDisabled ( item ) {
2009
- return items . indexOf ( item ) === defaultHighlightedIndex
2010
- } ,
2053
+ isItemDisabled,
2011
2054
} )
2012
2055
2013
2056
await clickOnInput ( )
2014
2057
2015
2058
expect ( getInput ( ) ) . toHaveAttribute ( 'aria-activedescendant' , '' )
2059
+ expect ( isItemDisabled ) . toHaveBeenNthCalledWith (
2060
+ 1 ,
2061
+ items [ defaultHighlightedIndex ] ,
2062
+ defaultHighlightedIndex ,
2063
+ )
2016
2064
} )
2017
2065
2018
2066
test ( 'both defaultHighlightedIndex and initialHighlightedIndex are ignored if items are disabled' , async ( ) => {
@@ -2032,7 +2080,6 @@ describe('getInputProps', () => {
2032
2080
2033
2081
expect ( getInput ( ) ) . toHaveAttribute ( 'aria-activedescendant' , '' )
2034
2082
} )
2035
-
2036
2083
} )
2037
2084
} )
2038
2085
0 commit comments