@@ -26,7 +26,7 @@ const TestComponent: React.FC = () => {
2626 onClick = { clickHandlers [ i ] }
2727 href = { i > 1 ? 'https://example.com' : undefined }
2828 >
29- Item { i + 1 }
29+ { i + 1 } Item
3030 </ a >
3131 ) ) }
3232 </ div >
@@ -62,7 +62,7 @@ it('Moves the focus to the first menu item after pressing enter while focused on
6262 skipClick : true ,
6363 } ) ;
6464
65- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
65+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
6666} ) ;
6767
6868it ( 'Moves the focus to the first menu item after pressing space while focused on the menu button' , ( ) => {
@@ -76,7 +76,7 @@ it('Moves the focus to the first menu item after pressing space while focused on
7676 skipClick : true ,
7777 } ) ;
7878
79- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
79+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
8080} ) ;
8181
8282it ( 'Moves the focus to the first menu item after clicking the menu to open it, then pressing tab while focused on the menu button' , ( ) => {
@@ -88,7 +88,7 @@ it('Moves the focus to the first menu item after clicking the menu to open it, t
8888
8989 userEvent . tab ( ) ;
9090
91- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
91+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
9292} ) ;
9393
9494it ( 'Moves the focus to the first menu item after clicking the menu to open it, then pressing arrow down while focused on the menu button' , ( ) => {
@@ -107,7 +107,7 @@ it('Moves the focus to the first menu item after clicking the menu to open it, t
107107 } )
108108 ) ;
109109
110- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
110+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
111111} ) ;
112112
113113it ( 'Sets isOpen to true after pressing enter while focused on the menu button' , ( ) => {
@@ -146,7 +146,7 @@ it('Sets isOpen to false after clicking a menu item that calls the state change
146146 render ( < TestComponent /> ) ;
147147
148148 userEvent . click ( screen . getByText ( 'Primary' ) ) ;
149- userEvent . click ( screen . getByText ( 'Item 2 ' ) ) ;
149+ userEvent . click ( screen . getByText ( '2 Item ' ) ) ;
150150
151151 expect ( screen . getByTestId ( 'is-open-indicator' ) ) . toHaveTextContent ( 'false' ) ;
152152} ) ;
@@ -160,18 +160,18 @@ it('Moves the focus to the next element in the menu after pressing the down arro
160160 skipClick : true ,
161161 } ) ;
162162
163- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
163+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
164164
165165 fireEvent (
166- screen . getByText ( 'Item 1 ' ) ,
166+ screen . getByText ( '1 Item ' ) ,
167167 new KeyboardEvent ( 'keydown' , {
168168 key : 'ArrowDown' ,
169169 bubbles : true ,
170170 cancelable : true ,
171171 } )
172172 ) ;
173173
174- expect ( screen . getByText ( 'Item 2 ' ) ) . toHaveFocus ( ) ;
174+ expect ( screen . getByText ( '2 Item ' ) ) . toHaveFocus ( ) ;
175175} ) ;
176176
177177it ( 'Moves the focus to the previous element in the menu after pressing the up arrow' , ( ) => {
@@ -183,29 +183,29 @@ it('Moves the focus to the previous element in the menu after pressing the up ar
183183 skipClick : true ,
184184 } ) ;
185185
186- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
186+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
187187
188188 fireEvent (
189- screen . getByText ( 'Item 1 ' ) ,
189+ screen . getByText ( '1 Item ' ) ,
190190 new KeyboardEvent ( 'keydown' , {
191191 key : 'ArrowDown' ,
192192 bubbles : true ,
193193 cancelable : true ,
194194 } )
195195 ) ;
196196
197- expect ( screen . getByText ( 'Item 2 ' ) ) . toHaveFocus ( ) ;
197+ expect ( screen . getByText ( '2 Item ' ) ) . toHaveFocus ( ) ;
198198
199199 fireEvent (
200- screen . getByText ( 'Item 2 ' ) ,
200+ screen . getByText ( '2 Item ' ) ,
201201 new KeyboardEvent ( 'keydown' , {
202202 key : 'ArrowUp' ,
203203 bubbles : true ,
204204 cancelable : true ,
205205 } )
206206 ) ;
207207
208- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
208+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
209209} ) ;
210210
211211it ( 'Wraps the focus to the last element when pressing the up arrow at the beginning of the menu' , ( ) => {
@@ -217,18 +217,18 @@ it('Wraps the focus to the last element when pressing the up arrow at the beginn
217217 skipClick : true ,
218218 } ) ;
219219
220- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
220+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
221221
222222 fireEvent (
223- screen . getByText ( 'Item 1 ' ) ,
223+ screen . getByText ( '1 Item ' ) ,
224224 new KeyboardEvent ( 'keydown' , {
225225 key : 'ArrowUp' ,
226226 bubbles : true ,
227227 cancelable : true ,
228228 } )
229229 ) ;
230230
231- expect ( screen . getByText ( 'Item 4 ' ) ) . toHaveFocus ( ) ;
231+ expect ( screen . getByText ( '4 Item ' ) ) . toHaveFocus ( ) ;
232232} ) ;
233233
234234it ( 'Wraps the focus to the first element when pressing the down arrow at the end of the menu' , ( ) => {
@@ -240,29 +240,29 @@ it('Wraps the focus to the first element when pressing the down arrow at the end
240240 skipClick : true ,
241241 } ) ;
242242
243- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
243+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
244244
245245 fireEvent (
246- screen . getByText ( 'Item 1 ' ) ,
246+ screen . getByText ( '1 Item ' ) ,
247247 new KeyboardEvent ( 'keydown' , {
248248 key : 'ArrowUp' ,
249249 bubbles : true ,
250250 cancelable : true ,
251251 } )
252252 ) ;
253253
254- expect ( screen . getByText ( 'Item 4 ' ) ) . toHaveFocus ( ) ;
254+ expect ( screen . getByText ( '4 Item ' ) ) . toHaveFocus ( ) ;
255255
256256 fireEvent (
257- screen . getByText ( 'Item 4 ' ) ,
257+ screen . getByText ( '4 Item ' ) ,
258258 new KeyboardEvent ( 'keydown' , {
259259 key : 'ArrowDown' ,
260260 bubbles : true ,
261261 cancelable : true ,
262262 } )
263263 ) ;
264264
265- expect ( screen . getByText ( 'Item 1 ' ) ) . toHaveFocus ( ) ;
265+ expect ( screen . getByText ( '1 Item ' ) ) . toHaveFocus ( ) ;
266266} ) ;
267267
268268it ( 'Sets isOpen to false after pressing escape while focused on a menu item' , ( ) => {
@@ -274,7 +274,7 @@ it('Sets isOpen to false after pressing escape while focused on a menu item', ()
274274 skipClick : true ,
275275 } ) ;
276276
277- userEvent . type ( screen . getByText ( 'Item 1 ' ) , '{esc}' , {
277+ userEvent . type ( screen . getByText ( '1 Item ' ) , '{esc}' , {
278278 skipClick : true ,
279279 } ) ;
280280
@@ -304,7 +304,7 @@ it('Moves the focus to the menu button after pressing escape while focused on a
304304 skipClick : true ,
305305 } ) ;
306306
307- userEvent . type ( screen . getByText ( 'Item 1 ' ) , '{esc}' , {
307+ userEvent . type ( screen . getByText ( '1 Item ' ) , '{esc}' , {
308308 skipClick : true ,
309309 } ) ;
310310
@@ -335,7 +335,7 @@ it('Adds properties to items added after mount', () => {
335335
336336 userEvent . click ( screen . getByText ( 'Add Item' ) ) ;
337337
338- expect ( screen . getByText ( 'Item 4 ' ) ) . toHaveAttribute ( 'role' , 'menuitem' ) ;
338+ expect ( screen . getByText ( '4 Item ' ) ) . toHaveAttribute ( 'role' , 'menuitem' ) ;
339339} ) ;
340340
341341it ( 'Can navigate to a dynamically-added item' , ( ) => {
@@ -355,7 +355,7 @@ it('Can navigate to a dynamically-added item', () => {
355355 ) ;
356356
357357 fireEvent (
358- screen . getByText ( 'Item 1 ' ) ,
358+ screen . getByText ( '1 Item ' ) ,
359359 new KeyboardEvent ( 'keydown' , {
360360 key : 'ArrowDown' ,
361361 bubbles : true ,
@@ -364,7 +364,7 @@ it('Can navigate to a dynamically-added item', () => {
364364 ) ;
365365
366366 fireEvent (
367- screen . getByText ( 'Item 2 ' ) ,
367+ screen . getByText ( '2 Item ' ) ,
368368 new KeyboardEvent ( 'keydown' , {
369369 key : 'ArrowDown' ,
370370 bubbles : true ,
@@ -373,7 +373,7 @@ it('Can navigate to a dynamically-added item', () => {
373373 ) ;
374374
375375 fireEvent (
376- screen . getByText ( 'Item 3 ' ) ,
376+ screen . getByText ( '3 Item ' ) ,
377377 new KeyboardEvent ( 'keydown' , {
378378 key : 'ArrowDown' ,
379379 bubbles : true ,
@@ -382,15 +382,15 @@ it('Can navigate to a dynamically-added item', () => {
382382 ) ;
383383
384384 fireEvent (
385- screen . getByText ( 'Item 4 ' ) ,
385+ screen . getByText ( '4 Item ' ) ,
386386 new KeyboardEvent ( 'keydown' , {
387387 key : 'ArrowDown' ,
388388 bubbles : true ,
389389 cancelable : true ,
390390 } )
391391 ) ;
392392
393- expect ( screen . getByText ( 'Item 5 ' ) ) . toHaveFocus ( ) ;
393+ expect ( screen . getByText ( '5 Item ' ) ) . toHaveFocus ( ) ;
394394} ) ;
395395
396396it ( 'Ignores keys that buttons don’t need to handle' , ( ) => {
@@ -412,9 +412,11 @@ it('Ignores keys that items don’t need to handle', () => {
412412 skipClick : true ,
413413 } ) ;
414414
415- userEvent . type ( screen . getByText ( 'Item 1 ' ) , 'Z' , {
415+ userEvent . type ( screen . getByText ( '1 Item ' ) , 'Z' , {
416416 skipClick : true ,
417417 } ) ;
418+
419+ expect ( screen . getByText ( '1 Item' ) ) . toHaveFocus ( ) ;
418420} ) ;
419421
420422it ( 'Doesn’t crash when enter press occurs on a menu item' , ( ) => {
@@ -426,7 +428,7 @@ it('Doesn’t crash when enter press occurs on a menu item', () => {
426428 skipClick : true ,
427429 } ) ;
428430
429- userEvent . type ( screen . getByText ( 'Item 1 ' ) , '{enter}' , {
431+ userEvent . type ( screen . getByText ( '1 Item ' ) , '{enter}' , {
430432 skipClick : true ,
431433 } ) ;
432434} ) ;
@@ -440,7 +442,7 @@ it('Closes the menu after pressing enter on a menu item with a click handler', (
440442 skipClick : true ,
441443 } ) ;
442444
443- userEvent . type ( screen . getByText ( 'Item 1 ' ) , '{enter}' , {
445+ userEvent . type ( screen . getByText ( '1 Item ' ) , '{enter}' , {
444446 skipClick : true ,
445447 } ) ;
446448
@@ -458,7 +460,7 @@ it('Activates the click handler of a menu item after pressing enter while focuse
458460 skipClick : true ,
459461 } ) ;
460462
461- userEvent . type ( screen . getByText ( 'Item 1 ' ) , '{enter}' , {
463+ userEvent . type ( screen . getByText ( '1 Item ' ) , '{enter}' , {
462464 skipClick : true ,
463465 } ) ;
464466
@@ -474,7 +476,7 @@ it('Closes the menu after pressing space on a menu item with a click handler', (
474476 skipClick : true ,
475477 } ) ;
476478
477- userEvent . type ( screen . getByText ( 'Item 1 ' ) , '{space}' , {
479+ userEvent . type ( screen . getByText ( '1 Item ' ) , '{space}' , {
478480 skipClick : true ,
479481 } ) ;
480482
@@ -492,9 +494,25 @@ it('Activates the click handler of a menu item after pressing space while focuse
492494 skipClick : true ,
493495 } ) ;
494496
495- userEvent . type ( screen . getByText ( 'Item 1 ' ) , '{space}' , {
497+ userEvent . type ( screen . getByText ( '1 Item ' ) , '{space}' , {
496498 skipClick : true ,
497499 } ) ;
498500
499501 expect ( console . log ) . toHaveBeenCalledWith ( 'Item one clicked' ) ;
500502} ) ;
503+
504+ it ( 'Moves the focus to the menu item with a label that starts with the corresponding character that was pressed' , ( ) => {
505+ render ( < TestComponent /> ) ;
506+
507+ userEvent . tab ( ) ;
508+
509+ userEvent . type ( screen . getByText ( 'Primary' ) , '{enter}' , {
510+ skipClick : true ,
511+ } ) ;
512+
513+ userEvent . type ( screen . getByText ( '1 Item' ) , '3' , {
514+ skipClick : true ,
515+ } ) ;
516+
517+ expect ( screen . getByText ( '3 Item' ) ) . toHaveFocus ( ) ;
518+ } ) ;
0 commit comments