@@ -590,6 +590,67 @@ describe("MultiSelect", () => {
590590 expect ( nthRenderedOptionText ( 0 ) ) . toBe ( "A" ) ;
591591 } ) ;
592592
593+ it ( "does not double re-sort when toggling with selectionFeedback: top" , async ( ) => {
594+ let sortCallCount = 0 ;
595+
596+ // Wrap sortItem to count how many comparisons occur per toggle.
597+ const sortItem = ( a : { text : string } , b : { text : string } ) => {
598+ sortCallCount ++ ;
599+ return a . text . localeCompare ( b . text ) ;
600+ } ;
601+
602+ render ( MultiSelect , {
603+ props : {
604+ items : [
605+ { id : "1" , text : "A" } ,
606+ { id : "2" , text : "B" } ,
607+ { id : "3" , text : "C" } ,
608+ { id : "4" , text : "D" } ,
609+ { id : "5" , text : "E" } ,
610+ ] ,
611+ selectionFeedback : "top" ,
612+ sortItem,
613+ } ,
614+ } ) ;
615+
616+ await openMenu ( ) ;
617+ sortCallCount = 0 ;
618+
619+ // Toggle two items so both checked and unchecked partitions get sorted.
620+ await toggleOption ( "C" ) ;
621+ await toggleOption ( "E" ) ;
622+ const callsForTwoToggles = sortCallCount ;
623+
624+ // With 5 items and 2 checked, a single sort() call produces
625+ // at most ~10 comparisons (two small partitions).
626+ // A double re-sort per toggle would roughly double this count.
627+ // Use a generous upper bound for a single-pass sort per toggle.
628+ expect ( callsForTwoToggles ) . toBeLessThanOrEqual ( 20 ) ;
629+ expect ( callsForTwoToggles ) . toBeGreaterThan ( 0 ) ;
630+ } ) ;
631+
632+ it ( "re-sorts when selectedIds changes externally with selectionFeedback: top" , async ( ) => {
633+ const { rerender } = render ( MultiSelect , {
634+ props : {
635+ items : [
636+ { id : "3" , text : "C" } ,
637+ { id : "1" , text : "A" } ,
638+ { id : "2" , text : "B" } ,
639+ ] ,
640+ selectionFeedback : "top" ,
641+ } ,
642+ } ) ;
643+
644+ await openMenu ( ) ;
645+ expect ( nthRenderedOptionText ( 0 ) ) . toBe ( "A" ) ;
646+
647+ // External selectedIds change should still trigger re-sort.
648+ await rerender ( { selectedIds : [ "3" ] } ) ;
649+ await tick ( ) ;
650+
651+ expect ( nthRenderedOptionText ( 0 ) ) . toBe ( "C" ) ;
652+ } ) ;
653+
593654 it ( "sorts after reopen with selectionFeedback: top-after-reopen" , async ( ) => {
594655 render ( MultiSelect , {
595656 props : {
0 commit comments