@@ -31,7 +31,7 @@ import {
31
31
flush ,
32
32
} from '@angular/core/testing' ;
33
33
import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
34
- import { MatOption } from '@angular/material/core' ;
34
+ import { MatOption , MatOptionSelectionChange } from '@angular/material/core' ;
35
35
import { MatFormField , MatFormFieldModule } from '@angular/material/form-field' ;
36
36
import { By } from '@angular/platform-browser' ;
37
37
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
@@ -1309,6 +1309,45 @@ describe('MatAutocomplete', () => {
1309
1309
expect ( componentOptions [ 0 ] . deselect ) . toHaveBeenCalled ( ) ;
1310
1310
componentOptions . slice ( 1 ) . forEach ( option => expect ( option . deselect ) . not . toHaveBeenCalled ( ) ) ;
1311
1311
} ) ) ;
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
+
1312
1351
} ) ;
1313
1352
1314
1353
describe ( 'panel closing' , ( ) => {
0 commit comments