diff --git a/src/ChartComponent.spec.ts b/src/ChartComponent.spec.ts index d6cb333..68a06b9 100644 --- a/src/ChartComponent.spec.ts +++ b/src/ChartComponent.spec.ts @@ -55,15 +55,29 @@ export function main() { }); }; - it('should create simple chart object', (done) => { + it('should create/destroy simple chart object', (done) => { create('').then(fixture => { fixture.componentInstance.options = ['options']; - spyOn(highchartsServiceMock.getHighchartsStatic(), 'Chart'); + + const RealChart = highchartsServiceMock.getHighchartsStatic().Chart; + + let destroySpy; + + const chartSpy = spyOn(highchartsServiceMock.getHighchartsStatic(), 'Chart') + .and.callFake(opts => { + const chart = new RealChart(opts); + destroySpy = spyOn(chart, 'destroy'); + return chart; + }); fixture.detectChanges(); - expect(highchartsServiceMock.getHighchartsStatic().Chart).toHaveBeenCalled(); + expect(chartSpy).toHaveBeenCalled(); + + + fixture.destroy(); + expect(destroySpy).toHaveBeenCalled(); done(); - }) + }); }); it('should emit the "create" event with HighchartsChartObject', (done) => { diff --git a/src/ChartComponent.ts b/src/ChartComponent.ts index 007f4a8..94f5926 100644 --- a/src/ChartComponent.ts +++ b/src/ChartComponent.ts @@ -1,4 +1,4 @@ -import { Input, ElementRef, Component, Output, EventEmitter, ContentChild } from '@angular/core'; +import { AfterViewInit, Input, ElementRef, Component, OnDestroy, Output, EventEmitter, ContentChild } from '@angular/core'; import { ChartSeriesComponent } from './ChartSeriesComponent'; import { ChartXAxisComponent } from './ChartXAxisComponent'; @@ -13,7 +13,7 @@ import { createBaseOpts } from './createBaseOpts'; template: ' ', providers: [HighchartsService], }) -export class ChartComponent { +export class ChartComponent implements AfterViewInit, OnDestroy { @ContentChild(ChartSeriesComponent) series: ChartSeriesComponent; @ContentChild(ChartXAxisComponent) xAxis: ChartXAxisComponent; @ContentChild(ChartYAxisComponent) yAxis: ChartYAxisComponent; @@ -45,11 +45,15 @@ export class ChartComponent { } } - ngAfterViewInit() { + public ngAfterViewInit() { this.baseOpts = createBaseOpts(this, this.series, this.series ? this.series.point : null, this.xAxis, this.yAxis, this.element.nativeElement); this.init(); } + public ngOnDestroy() { + if(this.chart) this.chart.destroy(); + } + constructor(element: ElementRef, highchartsService : HighchartsService) { this.element = element; this.highchartsService = highchartsService; diff --git a/src/Mocks.ts b/src/Mocks.ts index 7e129af..efd1d7e 100644 --- a/src/Mocks.ts +++ b/src/Mocks.ts @@ -28,6 +28,8 @@ export class HighchartsChartObjectMock { constructor (_opts) { opts = _opts; } + + public destroy() {} } const highchartsStatic = { Chart : HighchartsChartObjectMock,