@@ -67,7 +67,7 @@ describe('Listbox Pattern', () => {
67
67
const options = signal < TestOption [ ] > ( [ ] ) ;
68
68
const listbox = getListbox ( { ...inputs , items : options } ) ;
69
69
options . set ( getOptions ( listbox , values ) ) ;
70
- return { listbox, options} ;
70
+ return { listbox, options : options ( ) } ;
71
71
}
72
72
73
73
function getDefaultPatterns ( inputs : Partial < TestInputs > = { } ) {
@@ -266,7 +266,7 @@ describe('Listbox Pattern', () => {
266
266
multi : signal ( true ) ,
267
267
} ) ;
268
268
listbox = patterns . listbox ;
269
- options = patterns . options ( ) ;
269
+ options = patterns . options ;
270
270
} ) ;
271
271
272
272
it ( 'should select an option on Space' , ( ) => {
@@ -428,7 +428,7 @@ describe('Listbox Pattern', () => {
428
428
selectionMode : signal ( 'follow' ) ,
429
429
} ) ;
430
430
listbox = patterns . listbox ;
431
- options = patterns . options ( ) ;
431
+ options = patterns . options ;
432
432
} ) ;
433
433
434
434
it ( 'should select an option on navigation' , ( ) => {
@@ -563,9 +563,9 @@ describe('Listbox Pattern', () => {
563
563
} ) ;
564
564
565
565
describe ( 'Pointer Events' , ( ) => {
566
- function click ( options : WritableSignal < TestOption [ ] > , index : number , mods ?: ModifierKeys ) {
566
+ function click ( options : TestOption [ ] , index : number , mods ?: ModifierKeys ) {
567
567
return {
568
- target : options ( ) [ index ] . element ( ) ,
568
+ target : options [ index ] . element ( ) ,
569
569
shiftKey : mods ?. shift ,
570
570
ctrlKey : mods ?. control ,
571
571
} as unknown as PointerEvent ;
@@ -716,7 +716,7 @@ describe('Listbox Pattern', () => {
716
716
skipDisabled : signal ( false ) ,
717
717
selectionMode : signal ( 'follow' ) ,
718
718
} ) ;
719
- options ( ) [ 2 ] . disabled . set ( true ) ;
719
+ options [ 2 ] . disabled . set ( true ) ;
720
720
listbox . onPointerdown ( click ( options , 0 ) ) ;
721
721
expect ( listbox . inputs . value ( ) ) . toEqual ( [ 'Apple' ] ) ;
722
722
@@ -732,7 +732,7 @@ describe('Listbox Pattern', () => {
732
732
skipDisabled : signal ( true ) ,
733
733
selectionMode : signal ( 'follow' ) ,
734
734
} ) ;
735
- options ( ) [ 2 ] . disabled . set ( true ) ;
735
+ options [ 2 ] . disabled . set ( true ) ;
736
736
listbox . onPointerdown ( click ( options , 0 ) ) ;
737
737
expect ( listbox . inputs . value ( ) ) . toEqual ( [ 'Apple' ] ) ;
738
738
listbox . onKeydown ( down ( { control : true } ) ) ;
@@ -785,4 +785,50 @@ describe('Listbox Pattern', () => {
785
785
expect ( listbox . inputs . value ( ) ) . toEqual ( [ 'Apple' , 'Banana' , 'Blackberry' , 'Blueberry' ] ) ;
786
786
} ) ;
787
787
} ) ;
788
+
789
+ describe ( '#setDefaultState' , ( ) => {
790
+ it ( 'should set the active index to the first option' , ( ) => {
791
+ const { listbox} = getDefaultPatterns ( ) ;
792
+ listbox . setDefaultState ( ) ;
793
+ expect ( listbox . inputs . activeIndex ( ) ) . toBe ( 0 ) ;
794
+ } ) ;
795
+
796
+ it ( 'should set the active index to the first focusable option' , ( ) => {
797
+ const { listbox, options} = getDefaultPatterns ( {
798
+ skipDisabled : signal ( true ) ,
799
+ } ) ;
800
+ options [ 0 ] . disabled . set ( true ) ;
801
+ listbox . setDefaultState ( ) ;
802
+ expect ( listbox . inputs . activeIndex ( ) ) . toBe ( 1 ) ;
803
+ } ) ;
804
+
805
+ it ( 'should set the active index to the first selected option' , ( ) => {
806
+ const { listbox} = getDefaultPatterns ( {
807
+ value : signal ( [ 'Banana' ] ) ,
808
+ skipDisabled : signal ( true ) ,
809
+ } ) ;
810
+ listbox . setDefaultState ( ) ;
811
+ expect ( listbox . inputs . activeIndex ( ) ) . toBe ( 2 ) ;
812
+ } ) ;
813
+
814
+ it ( 'should set the active index to the first focusable selected option' , ( ) => {
815
+ const { listbox, options} = getDefaultPatterns ( {
816
+ value : signal ( [ 'Banana' , 'Blackberry' ] ) ,
817
+ skipDisabled : signal ( true ) ,
818
+ } ) ;
819
+ options [ 2 ] . disabled . set ( true ) ;
820
+ listbox . setDefaultState ( ) ;
821
+ expect ( listbox . inputs . activeIndex ( ) ) . toBe ( 3 ) ;
822
+ } ) ;
823
+
824
+ it ( 'should set the active index to the first option if no selected option is focusable' , ( ) => {
825
+ const { listbox, options} = getDefaultPatterns ( {
826
+ value : signal ( [ 'Banana' ] ) ,
827
+ skipDisabled : signal ( true ) ,
828
+ } ) ;
829
+ options [ 2 ] . disabled . set ( true ) ;
830
+ listbox . setDefaultState ( ) ;
831
+ expect ( listbox . inputs . activeIndex ( ) ) . toBe ( 0 ) ;
832
+ } ) ;
833
+ } ) ;
788
834
} ) ;
0 commit comments