diff --git a/src/lib/select/select.spec.ts b/src/lib/select/select.spec.ts index 43a4af6673cf..051f439e34c6 100644 --- a/src/lib/select/select.spec.ts +++ b/src/lib/select/select.spec.ts @@ -300,6 +300,41 @@ describe('MdSelect', () => { expect(optionInstances[2].selected).toBe(false); }); + it('should deselect other options when one is programmatically selected', () => { + let control = fixture.componentInstance.control; + let foods = fixture.componentInstance.foods; + + trigger.click(); + fixture.detectChanges(); + + let options = + overlayContainerElement.querySelectorAll('md-option') as NodeListOf; + + options[0].click(); + fixture.detectChanges(); + + control.setValue(foods[1].value); + fixture.detectChanges(); + + trigger.click(); + fixture.detectChanges(); + + options = + overlayContainerElement.querySelectorAll('md-option') as NodeListOf; + + expect(options[0].classList) + .not.toContain('mat-selected', 'Expected first option to no longer be selected'); + expect(options[1].classList) + .toContain('mat-selected', 'Expected second option to be selected'); + + const optionInstances = fixture.componentInstance.options.toArray(); + + expect(optionInstances[0].selected) + .toBe(false, 'Expected first option to no longer be selected'); + expect(optionInstances[1].selected) + .toBe(true, 'Expected second option to be selected'); + }); + it('should remove selection if option has been removed', async(() => { let select = fixture.componentInstance.select; diff --git a/src/lib/select/select.ts b/src/lib/select/select.ts index 5099709a42f6..8596d980b4a8 100644 --- a/src/lib/select/select.ts +++ b/src/lib/select/select.ts @@ -544,12 +544,13 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal throw getMdSelectNonArrayValueError(); } + this._clearSelection(); + if (isArray) { - this._clearSelection(); value.forEach((currentValue: any) => this._selectValue(currentValue)); this._sortValues(); - } else if (!this._selectValue(value)) { - this._clearSelection(); + } else { + this._selectValue(value); } this._setValueWidth();