@@ -43,12 +43,14 @@ import {
4343 FloatLabelType ,
4444 MAT_LABEL_GLOBAL_OPTIONS ,
4545 MatOption ,
46+ MatOptionSelectionChange ,
4647} from '@angular/material/core' ;
4748import { MatFormFieldModule } from '@angular/material/form-field' ;
4849import { By } from '@angular/platform-browser' ;
4950import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
5051import { map } from 'rxjs/operators/map' ;
5152import { Subject } from 'rxjs/Subject' ;
53+ import { Subscription } from 'rxjs/Subscription' ;
5254import { MatSelectModule } from './index' ;
5355import { MatSelect } from './select' ;
5456import {
@@ -1001,6 +1003,54 @@ describe('MatSelect', () => {
10011003 it ( 'should not throw if triggerValue accessed with no selected value' , fakeAsync ( ( ) => {
10021004 expect ( ( ) => fixture . componentInstance . select . triggerValue ) . not . toThrow ( ) ;
10031005 } ) ) ;
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+
10041054 } ) ;
10051055
10061056 describe ( 'forms integration' , ( ) => {
0 commit comments