From cf5ef7d258c0889719b672e152d5bd9d8c28861f Mon Sep 17 00:00:00 2001 From: Aziz Abbas Date: Tue, 10 May 2016 13:21:22 -0700 Subject: [PATCH] feat(radio-button) fix thrown error on dismiss solves #6466 --- ionic/components/radio/radio-button.ts | 4 +- ionic/components/radio/test/radio.spec.ts | 56 +++++++++++++++-------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/ionic/components/radio/radio-button.ts b/ionic/components/radio/radio-button.ts index 409d47c5f7..df1f3df9be 100644 --- a/ionic/components/radio/radio-button.ts +++ b/ionic/components/radio/radio-button.ts @@ -155,6 +155,8 @@ export class RadioButton { */ ngOnDestroy() { this._form.deregister(this); - this._group.remove(this); + if (this._group) { + this._group.remove(this); + } } } diff --git a/ionic/components/radio/test/radio.spec.ts b/ionic/components/radio/test/radio.spec.ts index b609c89fda..0a176099cb 100644 --- a/ionic/components/radio/test/radio.spec.ts +++ b/ionic/components/radio/test/radio.spec.ts @@ -86,30 +86,48 @@ export function run() { }); }); + }); - let rg: RadioGroup; - let form: Form; + describe('RadioButton', () => { - function createRadioButton() { - return new RadioButton(form, null, rg); - } + describe('ngOnDestroy', () => { + it('should work without a group', () => { + let rb1 = createRadioButton(false); + expect(() => rb1.ngOnDestroy()).not.toThrowError(); + }); - function mockRenderer(): any { - return { - setElementAttribute: function(){} - } - } + 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); + }); - 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(); + }); }