@@ -21,7 +21,7 @@ import {
21
21
} from '@angular/core' ;
22
22
import { async , ComponentFixture , fakeAsync , inject , TestBed , tick } from '@angular/core/testing' ;
23
23
import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
24
- import { MatOption } from '@angular/material/core' ;
24
+ import { MatOption , MatOptionSelectionChange } from '@angular/material/core' ;
25
25
import { MatFormField , MatFormFieldModule } from '@angular/material/form-field' ;
26
26
import { By } from '@angular/platform-browser' ;
27
27
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -1324,6 +1324,42 @@ describe('MatAutocomplete', () => {
1324
1324
componentOptions . slice ( 1 ) . forEach ( option => expect ( option . deselect ) . not . toHaveBeenCalled ( ) ) ;
1325
1325
} ) ;
1326
1326
} ) ) ;
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
+ } ) ) ;
1337
+
1338
+ it ( 'should handle `optionSelections` being accessed too early' , async ( ( ) => {
1339
+ fixture . destroy ( ) ;
1340
+ fixture = TestBed . createComponent ( SimpleAutocomplete ) ;
1341
+
1342
+ let spy = jasmine . createSpy ( 'option selection spy' ) ;
1343
+ let subscription : Subscription ;
1344
+
1345
+ expect ( fixture . componentInstance . trigger . autocomplete ) . toBeFalsy ( ) ;
1346
+ expect ( ( ) => {
1347
+ subscription = fixture . componentInstance . trigger . optionSelections . subscribe ( spy ) ;
1348
+ } ) . not . toThrow ( ) ;
1349
+
1350
+ fixture . detectChanges ( ) ;
1351
+ fixture . componentInstance . trigger . openPanel ( ) ;
1352
+ fixture . detectChanges ( ) ;
1353
+
1354
+ fixture . whenStable ( ) . then ( ( ) => {
1355
+ const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1356
+ option . click ( ) ;
1357
+ fixture . detectChanges ( ) ;
1358
+
1359
+ expect ( spy ) . toHaveBeenCalledWith ( jasmine . any ( MatOptionSelectionChange ) ) ;
1360
+ } ) ;
1361
+ } ) ) ;
1362
+
1327
1363
} ) ;
1328
1364
1329
1365
describe ( 'panel closing' , ( ) => {
0 commit comments