diff --git a/src/lib/select/select.spec.ts b/src/lib/select/select.spec.ts index 2b0d6796b98e..e2b5e51526dc 100644 --- a/src/lib/select/select.spec.ts +++ b/src/lib/select/select.spec.ts @@ -56,7 +56,8 @@ describe('MdSelect', () => { BasicSelectInitiallyHidden, BasicSelectNoPlaceholder, BasicSelectWithTheming, - ResetValuesSelect + ResetValuesSelect, + FalsyValueSelect ], providers: [ {provide: OverlayContainer, useFactory: () => { @@ -621,6 +622,22 @@ describe('MdSelect', () => { .toContain('*', `Expected placeholder to have an asterisk, as control was required.`); }); + it('should be able to programmatically select a falsy option', () => { + fixture.destroy(); + + const falsyFixture = TestBed.createComponent(FalsyValueSelect); + + falsyFixture.detectChanges(); + falsyFixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement.click(); + falsyFixture.componentInstance.control.setValue(0); + falsyFixture.detectChanges(); + + expect(falsyFixture.componentInstance.options.first.selected) + .toBe(true, 'Expected first option to be selected'); + expect(overlayContainerElement.querySelectorAll('md-option')[0].classList) + .toContain('mat-selected', 'Expected first option to be selected'); + }); + }); describe('disabled behavior', () => { @@ -2504,3 +2521,20 @@ class ResetValuesSelect { @ViewChild(MdSelect) select: MdSelect; } + + +@Component({ + template: ` + + {{ food.viewValue }} + + ` +}) +class FalsyValueSelect { + foods: any[] = [ + { value: 0, viewValue: 'Steak' }, + { value: 1, viewValue: 'Pizza' }, + ]; + control = new FormControl(); + @ViewChildren(MdOption) options: QueryList; +} diff --git a/src/lib/select/select.ts b/src/lib/select/select.ts index 107dd980331a..b2593cdca35f 100644 --- a/src/lib/select/select.ts +++ b/src/lib/select/select.ts @@ -579,7 +579,9 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal */ private _selectValue(value: any): MdOption { let optionsArray = this.options.toArray(); - let correspondingOption = optionsArray.find(option => option.value && option.value === value); + let correspondingOption = optionsArray.find(option => { + return option.value != null && option.value === value; + }); if (correspondingOption) { correspondingOption.select();