@@ -918,6 +918,69 @@ describe('MdAutocomplete', () => {
918
918
919
919
} ) ;
920
920
921
+ describe ( 'Option selection' , ( ) => {
922
+ let fixture : ComponentFixture < SimpleAutocomplete > ;
923
+
924
+ beforeEach ( ( ) => {
925
+ fixture = TestBed . createComponent ( SimpleAutocomplete ) ;
926
+ fixture . detectChanges ( ) ;
927
+
928
+ fixture . componentInstance . trigger . openPanel ( ) ;
929
+ fixture . detectChanges ( ) ;
930
+ } ) ;
931
+
932
+ it ( 'should deselect any other selected option' , async ( ( ) => {
933
+ let options =
934
+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
935
+ options [ 0 ] . click ( ) ;
936
+ fixture . detectChanges ( ) ;
937
+
938
+ fixture . whenStable ( ) . then ( ( ) => {
939
+ fixture . detectChanges ( ) ;
940
+
941
+ let componentOptions = fixture . componentInstance . options . toArray ( ) ;
942
+ expect ( componentOptions [ 0 ] . selected )
943
+ . toBe ( true , `Clicked option should be selected.` ) ;
944
+
945
+ options =
946
+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
947
+ options [ 1 ] . click ( ) ;
948
+ fixture . detectChanges ( ) ;
949
+
950
+ expect ( componentOptions [ 0 ] . selected )
951
+ . toBe ( false , `Previous option should not be selected.` ) ;
952
+ expect ( componentOptions [ 1 ] . selected )
953
+ . toBe ( true , `New Clicked option should be selected.` ) ;
954
+
955
+ } ) ;
956
+ } ) ) ;
957
+
958
+ it ( 'should call deselect only on the previous selected option' , async ( ( ) => {
959
+ let options =
960
+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
961
+ options [ 0 ] . click ( ) ;
962
+ fixture . detectChanges ( ) ;
963
+
964
+ fixture . whenStable ( ) . then ( ( ) => {
965
+ fixture . detectChanges ( ) ;
966
+
967
+ let componentOptions = fixture . componentInstance . options . toArray ( ) ;
968
+ componentOptions . forEach ( option => spyOn ( option , 'deselect' ) ) ;
969
+
970
+ expect ( componentOptions [ 0 ] . selected )
971
+ . toBe ( true , `Clicked option should be selected.` ) ;
972
+
973
+ options =
974
+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
975
+ options [ 1 ] . click ( ) ;
976
+ fixture . detectChanges ( ) ;
977
+
978
+ expect ( componentOptions [ 0 ] . deselect ) . toHaveBeenCalled ( ) ;
979
+ componentOptions . slice ( 1 ) . forEach ( option => expect ( option . deselect ) . not . toHaveBeenCalled ( ) ) ;
980
+ } ) ;
981
+ } ) ) ;
982
+ } ) ;
983
+
921
984
describe ( 'misc' , ( ) => {
922
985
923
986
it ( 'should allow basic use without any forms directives' , ( ) => {
0 commit comments