@@ -22,6 +22,22 @@ describe('components/table/makeSelectable', () => {
22
22
const getWrapper = ( props = { } ) =>
23
23
shallow ( < SelectableTable onSelect = { sandbox . stub ( ) } data = { data } selectedItems = { [ ] } enableHotkeys { ...props } /> ) ;
24
24
25
+ const testClassNamePreventsArrowNavigation = ( className , hotKey , isGridView = false ) => {
26
+ const wrapper = getWrapper ( {
27
+ gridColumnCount : 3 ,
28
+ isGridView,
29
+ selectedItems : [ 'a' ] ,
30
+ } ) ;
31
+
32
+ jest . spyOn ( document , 'querySelector' ) . mockImplementation ( selector => className === selector ) ;
33
+
34
+ wrapper . setState ( { focusedIndex : undefined } ) ;
35
+ const instance = wrapper . instance ( ) ;
36
+ const shortcut = instance . getHotkeyConfigs ( ) . find ( h => h . get ( 'key' ) === hotKey ) ;
37
+ shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
38
+ expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( undefined ) ;
39
+ } ;
40
+
25
41
afterEach ( ( ) => {
26
42
jest . clearAllTimers ( ) ;
27
43
jest . clearAllMocks ( ) ;
@@ -677,6 +693,7 @@ describe('components/table/makeSelectable', () => {
677
693
describe ( 'ListView specific' , ( ) => {
678
694
describe ( 'down' , ( ) => {
679
695
const hotKey = 'down' ;
696
+
680
697
test ( 'should set focus to first row when no currently focused item' , ( ) => {
681
698
const wrapper = getWrapper ( {
682
699
selectedItems : [ 'a' ] ,
@@ -687,6 +704,7 @@ describe('components/table/makeSelectable', () => {
687
704
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
688
705
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
689
706
} ) ;
707
+
690
708
test ( 'should call event.preventDefault() and set focus to next item' , ( ) => {
691
709
const wrapper = getWrapper ( {
692
710
selectedItems : [ 'a' ] ,
@@ -697,6 +715,7 @@ describe('components/table/makeSelectable', () => {
697
715
shortcut . handler ( { preventDefault : sandbox . mock ( ) } ) ;
698
716
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 1 ) ;
699
717
} ) ;
718
+
700
719
test ( 'should not focus on an index higher than the highest index in the table' , ( ) => {
701
720
const wrapper = getWrapper ( {
702
721
selectedItems : [ 'a' ] ,
@@ -707,9 +726,18 @@ describe('components/table/makeSelectable', () => {
707
726
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
708
727
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 4 ) ;
709
728
} ) ;
729
+
730
+ test . each ( [ [ 'flyout-overlay' ] , [ 'dropdown-menu-element' ] ] ) (
731
+ 'should not set focus if element with class %s is rendered' ,
732
+ className => {
733
+ testClassNamePreventsArrowNavigation ( className , hotKey ) ;
734
+ } ,
735
+ ) ;
710
736
} ) ;
737
+
711
738
describe ( 'up' , ( ) => {
712
739
const hotKey = 'up' ;
740
+
713
741
test ( 'should call event.preventDefault() and call onSelect with new focused item' , ( ) => {
714
742
const wrapper = getWrapper ( {
715
743
selectedItems : [ 'a' ] ,
@@ -720,6 +748,7 @@ describe('components/table/makeSelectable', () => {
720
748
shortcut . handler ( { preventDefault : sandbox . mock ( ) } ) ;
721
749
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
722
750
} ) ;
751
+
723
752
test ( 'should not focus on an index lower than 0' , ( ) => {
724
753
const wrapper = getWrapper ( {
725
754
selectedItems : [ 'a' ] ,
@@ -730,6 +759,13 @@ describe('components/table/makeSelectable', () => {
730
759
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
731
760
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
732
761
} ) ;
762
+
763
+ test . each ( [ [ 'flyout-overlay' ] , [ 'dropdown-menu-element' ] ] ) (
764
+ 'should not set focus if element with class %s is rendered' ,
765
+ className => {
766
+ testClassNamePreventsArrowNavigation ( className , hotKey ) ;
767
+ } ,
768
+ ) ;
733
769
} ) ;
734
770
735
771
describe ( 'shift+down' , ( ) => {
@@ -858,6 +894,7 @@ describe('components/table/makeSelectable', () => {
858
894
859
895
describe ( 'right' , ( ) => {
860
896
const hotKey = 'right' ;
897
+
861
898
test ( 'should set focus to first row when no currently focused item' , ( ) => {
862
899
const wrapper = getWrapper ( {
863
900
gridColumnCount,
@@ -870,6 +907,7 @@ describe('components/table/makeSelectable', () => {
870
907
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
871
908
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
872
909
} ) ;
910
+
873
911
test ( 'should not set focus to first row if target has role of slider' , ( ) => {
874
912
const wrapper = getWrapper ( {
875
913
gridColumnCount,
@@ -895,6 +933,7 @@ describe('components/table/makeSelectable', () => {
895
933
shortcut . handler ( { preventDefault : sandbox . mock ( ) } ) ;
896
934
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 1 ) ;
897
935
} ) ;
936
+
898
937
test ( 'should not focus on an index higher than the highest index in the table' , ( ) => {
899
938
const wrapper = getWrapper ( {
900
939
gridColumnCount,
@@ -907,9 +946,18 @@ describe('components/table/makeSelectable', () => {
907
946
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
908
947
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 4 ) ;
909
948
} ) ;
949
+
950
+ test . each ( [ [ 'flyout-overlay' ] , [ 'dropdown-menu-element' ] ] ) (
951
+ 'should not set focus if element with class %s is rendered' ,
952
+ className => {
953
+ testClassNamePreventsArrowNavigation ( className , hotKey , true ) ;
954
+ } ,
955
+ ) ;
910
956
} ) ;
957
+
911
958
describe ( 'left' , ( ) => {
912
959
const hotKey = 'left' ;
960
+
913
961
test ( 'should not set focus to first row if target has role of slider' , ( ) => {
914
962
const wrapper = getWrapper ( {
915
963
gridColumnCount,
@@ -922,6 +970,7 @@ describe('components/table/makeSelectable', () => {
922
970
shortcut . handler ( { target : { role : 'slider' } } ) ;
923
971
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( undefined ) ;
924
972
} ) ;
973
+
925
974
test ( 'should call event.preventDefault() and call onSelect with new focused item' , ( ) => {
926
975
const wrapper = getWrapper ( {
927
976
gridColumnCount,
@@ -934,6 +983,7 @@ describe('components/table/makeSelectable', () => {
934
983
shortcut . handler ( { preventDefault : sandbox . mock ( ) } ) ;
935
984
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
936
985
} ) ;
986
+
937
987
test ( 'should call event.preventDefault() and set focus to previous item' , ( ) => {
938
988
const wrapper = getWrapper ( {
939
989
gridColumnCount,
@@ -946,6 +996,7 @@ describe('components/table/makeSelectable', () => {
946
996
shortcut . handler ( { preventDefault : sandbox . mock ( ) } ) ;
947
997
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
948
998
} ) ;
999
+
949
1000
test ( 'should not focus on an index lower than 0' , ( ) => {
950
1001
const wrapper = getWrapper ( {
951
1002
gridColumnCount,
@@ -958,9 +1009,17 @@ describe('components/table/makeSelectable', () => {
958
1009
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
959
1010
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
960
1011
} ) ;
1012
+
1013
+ test . each ( [ [ 'flyout-overlay' ] , [ 'dropdown-menu-element' ] ] ) (
1014
+ 'should not set focus if element with class %s is rendered' ,
1015
+ className => {
1016
+ testClassNamePreventsArrowNavigation ( className , hotKey , true ) ;
1017
+ } ,
1018
+ ) ;
961
1019
} ) ;
962
1020
describe ( 'down' , ( ) => {
963
1021
const hotKey = 'down' ;
1022
+
964
1023
test ( 'should set focus to first row when no currently focused item' , ( ) => {
965
1024
const wrapper = getWrapper ( {
966
1025
gridColumnCount,
@@ -973,6 +1032,7 @@ describe('components/table/makeSelectable', () => {
973
1032
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
974
1033
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
975
1034
} ) ;
1035
+
976
1036
test ( 'should not set focus to first row if target has role of slider' , ( ) => {
977
1037
const wrapper = getWrapper ( {
978
1038
gridColumnCount,
@@ -985,6 +1045,7 @@ describe('components/table/makeSelectable', () => {
985
1045
shortcut . handler ( { target : { role : 'slider' } } ) ;
986
1046
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( undefined ) ;
987
1047
} ) ;
1048
+
988
1049
test ( 'should call event.preventDefault() and set focus to next row item' , ( ) => {
989
1050
const wrapper = getWrapper ( {
990
1051
gridColumnCount,
@@ -997,6 +1058,7 @@ describe('components/table/makeSelectable', () => {
997
1058
shortcut . handler ( { preventDefault : sandbox . mock ( ) } ) ;
998
1059
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( gridColumnCount ) ;
999
1060
} ) ;
1061
+
1000
1062
test ( 'should not focus on an index higher than the highest index in the table' , ( ) => {
1001
1063
const wrapper = getWrapper ( {
1002
1064
gridColumnCount,
@@ -1009,9 +1071,18 @@ describe('components/table/makeSelectable', () => {
1009
1071
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
1010
1072
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 4 ) ;
1011
1073
} ) ;
1074
+
1075
+ test . each ( [ [ 'flyout-overlay' ] , [ 'dropdown-menu-element' ] ] ) (
1076
+ 'should not set focus if element with class %s is rendered' ,
1077
+ className => {
1078
+ testClassNamePreventsArrowNavigation ( className , hotKey , true ) ;
1079
+ } ,
1080
+ ) ;
1012
1081
} ) ;
1082
+
1013
1083
describe ( 'up' , ( ) => {
1014
1084
const hotKey = 'up' ;
1085
+
1015
1086
test ( 'should not set focus to first row if target has role of slider' , ( ) => {
1016
1087
const wrapper = getWrapper ( {
1017
1088
gridColumnCount,
@@ -1024,6 +1095,7 @@ describe('components/table/makeSelectable', () => {
1024
1095
shortcut . handler ( { target : { role : 'slider' } } ) ;
1025
1096
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( undefined ) ;
1026
1097
} ) ;
1098
+
1027
1099
test ( 'should call event.preventDefault() and call onSelect with new focused item' , ( ) => {
1028
1100
const wrapper = getWrapper ( {
1029
1101
gridColumnCount,
@@ -1036,6 +1108,7 @@ describe('components/table/makeSelectable', () => {
1036
1108
shortcut . handler ( { preventDefault : sandbox . mock ( ) } ) ;
1037
1109
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
1038
1110
} ) ;
1111
+
1039
1112
test ( 'should call event.preventDefault() and set focus to previous row item' , ( ) => {
1040
1113
const wrapper = getWrapper ( {
1041
1114
gridColumnCount,
@@ -1048,6 +1121,7 @@ describe('components/table/makeSelectable', () => {
1048
1121
shortcut . handler ( { preventDefault : sandbox . mock ( ) } ) ;
1049
1122
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
1050
1123
} ) ;
1124
+
1051
1125
test ( 'should not focus on an index lower than 0' , ( ) => {
1052
1126
const wrapper = getWrapper ( {
1053
1127
gridColumnCount,
@@ -1060,6 +1134,13 @@ describe('components/table/makeSelectable', () => {
1060
1134
shortcut . handler ( { preventDefault : sandbox . stub ( ) } ) ;
1061
1135
expect ( wrapper . state ( 'focusedIndex' ) ) . toEqual ( 0 ) ;
1062
1136
} ) ;
1137
+
1138
+ test . each ( [ [ 'flyout-overlay' ] , [ 'dropdown-menu-element' ] ] ) (
1139
+ 'should not set focus if element with class %s is rendered' ,
1140
+ className => {
1141
+ testClassNamePreventsArrowNavigation ( className , hotKey , true ) ;
1142
+ } ,
1143
+ ) ;
1063
1144
} ) ;
1064
1145
1065
1146
describe ( 'shift+right' , ( ) => {
0 commit comments