@@ -654,7 +654,7 @@ describe("MultiSelect", () => {
654654
655655 // Regression test for https://github.com/carbon-design-system/carbon-components-svelte/issues/2313
656656 describe ( "keyboard navigation (issue #2313)" , ( ) => {
657- it ( "filterable: menu opens when starting to type after Tab focus " , async ( ) => {
657+ it ( "filterable: menu opens when tabbing into field " , async ( ) => {
658658 render ( MultiSelect , {
659659 props : {
660660 items,
@@ -665,19 +665,14 @@ describe("MultiSelect", () => {
665665
666666 const input = screen . getByPlaceholderText ( "Filter..." ) ;
667667
668- // Simulate tabbing into the field
669- input . focus ( ) ;
670-
671- // Menu doesn't need to open immediately on focus for filterable variant
672- // but should open when user starts typing
673- await user . type ( input , "s" ) ;
668+ await user . tab ( ) ;
669+ expect ( input ) . toHaveFocus ( ) ;
674670
675- // Menu should now be open
676671 expect ( input ) . toHaveAttribute ( "aria-expanded" , "true" ) ;
677672 expect ( screen . getByRole ( "listbox" ) ) . toBeInTheDocument ( ) ;
678673 } ) ;
679674
680- it ( "filterable: accepts keyboard input after tabbing into field " , async ( ) => {
675+ it ( "filterable: first character shows correctly after tabbing" , async ( ) => {
681676 render ( MultiSelect , {
682677 props : {
683678 items,
@@ -688,14 +683,15 @@ describe("MultiSelect", () => {
688683
689684 const input = screen . getByPlaceholderText ( "Filter..." ) ;
690685
691- // Simulate tabbing into the field
692- input . focus ( ) ;
686+ await user . tab ( ) ;
687+ expect ( input ) . toHaveFocus ( ) ;
693688
694- // Should be able to type immediately
695- await user . type ( input , "slack" ) ;
689+ await user . type ( input , "s" ) ;
690+ expect ( input ) . toHaveValue ( "s" ) ;
691+
692+ await user . type ( input , "lack" ) ;
696693 expect ( input ) . toHaveValue ( "slack" ) ;
697694
698- // Filtered results should be shown
699695 expect ( screen . getByText ( "Slack" ) ) . toBeInTheDocument ( ) ;
700696 expect ( screen . queryByText ( "Email" ) ) . not . toBeInTheDocument ( ) ;
701697 } ) ;
@@ -711,18 +707,13 @@ describe("MultiSelect", () => {
711707
712708 const input = screen . getByPlaceholderText ( "Filter..." ) ;
713709 await user . click ( input ) ;
714-
715- // Menu should be open
716710 expect ( input ) . toHaveAttribute ( "aria-expanded" , "true" ) ;
717711
718- // Press Tab - menu should close to allow natural tab navigation
719712 await user . keyboard ( "{Tab}" ) ;
720-
721- // Menu should close when Tab is pressed to move focus away
722713 expect ( input ) . toHaveAttribute ( "aria-expanded" , "false" ) ;
723714 } ) ;
724715
725- it ( "filterable: focus should go to input, not clear button when items selected " , async ( ) => {
716+ it ( "filterable: focus goes to input, not clear button" , async ( ) => {
726717 render ( MultiSelect , {
727718 props : {
728719 items,
@@ -733,15 +724,36 @@ describe("MultiSelect", () => {
733724 } ) ;
734725
735726 const input = screen . getByPlaceholderText ( "Filter..." ) ;
727+ const clearButton = screen . getAllByRole ( "button" , { name : / c l e a r / i } ) [ 0 ] ;
736728
737- // Simulate tabbing into the field
738- input . focus ( ) ;
729+ expect ( clearButton ) . toHaveAttribute ( "tabindex" , "-1" ) ;
730+ await user . tab ( ) ;
739731
740- // Input should have focus, not the clear button
741732 expect ( input ) . toHaveFocus ( ) ;
733+ expect ( clearButton ) . not . toHaveFocus ( ) ;
734+ expect ( input ) . toHaveAttribute ( "aria-expanded" , "true" ) ;
735+ } ) ;
742736
737+ it ( "filterable: clear button is not keyboard accessible but works with mouse" , async ( ) => {
738+ render ( MultiSelect , {
739+ props : {
740+ items,
741+ filterable : true ,
742+ placeholder : "Filter..." ,
743+ selectedIds : [ "0" , "1" ] ,
744+ } ,
745+ } ) ;
746+
747+ const input = screen . getByPlaceholderText ( "Filter..." ) ;
743748 const clearButton = screen . getAllByRole ( "button" , { name : / c l e a r / i } ) [ 0 ] ;
744- expect ( clearButton ) . not . toHaveFocus ( ) ;
749+
750+ expect ( clearButton ) . toHaveAttribute ( "tabindex" , "-1" ) ;
751+ await user . click ( clearButton ) ;
752+
753+ await user . click ( input ) ;
754+ const options = screen . getAllByRole ( "option" ) ;
755+ expect ( options [ 0 ] ) . toHaveAttribute ( "aria-selected" , "false" ) ;
756+ expect ( options [ 1 ] ) . toHaveAttribute ( "aria-selected" , "false" ) ;
745757 } ) ;
746758 } ) ;
747759} ) ;
0 commit comments