diff --git a/src/components/radio/radio.spec.ts b/src/components/radio/radio.spec.ts index b63a4129f916..1ec336ee1966 100644 --- a/src/components/radio/radio.spec.ts +++ b/src/components/radio/radio.spec.ts @@ -352,7 +352,28 @@ export function main() { }); }).then(done); }); + it('should deselect all buttons when model is null or undefined', (done: () => void) => { + builder + .overrideTemplate(TestAppWithInitialValue, ` + + + + `) + .createAsync(TestAppWithInitialValue) + .then(fixture => { + fakeAsync(function() { + let buttons = fixture.debugElement.queryAll(By.css('md-radio-button')); + fixture.detectChanges(); + fixture.componentInstance.choice = 0; + expect(isSinglySelected(buttons[0], buttons)).toBe(true); + + fixture.detectChanges(); + fixture.componentInstance.choice = null; + expect(allDeselected(buttons)).toBe(true); + }); + }).then(done); + }); }); } @@ -365,6 +386,13 @@ function isSinglySelected(button: DebugElement, buttons: DebugElement[]): boolea return component.checked && otherSelectedButtons.length == 0; } +/** Checks whether no button is selected from a group of buttons. */ +function allDeselected(buttons: DebugElement[]): boolean { + let selectedButtons = + buttons.filter((e: DebugElement) => e.componentInstance.checked); + return selectedButtons.length == 0; +} + /** Browser-agnostic function for creating an event. */ function createEvent(name: string): Event { let ev: Event; diff --git a/src/components/radio/radio.ts b/src/components/radio/radio.ts index a2b782af4060..09740e6da98d 100644 --- a/src/components/radio/radio.ts +++ b/src/components/radio/radio.ts @@ -150,7 +150,10 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor { }); if (matched.length == 0) { - // Didn't find a button that matches this value, return early without setting. + // Didn't find a button that matches this value, deselecting all buttons. + if (this._value == null) { + this.selected = null; + } return; }