@@ -2221,6 +2221,38 @@ describe('MatAutocomplete', () => {
2221
2221
expect ( formControl . value ) . toBe ( 'Cal' , 'Expected new value to be propagated to model' ) ;
2222
2222
} ) ) ;
2223
2223
2224
+ it ( 'should work when dynamically changing the autocomplete' , ( ) => {
2225
+ const fixture = createComponent ( DynamicallyChangingAutocomplete ) ;
2226
+ fixture . detectChanges ( ) ;
2227
+ const input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
2228
+
2229
+ dispatchFakeEvent ( input , 'focusin' ) ;
2230
+ fixture . detectChanges ( ) ;
2231
+
2232
+ expect ( overlayContainerElement . textContent ) . toContain ( 'First' ,
2233
+ `Expected panel to display the option of the first autocomplete.` ) ;
2234
+ expect ( overlayContainerElement . textContent ) . not . toContain ( 'Second' ,
2235
+ `Expected panel to not display the option of the second autocomplete.` ) ;
2236
+
2237
+ // close overlay
2238
+ dispatchFakeEvent ( document , 'click' ) ;
2239
+ fixture . detectChanges ( ) ;
2240
+
2241
+ // Switch to second autocomplete
2242
+ fixture . componentInstance . selected = 1 ;
2243
+ fixture . detectChanges ( ) ;
2244
+
2245
+ // reopen agian
2246
+ dispatchFakeEvent ( input , 'focusin' ) ;
2247
+ fixture . detectChanges ( ) ;
2248
+
2249
+ expect ( overlayContainerElement . textContent ) . not . toContain ( 'First' ,
2250
+ `Expected panel to not display the option of the first autocomplete.` ) ;
2251
+ expect ( overlayContainerElement . textContent ) . toContain ( 'Second' ,
2252
+ `Expected panel to display the option of the second autocomplete.` ) ;
2253
+
2254
+ } ) ;
2255
+
2224
2256
} ) ;
2225
2257
2226
2258
@Component ( {
@@ -2607,3 +2639,19 @@ class AutocompleteWithNativeAutocompleteAttribute {
2607
2639
} )
2608
2640
class InputWithoutAutocompleteAndDisabled {
2609
2641
}
2642
+
2643
+ @Component ( {
2644
+ template : `
2645
+ <input type="number" matInput [matAutocomplete]="selected ? auto1 : auto0">
2646
+ <mat-autocomplete #auto0="matAutocomplete">
2647
+ <mat-option [value]="0">First</mat-option>
2648
+ </mat-autocomplete>
2649
+
2650
+ <mat-autocomplete #auto1="matAutocomplete">
2651
+ <mat-option [value]="1">Second</mat-option>
2652
+ </mat-autocomplete>
2653
+ ` ,
2654
+ } )
2655
+ class DynamicallyChangingAutocomplete {
2656
+ selected = 0 ;
2657
+ }
0 commit comments