@@ -31,7 +31,7 @@ import {
3131 flush ,
3232} from '@angular/core/testing' ;
3333import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
34- import { MatOption } from '@angular/material/core' ;
34+ import { MatOption , MatOptionSelectionChange } from '@angular/material/core' ;
3535import { MatFormField , MatFormFieldModule } from '@angular/material/form-field' ;
3636import { By } from '@angular/platform-browser' ;
3737import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -1309,6 +1309,45 @@ describe('MatAutocomplete', () => {
13091309 expect ( componentOptions [ 0 ] . deselect ) . toHaveBeenCalled ( ) ;
13101310 componentOptions . slice ( 1 ) . forEach ( option => expect ( option . deselect ) . not . toHaveBeenCalled ( ) ) ;
13111311 } ) ) ;
1312+
1313+ it ( 'should emit an event when an option is selected' , fakeAsync ( ( ) => {
1314+ const spy = jasmine . createSpy ( 'option selection spy' ) ;
1315+ const subscription = fixture . componentInstance . trigger . optionSelections . subscribe ( spy ) ;
1316+ const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1317+ option . click ( ) ;
1318+ fixture . detectChanges ( ) ;
1319+
1320+ expect ( spy ) . toHaveBeenCalledWith ( jasmine . any ( MatOptionSelectionChange ) ) ;
1321+ subscription . unsubscribe ( ) ;
1322+ } ) ) ;
1323+
1324+ it ( 'should handle `optionSelections` being accessed too early' , fakeAsync ( ( ) => {
1325+ overlayContainer . ngOnDestroy ( ) ;
1326+ fixture . destroy ( ) ;
1327+ fixture = TestBed . createComponent ( SimpleAutocomplete ) ;
1328+
1329+ let spy = jasmine . createSpy ( 'option selection spy' ) ;
1330+ let subscription : Subscription ;
1331+
1332+ expect ( fixture . componentInstance . trigger . autocomplete ) . toBeFalsy ( ) ;
1333+ expect ( ( ) => {
1334+ subscription = fixture . componentInstance . trigger . optionSelections . subscribe ( spy ) ;
1335+ } ) . not . toThrow ( ) ;
1336+
1337+ fixture . detectChanges ( ) ;
1338+ fixture . componentInstance . trigger . openPanel ( ) ;
1339+ fixture . detectChanges ( ) ;
1340+ zone . simulateZoneExit ( ) ;
1341+
1342+ const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1343+
1344+ option . click ( ) ;
1345+ fixture . detectChanges ( ) ;
1346+ zone . simulateZoneExit ( ) ;
1347+
1348+ expect ( spy ) . toHaveBeenCalledWith ( jasmine . any ( MatOptionSelectionChange ) ) ;
1349+ } ) ) ;
1350+
13121351 } ) ;
13131352
13141353 describe ( 'panel closing' , ( ) => {
0 commit comments