diff --git a/lib/components/Redoc/redoc.js b/lib/components/Redoc/redoc.js index cf50f815..6345d6e6 100644 --- a/lib/components/Redoc/redoc.js +++ b/lib/components/Redoc/redoc.js @@ -106,16 +106,22 @@ export default class Redoc extends BaseComponent { static dispose() { let dom = new BrowserDomAdapter(); let el = dom.query('redoc'); - let parent = el.parentElement; - let nextSibling = el.nextElementSibling; + let parent; + let nextSibling; + if (el) { + parent = el.parentElement; + nextSibling = el.nextElementSibling; + } - Redoc.appRef && Redoc.appRef.dispose(); - Redoc.appRef = null; + if (Redoc.appRef) { + Redoc.appRef.dispose(); + Redoc.appRef = null; - // Redoc dispose removes host element, so need to restore it - el = dom.createElement('redoc'); - el.innerText = 'Loading...'; - parent.insertBefore(el, nextSibling); + // Redoc dispose removes host element, so need to restore it + el = dom.createElement('redoc'); + el.innerText = 'Loading...'; + parent && parent.insertBefore(el, nextSibling); + } } } Redoc.parameters = Redoc.parameters.concat([[OptionsManager], [ElementRef], [BrowserDomAdapter]]); diff --git a/lib/components/Redoc/redoc.spec.js b/lib/components/Redoc/redoc.spec.js index a9642299..211d1269 100644 --- a/lib/components/Redoc/redoc.spec.js +++ b/lib/components/Redoc/redoc.spec.js @@ -129,8 +129,59 @@ describe('Redoc init', () => { done.fail('Error handler should not been called'); }); }); -}); + describe('Redoc dispose', () => { + let builder; + let fixture; + let element; + let disposeSpy; + let dom = new BrowserDomAdapter(); + beforeEachProviders(() => [ + provide(SchemaManager, {useValue: new SchemaManager()}), + provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()}), + provide(OptionsManager, {useValue: optsMgr}) + ]); + beforeEach(injectAsync([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => { + builder = tcb; + return schemaMgr.load('/tests/schemas/methods-list-component.json').then(() => null, (err) => { throw err; }); + })); + + beforeEach((done) => { + builder.createAsync(TestApp).then(_fixture => { + fixture = _fixture; + element = getChildDebugElement(fixture.debugElement, 'methods-list').nativeElement; + disposeSpy = jasmine.createSpy('spy'); + Redoc.appRef = { + dispose: disposeSpy + }; + fixture.detectChanges(); + done(); + }, err => { throw err; }); + }); + + afterEach(()=> { + fixture.destroy(); + Redoc.appRef = null; + }); + + it('should call componentRef.dispose', () => { + Redoc.dispose(); + expect(disposeSpy).toHaveBeenCalled(); + }); + + it('should create new host element', () => { + element.parentElement.removeChild(element); + Redoc.dispose(); + expect(dom.query('redoc')).not.toBeNull(); + dom.query('redoc').should.not.be.equal(element); + }); + + it('should set to null appRef', () => { + Redoc.dispose(); + expect(Redoc.appRef).toBeNull(); + }); + }); +}); /** Test component that contains a Redoc. */ @Component({selector: 'test-app'})