@@ -1614,6 +1614,36 @@ describe('MatAutocomplete', () => {
16141614 } ) ;
16151615 } ) ;
16161616
1617+ describe ( 'form control with initial value' , ( ) => {
1618+ let fixture : ComponentFixture < FormControlWithInitialValue > ;
1619+ let input : HTMLInputElement ;
1620+
1621+ beforeEach ( waitForAsync ( async ( ) => {
1622+ fixture = createComponent ( FormControlWithInitialValue ) ;
1623+ fixture . detectChanges ( ) ;
1624+
1625+ input = fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement ;
1626+
1627+ fixture . componentInstance . trigger . openPanel ( ) ;
1628+ fixture . detectChanges ( ) ;
1629+ await new Promise ( r => setTimeout ( r ) ) ;
1630+ } ) ) ;
1631+
1632+ it ( 'should mark the initial value as selected if its present' , fakeAsync ( ( ) => {
1633+ const trigger = fixture . componentInstance . trigger ;
1634+ trigger . openPanel ( ) ;
1635+ fixture . detectChanges ( ) ;
1636+
1637+ const options = overlayContainerElement . querySelectorAll (
1638+ 'mat-option' ,
1639+ ) as NodeListOf < HTMLElement > ;
1640+
1641+ expect ( input . value ) . toBe ( 'Three' ) ;
1642+ expect ( options . length ) . toBe ( 3 ) ;
1643+ expect ( options [ 2 ] . classList ) . toContain ( 'mdc-list-item--selected' ) ;
1644+ } ) ) ;
1645+ } ) ;
1646+
16171647 describe ( 'option groups' , ( ) => {
16181648 let DOWN_ARROW_EVENT : KeyboardEvent ;
16191649 let UP_ARROW_EVENT : KeyboardEvent ;
@@ -4414,6 +4444,31 @@ class PlainAutocompleteInputWithFormControl {
44144444 formControl = new FormControl ( '' ) ;
44154445}
44164446
4447+ @Component ( {
4448+ template : `
4449+ <mat-form-field>
4450+ <input matInput placeholder="Choose" [matAutocomplete]="auto" [formControl]="optionCtrl">
4451+ </mat-form-field>
4452+ <mat-autocomplete #auto="matAutocomplete" >
4453+ @for (option of options; track option) {
4454+ <mat-option [value]="option">
4455+ {{option}}
4456+ </mat-option>
4457+ }
4458+ </mat-autocomplete>
4459+ ` ,
4460+ imports : [ MatAutocomplete , MatAutocompleteTrigger , MatOption , ReactiveFormsModule ] ,
4461+ } )
4462+ class FormControlWithInitialValue {
4463+ optionCtrl = new FormControl ( 'Three' ) ;
4464+ filteredOptions : Observable < any > ;
4465+ options = [ 'One' , 'Two' , 'Three' ] ;
4466+
4467+ @ViewChild ( MatAutocompleteTrigger ) trigger : MatAutocompleteTrigger ;
4468+
4469+ constructor ( ) { }
4470+ }
4471+
44174472@Component ( {
44184473 template : `
44194474 <mat-form-field>
0 commit comments