Merge pull request #6492 from teleaziz/fixRadio

feat(radio-button) fix thrown error on dismiss solves #6466
This commit is contained in:
Brandy Carney
2016-05-10 17:26:05 -04:00
2 changed files with 40 additions and 20 deletions

View File

@ -155,6 +155,8 @@ export class RadioButton {
*/ */
ngOnDestroy() { ngOnDestroy() {
this._form.deregister(this); this._form.deregister(this);
if (this._group) {
this._group.remove(this); this._group.remove(this);
} }
} }
}

View File

@ -86,12 +86,32 @@ export function run() {
}); });
}); });
});
describe('RadioButton', () => {
describe('ngOnDestroy', () => {
it('should work without a group', () => {
let rb1 = createRadioButton(false);
expect(() => rb1.ngOnDestroy()).not.toThrowError();
});
it('should remove button from group if part of a radio group', () => {
let rb1 = createRadioButton();
spyOn(rg, 'remove');
rb1.ngOnDestroy();
expect(rg.remove).toHaveBeenCalledWith(rb1);
});
});
});
let rg: RadioGroup; let rg: RadioGroup;
let form: Form; let form: Form;
function createRadioButton() { function createRadioButton(shouldIncludeGroup = true) {
return new RadioButton(form, null, rg); return new RadioButton(form, null, shouldIncludeGroup? rg : null);
} }
function mockRenderer(): any { function mockRenderer(): any {
@ -110,6 +130,4 @@ export function run() {
rg = new RadioGroup(mockRenderer(), mockElementRef()); rg = new RadioGroup(mockRenderer(), mockElementRef());
form = new Form(); form = new Form();
}); });
});
} }