Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/material-experimental/mdc-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3768,6 +3768,30 @@ describe('MDC-based MatSelect', () => {
.toEqual([true, true, true, true, false, false]);
}));

it('should update the option selected state if the same array is mutated and passed back in',
fakeAsync(() => {
const value: string[] = [];
trigger.click();
testInstance.control.setValue(value);
fixture.detectChanges();

const optionNodes =
Array.from<HTMLElement>(overlayContainerElement.querySelectorAll('mat-option'));
const optionInstances = testInstance.options.toArray();

expect(optionNodes.some(option => {
return option.classList.contains('mdc-list-item--selected');
})).toBe(false);
expect(optionInstances.some(option => option.selected)).toBe(false);

value.push('eggs-5');
testInstance.control.setValue(value);
fixture.detectChanges();

expect(optionNodes[5].classList).toContain('mdc-list-item--selected');
expect(optionInstances[5].selected).toBe(true);
}));

});

it('should be able to provide default values through an injection token', fakeAsync(() => {
Expand Down
22 changes: 22 additions & 0 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4666,6 +4666,28 @@ describe('MatSelect', () => {
.toEqual([true, true, true, true, false, false]);
}));

it('should update the option selected state if the same array is mutated and passed back in',
fakeAsync(() => {
const value: string[] = [];
trigger.click();
testInstance.control.setValue(value);
fixture.detectChanges();

const optionNodes =
Array.from<HTMLElement>(overlayContainerElement.querySelectorAll('mat-option'));
const optionInstances = testInstance.options.toArray();

expect(optionNodes.some(option => option.classList.contains('mat-selected'))).toBe(false);
expect(optionInstances.some(option => option.selected)).toBe(false);

value.push('eggs-5');
testInstance.control.setValue(value);
fixture.detectChanges();

expect(optionNodes[5].classList).toContain('mat-selected');
expect(optionInstances[5].selected).toBe(true);
}));

});

it('should be able to provide default values through an injection token', fakeAsync(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ export abstract class _MatSelectBase<C> extends _MatSelectMixinBase implements A
@Input()
get value(): any { return this._value; }
set value(newValue: any) {
if (newValue !== this._value) {
// Always re-assign an array, because it might have been mutated.
if (newValue !== this._value || (this._multiple && Array.isArray(newValue))) {
if (this.options) {
this._setSelectionByValue(newValue);
}
Expand Down