@@ -21,7 +21,7 @@ import {
2121} from '@angular/core' ;
2222import { async , ComponentFixture , fakeAsync , inject , TestBed , tick } from '@angular/core/testing' ;
2323import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
24- import { MatOption } from '@angular/material/core' ;
24+ import { MatOption , MatOptionSelectionChange } from '@angular/material/core' ;
2525import { MatFormField , MatFormFieldModule } from '@angular/material/form-field' ;
2626import { By } from '@angular/platform-browser' ;
2727import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -1324,6 +1324,43 @@ describe('MatAutocomplete', () => {
13241324 componentOptions . slice ( 1 ) . forEach ( option => expect ( option . deselect ) . not . toHaveBeenCalled ( ) ) ;
13251325 } ) ;
13261326 } ) ) ;
1327+
1328+ it ( 'should emit an event when an option is selected' , fakeAsync ( ( ) => {
1329+ const spy = jasmine . createSpy ( 'option selection spy' ) ;
1330+ const subscription = fixture . componentInstance . trigger . optionSelections . subscribe ( spy ) ;
1331+ const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1332+ option . click ( ) ;
1333+ fixture . detectChanges ( ) ;
1334+
1335+ expect ( spy ) . toHaveBeenCalledWith ( jasmine . any ( MatOptionSelectionChange ) ) ;
1336+ subscription . unsubscribe ( ) ;
1337+ } ) ) ;
1338+
1339+ it ( 'should handle `optionSelections` being accessed too early' , async ( ( ) => {
1340+ fixture . destroy ( ) ;
1341+ fixture = TestBed . createComponent ( SimpleAutocomplete ) ;
1342+
1343+ let spy = jasmine . createSpy ( 'option selection spy' ) ;
1344+ let subscription : Subscription ;
1345+
1346+ expect ( fixture . componentInstance . trigger . autocomplete ) . toBeFalsy ( ) ;
1347+ expect ( ( ) => {
1348+ subscription = fixture . componentInstance . trigger . optionSelections . subscribe ( spy ) ;
1349+ } ) . not . toThrow ( ) ;
1350+
1351+ fixture . detectChanges ( ) ;
1352+ fixture . componentInstance . trigger . openPanel ( ) ;
1353+ fixture . detectChanges ( ) ;
1354+
1355+ fixture . whenStable ( ) . then ( ( ) => {
1356+ const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1357+ option . click ( ) ;
1358+ fixture . detectChanges ( ) ;
1359+
1360+ expect ( spy ) . toHaveBeenCalledWith ( jasmine . any ( MatOptionSelectionChange ) ) ;
1361+ } ) ;
1362+ } ) ) ;
1363+
13271364 } ) ;
13281365
13291366 describe ( 'panel closing' , ( ) => {
0 commit comments