@@ -16,6 +16,8 @@ import {
16
16
} from '@angular/cdk/testing/private' ;
17
17
import { A , DOWN_ARROW , END , HOME , SPACE } from '@angular/cdk/keycodes' ;
18
18
import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
19
+ import { CdkCombobox , CdkComboboxModule } from '@angular/cdk-experimental/combobox' ;
20
+
19
21
20
22
describe ( 'CdkOption and CdkListbox' , ( ) => {
21
23
@@ -657,6 +659,11 @@ describe('CdkOption and CdkListbox', () => {
657
659
listboxInstance . writeValue ( [ 'arc' , 'stasis' ] ) ;
658
660
fixture . detectChanges ( ) ;
659
661
662
+ const selectedValues = listboxInstance . getSelectedValues ( ) ;
663
+ expect ( selectedValues . length ) . toBe ( 2 ) ;
664
+ expect ( selectedValues [ 0 ] ) . toBe ( 'arc' ) ;
665
+ expect ( selectedValues [ 1 ] ) . toBe ( 'stasis' ) ;
666
+
660
667
expect ( optionElements [ 0 ] . hasAttribute ( 'aria-selected' ) ) . toBeFalse ( ) ;
661
668
expect ( optionElements [ 1 ] . hasAttribute ( 'aria-selected' ) ) . toBeFalse ( ) ;
662
669
@@ -762,6 +769,118 @@ describe('CdkOption and CdkListbox', () => {
762
769
expect ( testComponent . form . value ) . toEqual ( [ 'solar' ] ) ;
763
770
} ) ;
764
771
} ) ;
772
+
773
+ describe ( 'inside a combobox' , ( ) => {
774
+ let fixture : ComponentFixture < ListboxInsideCombobox > ;
775
+ let testComponent : ListboxInsideCombobox ;
776
+
777
+ let listbox : DebugElement ;
778
+ let listboxInstance : CdkListbox < unknown > ;
779
+ let listboxElement : HTMLElement ;
780
+
781
+ let combobox : DebugElement ;
782
+ let comboboxInstance : CdkCombobox < string > ;
783
+ let comboboxElement : HTMLElement ;
784
+
785
+ let options : DebugElement [ ] ;
786
+ let optionInstances : CdkOption [ ] ;
787
+ let optionElements : HTMLElement [ ] ;
788
+
789
+ beforeEach ( async ( ( ) => {
790
+ TestBed . configureTestingModule ( {
791
+ imports : [ CdkListboxModule , CdkComboboxModule ] ,
792
+ declarations : [ ListboxInsideCombobox ] ,
793
+ } ) . compileComponents ( ) ;
794
+ } ) ) ;
795
+
796
+ beforeEach ( ( ) => {
797
+ fixture = TestBed . createComponent ( ListboxInsideCombobox ) ;
798
+ fixture . detectChanges ( ) ;
799
+
800
+ testComponent = fixture . debugElement . componentInstance ;
801
+
802
+ combobox = fixture . debugElement . query ( By . directive ( CdkCombobox ) ) ;
803
+ comboboxInstance = combobox . injector . get < CdkCombobox < string > > ( CdkCombobox ) ;
804
+ comboboxElement = combobox . nativeElement ;
805
+
806
+ } ) ;
807
+
808
+ it ( 'should update combobox value on selection of an option' , ( ) => {
809
+ expect ( comboboxInstance . value ) . toBeUndefined ( ) ;
810
+ expect ( comboboxInstance . isOpen ( ) ) . toBeFalse ( ) ;
811
+
812
+ dispatchMouseEvent ( comboboxElement , 'click' ) ;
813
+ fixture . detectChanges ( ) ;
814
+
815
+ listbox = fixture . debugElement . query ( By . directive ( CdkListbox ) ) ;
816
+ listboxInstance = listbox . injector . get < CdkListbox < unknown > > ( CdkListbox ) ;
817
+
818
+ options = fixture . debugElement . queryAll ( By . directive ( CdkOption ) ) ;
819
+ optionInstances = options . map ( o => o . injector . get < CdkOption > ( CdkOption ) ) ;
820
+ optionElements = options . map ( o => o . nativeElement ) ;
821
+
822
+ expect ( comboboxInstance . isOpen ( ) ) . toBeTrue ( ) ;
823
+
824
+ dispatchMouseEvent ( optionElements [ 0 ] , 'click' ) ;
825
+ fixture . detectChanges ( ) ;
826
+
827
+ expect ( comboboxInstance . isOpen ( ) ) . toBeFalse ( ) ;
828
+ expect ( comboboxInstance . value ) . toBe ( 'purple' ) ;
829
+ } ) ;
830
+
831
+ it ( 'should update combobox value on selection via keyboard' , ( ) => {
832
+ expect ( comboboxInstance . value ) . toBeUndefined ( ) ;
833
+ expect ( comboboxInstance . isOpen ( ) ) . toBeFalse ( ) ;
834
+
835
+ dispatchMouseEvent ( comboboxElement , 'click' ) ;
836
+ fixture . detectChanges ( ) ;
837
+
838
+ listbox = fixture . debugElement . query ( By . directive ( CdkListbox ) ) ;
839
+ listboxInstance = listbox . injector . get < CdkListbox < unknown > > ( CdkListbox ) ;
840
+ listboxElement = listbox . nativeElement ;
841
+
842
+ options = fixture . debugElement . queryAll ( By . directive ( CdkOption ) ) ;
843
+ optionInstances = options . map ( o => o . injector . get < CdkOption > ( CdkOption ) ) ;
844
+ optionElements = options . map ( o => o . nativeElement ) ;
845
+
846
+ expect ( comboboxInstance . isOpen ( ) ) . toBeTrue ( ) ;
847
+
848
+ listboxInstance . setActiveOption ( optionInstances [ 1 ] ) ;
849
+ dispatchKeyboardEvent ( listboxElement , 'keydown' , SPACE ) ;
850
+ fixture . detectChanges ( ) ;
851
+
852
+ expect ( comboboxInstance . isOpen ( ) ) . toBeFalse ( ) ;
853
+ expect ( comboboxInstance . value ) . toBe ( 'solar' ) ;
854
+ } ) ;
855
+
856
+ it ( 'should not update combobox if listbox is in multiple mode' , ( ) => {
857
+ expect ( comboboxInstance . value ) . toBeUndefined ( ) ;
858
+ expect ( comboboxInstance . isOpen ( ) ) . toBeFalse ( ) ;
859
+
860
+ dispatchMouseEvent ( comboboxElement , 'click' ) ;
861
+ fixture . detectChanges ( ) ;
862
+
863
+ listbox = fixture . debugElement . query ( By . directive ( CdkListbox ) ) ;
864
+ listboxInstance = listbox . injector . get < CdkListbox < unknown > > ( CdkListbox ) ;
865
+ listboxElement = listbox . nativeElement ;
866
+
867
+ testComponent . isMultiselectable = true ;
868
+ fixture . detectChanges ( ) ;
869
+
870
+ options = fixture . debugElement . queryAll ( By . directive ( CdkOption ) ) ;
871
+ optionInstances = options . map ( o => o . injector . get < CdkOption > ( CdkOption ) ) ;
872
+ optionElements = options . map ( o => o . nativeElement ) ;
873
+
874
+ expect ( comboboxInstance . isOpen ( ) ) . toBeTrue ( ) ;
875
+
876
+ listboxInstance . setActiveOption ( optionInstances [ 1 ] ) ;
877
+ dispatchKeyboardEvent ( listboxElement , 'keydown' , SPACE ) ;
878
+ fixture . detectChanges ( ) ;
879
+
880
+ expect ( comboboxInstance . isOpen ( ) ) . toBeTrue ( ) ;
881
+ expect ( comboboxInstance . value ) . toBeUndefined ( ) ;
882
+ } ) ;
883
+ } ) ;
765
884
} ) ;
766
885
767
886
@Component ( {
@@ -862,3 +981,36 @@ class ListboxControlValueAccessor {
862
981
this . changedOption = event . option ;
863
982
}
864
983
}
984
+
985
+ @Component ( {
986
+ template : `
987
+ <button cdkCombobox #toggleCombobox class="example-combobox"
988
+ [cdkComboboxTriggerFor]="panel"
989
+ [openActions]="'click'">
990
+ No Value
991
+ </button>
992
+
993
+ <ng-template cdkComboboxPanel #panel="cdkComboboxPanel">
994
+ <select cdkListbox
995
+ [parentPanel]="panel"
996
+ [disabled]="isDisabled"
997
+ [multiple]="isMultiselectable"
998
+ (selectionChange)="onSelectionChange($event)">
999
+ <option cdkOption [value]="'purple'">Purple</option>
1000
+ <option cdkOption [value]="'solar'">Solar</option>
1001
+ <option cdkOption [value]="'arc'">Arc</option>
1002
+ <option cdkOption [value]="'stasis'">Stasis</option>
1003
+ </select>
1004
+ </ng-template>
1005
+ `
1006
+ } )
1007
+ class ListboxInsideCombobox {
1008
+ changedOption : CdkOption < string > ;
1009
+ isDisabled : boolean = false ;
1010
+ isMultiselectable : boolean = false ;
1011
+ @ViewChild ( CdkListbox ) listbox : CdkListbox < string > ;
1012
+
1013
+ onSelectionChange ( event : ListboxSelectionChangeEvent < string > ) {
1014
+ this . changedOption = event . option ;
1015
+ }
1016
+ }
0 commit comments