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);
this._group.remove(this); if (this._group) {
this._group.remove(this);
}
} }
} }

View File

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