@@ -392,6 +392,10 @@ export const KeyboardNavigation: Story = {
392392 return activeOptionId ;
393393 } , { timeout : 1000 } ) ;
394394
395+ // Wait for the component's 100ms initialization delay to complete
396+ // This is crucial for keyboard navigation to work properly
397+ await new Promise ( resolve => setTimeout ( resolve , 150 ) ) ;
398+
395399 // Get the current active option ID after initialization
396400 const firstOptionId = searchInput . getAttribute ( 'aria-activedescendant' ) ;
397401
@@ -411,24 +415,39 @@ export const KeyboardNavigation: Story = {
411415 const listbox = within ( document . body ) . getByRole ( 'listbox' ) ;
412416 const searchInput = within ( listbox ) . getByPlaceholderText ( 'Search...' ) ;
413417
414- // Debug: Log the current state before navigation
415- console . log ( 'Before navigation - activeIndex should be 0' ) ;
418+ // Verify initial state
416419 const initialActiveOptionId = searchInput . getAttribute ( 'aria-activedescendant' ) ;
417420 const initialActiveOption = document . getElementById ( initialActiveOptionId ! ) ;
418- console . log ( 'Initial active option index:' , initialActiveOption ?. getAttribute ( 'data-index' ) ) ;
421+ expect ( initialActiveOption ) . toHaveAttribute ( 'data-index' , '0' ) ;
419422
420423 // Press ArrowDown once to move to the second item
421- fireEvent . keyDown ( searchInput , { key : 'ArrowDown' , code : 'ArrowDown' } ) ;
424+ // Focus the search input first to ensure keyboard events are received
425+ searchInput . focus ( ) ;
426+
427+ fireEvent . keyDown ( searchInput , {
428+ key : 'ArrowDown' ,
429+ code : 'ArrowDown' ,
430+ keyCode : 40 ,
431+ which : 40 ,
432+ bubbles : true ,
433+ cancelable : true
434+ } ) ;
422435
423436 // Wait for first arrow down to take effect
424437 await waitFor ( ( ) => {
425438 const activeOptionId = searchInput . getAttribute ( 'aria-activedescendant' ) ;
426439 const activeOption = document . getElementById ( activeOptionId ! ) ;
427- console . log ( 'After first arrow down - expected index 1, actual:' , activeOption ?. getAttribute ( 'data-index' ) ) ;
428440 expect ( activeOption ) . toHaveAttribute ( 'data-index' , '1' ) ;
429441 } , { timeout : 2000 } ) ;
430442
431- fireEvent . keyDown ( searchInput , { key : 'ArrowDown' , code : 'ArrowDown' } ) ;
443+ fireEvent . keyDown ( searchInput , {
444+ key : 'ArrowDown' ,
445+ code : 'ArrowDown' ,
446+ keyCode : 40 ,
447+ which : 40 ,
448+ bubbles : true ,
449+ cancelable : true
450+ } ) ;
432451
433452 // Wait for the aria-activedescendant to update after second keyboard navigation
434453 await waitFor ( ( ) => {
@@ -450,7 +469,14 @@ export const KeyboardNavigation: Story = {
450469 const searchInput = within ( listbox ) . getByPlaceholderText ( 'Search...' ) ;
451470
452471 // Press Enter to select the active item
453- fireEvent . keyDown ( searchInput , { key : 'Enter' , code : 'Enter' } ) ;
472+ fireEvent . keyDown ( searchInput , {
473+ key : 'Enter' ,
474+ code : 'Enter' ,
475+ keyCode : 13 ,
476+ which : 13 ,
477+ bubbles : true ,
478+ cancelable : true
479+ } ) ;
454480
455481 // Wait for the ShadCN popover to close (Portal cleanup)
456482 await waitFor ( ( ) => {
@@ -492,7 +518,14 @@ export const KeyboardNavigation: Story = {
492518 } , { timeout : 1000 } ) ;
493519
494520 // Press Enter to select the filtered item
495- fireEvent . keyDown ( searchInput , { key : 'Enter' , code : 'Enter' } ) ;
521+ fireEvent . keyDown ( searchInput , {
522+ key : 'Enter' ,
523+ code : 'Enter' ,
524+ keyCode : 13 ,
525+ which : 13 ,
526+ bubbles : true ,
527+ cancelable : true
528+ } ) ;
496529
497530 // Wait for the popover to close and selection to update
498531 await waitFor ( ( ) => {
0 commit comments