@@ -2221,6 +2221,38 @@ describe('MatAutocomplete', () => {
22212221 expect ( formControl . value ) . toBe ( 'Cal' , 'Expected new value to be propagated to model' ) ;
22222222 } ) ) ;
22232223
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+
22242256} ) ;
22252257
22262258@Component ( {
@@ -2607,3 +2639,19 @@ class AutocompleteWithNativeAutocompleteAttribute {
26072639} )
26082640class InputWithoutAutocompleteAndDisabled {
26092641}
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