Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/ChartComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,29 @@ export function main() {
});
};

it('should create simple chart object', (done) => {
it('should create/destroy simple chart object', (done) => {
create('<chart [options]="options"></chart>').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) => {
Expand Down
10 changes: 7 additions & 3 deletions src/ChartComponent.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,7 +13,7 @@ import { createBaseOpts } from './createBaseOpts';
template: '&nbsp;',
providers: [HighchartsService],
})
export class ChartComponent {
export class ChartComponent implements AfterViewInit, OnDestroy {
@ContentChild(ChartSeriesComponent) series: ChartSeriesComponent;
@ContentChild(ChartXAxisComponent) xAxis: ChartXAxisComponent;
@ContentChild(ChartYAxisComponent) yAxis: ChartYAxisComponent;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class HighchartsChartObjectMock {
constructor (_opts) {
opts = _opts;
}

public destroy() {}
}
const highchartsStatic = {
Chart : HighchartsChartObjectMock,
Expand Down