@@ -1607,6 +1607,36 @@ describe('MatAutocomplete', () => {
1607
1607
} ) ;
1608
1608
} ) ;
1609
1609
1610
+ describe ( 'form control with initial value' , ( ) => {
1611
+ let fixture : ComponentFixture < FormControlWithInitialValue > ;
1612
+ let input : HTMLInputElement ;
1613
+
1614
+ beforeEach ( waitForAsync ( async ( ) => {
1615
+ fixture = createComponent ( FormControlWithInitialValue ) ;
1616
+ fixture . detectChanges ( ) ;
1617
+
1618
+ input = fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement ;
1619
+
1620
+ fixture . componentInstance . trigger . openPanel ( ) ;
1621
+ fixture . detectChanges ( ) ;
1622
+ await new Promise ( r => setTimeout ( r ) ) ;
1623
+ } ) ) ;
1624
+
1625
+ it ( 'should mark the initial value as selected if its present' , fakeAsync ( ( ) => {
1626
+ const trigger = fixture . componentInstance . trigger ;
1627
+ trigger . openPanel ( ) ;
1628
+ fixture . detectChanges ( ) ;
1629
+
1630
+ const options = overlayContainerElement . querySelectorAll (
1631
+ 'mat-option' ,
1632
+ ) as NodeListOf < HTMLElement > ;
1633
+
1634
+ expect ( input . value ) . toBe ( 'Three' ) ;
1635
+ expect ( options . length ) . toBe ( 3 ) ;
1636
+ expect ( options [ 2 ] . classList ) . toContain ( 'mdc-list-item--selected' ) ;
1637
+ } ) ) ;
1638
+ } ) ;
1639
+
1610
1640
describe ( 'option groups' , ( ) => {
1611
1641
let DOWN_ARROW_EVENT : KeyboardEvent ;
1612
1642
let UP_ARROW_EVENT : KeyboardEvent ;
@@ -4406,6 +4436,31 @@ class PlainAutocompleteInputWithFormControl {
4406
4436
formControl = new FormControl ( '' ) ;
4407
4437
}
4408
4438
4439
+ @Component ( {
4440
+ template : `
4441
+ <mat-form-field>
4442
+ <input matInput placeholder="Choose" [matAutocomplete]="auto" [formControl]="optionCtrl">
4443
+ </mat-form-field>
4444
+ <mat-autocomplete #auto="matAutocomplete" >
4445
+ @for (option of options; track option) {
4446
+ <mat-option [value]="option">
4447
+ {{option}}
4448
+ </mat-option>
4449
+ }
4450
+ </mat-autocomplete>
4451
+ ` ,
4452
+ imports : [ MatAutocomplete , MatAutocompleteTrigger , MatOption , ReactiveFormsModule ] ,
4453
+ } )
4454
+ class FormControlWithInitialValue {
4455
+ optionCtrl = new FormControl ( 'Three' ) ;
4456
+ filteredOptions : Observable < any > ;
4457
+ options = [ 'One' , 'Two' , 'Three' ] ;
4458
+
4459
+ @ViewChild ( MatAutocompleteTrigger ) trigger : MatAutocompleteTrigger ;
4460
+
4461
+ constructor ( ) { }
4462
+ }
4463
+
4409
4464
@Component ( {
4410
4465
template : `
4411
4466
<mat-form-field>
0 commit comments