@@ -43,12 +43,14 @@ import {
43
43
FloatLabelType ,
44
44
MAT_LABEL_GLOBAL_OPTIONS ,
45
45
MatOption ,
46
+ MatOptionSelectionChange ,
46
47
} from '@angular/material/core' ;
47
48
import { MatFormFieldModule } from '@angular/material/form-field' ;
48
49
import { By } from '@angular/platform-browser' ;
49
50
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
50
51
import { map } from 'rxjs/operators/map' ;
51
52
import { Subject } from 'rxjs/Subject' ;
53
+ import { Subscription } from 'rxjs/Subscription' ;
52
54
import { MatSelectModule } from './index' ;
53
55
import { MatSelect } from './select' ;
54
56
import {
@@ -1001,6 +1003,54 @@ describe('MatSelect', () => {
1001
1003
it ( 'should not throw if triggerValue accessed with no selected value' , fakeAsync ( ( ) => {
1002
1004
expect ( ( ) => fixture . componentInstance . select . triggerValue ) . not . toThrow ( ) ;
1003
1005
} ) ) ;
1006
+
1007
+ it ( 'should emit to `optionSelectionChanges` when an option is selected' , fakeAsync ( ( ) => {
1008
+ trigger . click ( ) ;
1009
+ fixture . detectChanges ( ) ;
1010
+ flush ( ) ;
1011
+
1012
+ const spy = jasmine . createSpy ( 'option selection spy' ) ;
1013
+ const subscription = fixture . componentInstance . select . optionSelectionChanges . subscribe ( spy ) ;
1014
+ const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1015
+ option . click ( ) ;
1016
+ fixture . detectChanges ( ) ;
1017
+ flush ( ) ;
1018
+
1019
+ expect ( spy ) . toHaveBeenCalledWith ( jasmine . any ( MatOptionSelectionChange ) ) ;
1020
+
1021
+ subscription . unsubscribe ( ) ;
1022
+ } ) ) ;
1023
+
1024
+ it ( 'should handle accessing `optionSelectionChanges` before the options are initialized' ,
1025
+ fakeAsync ( ( ) => {
1026
+ fixture . destroy ( ) ;
1027
+ fixture = TestBed . createComponent ( BasicSelect ) ;
1028
+
1029
+ let spy = jasmine . createSpy ( 'option selection spy' ) ;
1030
+ let subscription : Subscription ;
1031
+
1032
+ expect ( fixture . componentInstance . select . options ) . toBeFalsy ( ) ;
1033
+ expect ( ( ) => {
1034
+ subscription = fixture . componentInstance . select . optionSelectionChanges . subscribe ( spy ) ;
1035
+ } ) . not . toThrow ( ) ;
1036
+
1037
+ fixture . detectChanges ( ) ;
1038
+ trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
1039
+
1040
+ trigger . click ( ) ;
1041
+ fixture . detectChanges ( ) ;
1042
+ flush ( ) ;
1043
+
1044
+ const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1045
+ option . click ( ) ;
1046
+ fixture . detectChanges ( ) ;
1047
+ flush ( ) ;
1048
+
1049
+ expect ( spy ) . toHaveBeenCalledWith ( jasmine . any ( MatOptionSelectionChange ) ) ;
1050
+
1051
+ subscription ! . unsubscribe ( ) ;
1052
+ } ) ) ;
1053
+
1004
1054
} ) ;
1005
1055
1006
1056
describe ( 'forms integration' , ( ) => {
0 commit comments