@@ -24,7 +24,7 @@ describe('OptionList', () => {
24
24
} ) ;
25
25
26
26
function generateList ( { options, values, ref, ...props } : any ) {
27
- const fieldNames = fillFieldNames ( { } , false ) ;
27
+ const fieldNames = fillFieldNames ( props . fieldNames || { } , false ) ;
28
28
const flattenedOptions = flattenOptions ( options , {
29
29
fieldNames,
30
30
childrenAsData : false ,
@@ -121,6 +121,56 @@ describe('OptionList', () => {
121
121
) ;
122
122
} ) ;
123
123
124
+ it ( 'key operation with fieldNames' , ( ) => {
125
+ const onActiveValue = jest . fn ( ) ;
126
+ const toggleOpen = jest . fn ( ) ;
127
+ const onSelect = jest . fn ( ) ;
128
+ const listRef = React . createRef < RefOptionListProps > ( ) ;
129
+ mount (
130
+ generateList ( {
131
+ fieldNames : { value : 'foo' , label : 'bar' } ,
132
+ options : [ { foo : '1' } , { foo : '2' } ] ,
133
+ onActiveValue,
134
+ onSelect,
135
+ toggleOpen,
136
+ ref : listRef ,
137
+ } ) ,
138
+ ) ;
139
+
140
+ onActiveValue . mockReset ( ) ;
141
+ act ( ( ) => {
142
+ listRef . current . onKeyDown ( { which : KeyCode . DOWN } as any ) ;
143
+ } ) ;
144
+ expect ( onActiveValue ) . toHaveBeenCalledWith (
145
+ '2' ,
146
+ expect . anything ( ) ,
147
+ expect . objectContaining ( { source : 'keyboard' } ) ,
148
+ ) ;
149
+
150
+ act ( ( ) => {
151
+ listRef . current . onKeyDown ( { which : KeyCode . ENTER } as any ) ;
152
+ } ) ;
153
+ expect ( onSelect ) . toHaveBeenCalledTimes ( 1 ) ;
154
+ expect ( onSelect ) . toHaveBeenCalledWith ( '2' , expect . objectContaining ( { selected : true } ) ) ;
155
+
156
+ onSelect . mockReset ( ) ;
157
+ onActiveValue . mockReset ( ) ;
158
+ act ( ( ) => {
159
+ listRef . current . onKeyDown ( { which : KeyCode . UP } as any ) ;
160
+ } ) ;
161
+ expect ( onActiveValue ) . toHaveBeenCalledWith (
162
+ '1' ,
163
+ expect . anything ( ) ,
164
+ expect . objectContaining ( { source : 'keyboard' } ) ,
165
+ ) ;
166
+
167
+ act ( ( ) => {
168
+ listRef . current . onKeyDown ( { which : KeyCode . ENTER } as any ) ;
169
+ } ) ;
170
+ expect ( onSelect ) . toHaveBeenCalledTimes ( 1 ) ;
171
+ expect ( onSelect ) . toHaveBeenCalledWith ( '1' , expect . objectContaining ( { selected : true } ) ) ;
172
+ } ) ;
173
+
124
174
// mocked how we detect running platform in test environment
125
175
it ( 'special key operation on Mac' , ( ) => {
126
176
const onActiveValue = jest . fn ( ) ;
0 commit comments