[MNT-25412] Chip autocomplete set input value when preselected options change (#11350)

This commit is contained in:
Michal Kinas
2025-11-17 14:52:01 +01:00
committed by GitHub
parent a748541ca0
commit 9af4b7a4b6
2 changed files with 14 additions and 1 deletions
@@ -21,7 +21,7 @@ import { By } from '@angular/platform-browser';
import { Subject } from 'rxjs';
import { ContentTestingModule } from '../../../testing/content.testing.module';
import { SearchChipAutocompleteInputComponent } from './search-chip-autocomplete-input.component';
import { DebugElement } from '@angular/core';
import { DebugElement, SimpleChanges } from '@angular/core';
import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatChipHarness, MatChipGridHarness } from '@angular/material/chips/testing';
@@ -128,6 +128,16 @@ describe('SearchChipAutocompleteInputComponent', () => {
expect(component.selectedOptions).toEqual([{ value: 'option1' }]);
});
it('should assign preselected values whenever they are changed', async () => {
const preselectedOptionsChange: SimpleChanges = {
preselectedOptions: { currentValue: [{ value: 'option1' }], previousValue: [], firstChange: false, isFirstChange: () => false }
};
component.ngOnChanges(preselectedOptionsChange);
fixture.detectChanges();
expect(component.selectedOptions).toEqual([{ value: 'option1' }]);
expect(await getChipValue(0)).toBe('option1');
});
it('should add new option only if value is predefined when allowOnlyPredefinedValues = true', async () => {
addNewOption('test');
addNewOption('option1');
@@ -122,6 +122,9 @@ export class SearchChipAutocompleteInputComponent implements OnInit, OnChanges {
? this.filter(changes.autocompleteOptions.currentValue, this.formCtrl.value)
: [];
}
if (changes.preselectedOptions) {
this.selectedOptions = changes.preselectedOptions.currentValue ?? [];
}
}
add(event: MatChipInputEvent) {