Skip to content

Commit

Permalink
fix(ng-chartist): corrected method visibility in chartist component.
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimio committed Jan 14, 2019
1 parent 6d372ef commit de1f891
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
30 changes: 15 additions & 15 deletions projects/ng-chartist/src/lib/chartist.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('chartist component', function(): void {
instance.data = data[chartType];
instance.type = chartType;

await instance.renderChart();
await instance['renderChart']();

expect(Chartist.Bar).toHaveBeenCalledTimes(1);
});
Expand All @@ -45,15 +45,15 @@ describe('chartist component', function(): void {
instance.data = data[chartType];
instance.type = chartType;

const chart = await instance.renderChart();
const chart = await instance['renderChart']();

expect(chart instanceof Chartist.Bar).toBe(true);
});

it('should bind events if there are events', async function() {
const chartType: ChartType = 'Bar';

spyOn(instance, 'bindEvents').and.callThrough();
spyOn(<any>instance, 'bindEvents').and.callThrough();

instance.data = data[chartType];
instance.type = chartType;
Expand All @@ -65,7 +65,7 @@ describe('chartist component', function(): void {

await instance.ngOnInit();

expect(instance.bindEvents).toHaveBeenCalled();
expect(instance['bindEvents']).toHaveBeenCalled();
});

it('should re-render the chart if the chart type changes', function(): void {
Expand All @@ -75,11 +75,11 @@ describe('chartist component', function(): void {

instance.type = 'Line';

spyOn(instance, 'renderChart').and.callThrough();
spyOn(<any>instance, 'renderChart').and.callThrough();

instance.update(changes);
instance['update'](changes);

expect(instance.renderChart).toHaveBeenCalled();
expect(instance['renderChart']).toHaveBeenCalled();
});

it('should update the chart if the data changes', async function() {
Expand All @@ -95,19 +95,19 @@ describe('chartist component', function(): void {

fixture.detectChanges();

await instance.renderChart();
await instance['renderChart']();

instance.data = data.Line;
instance.type = 'Line';

spyOn(instance.chart, 'update').and.callThrough();
spyOn(instance, 'renderChart').and.callThrough();
spyOn(<any>instance, 'renderChart').and.callThrough();

fixture.detectChanges();

instance.update(changes);
instance['update'](changes);

expect(instance.renderChart).not.toHaveBeenCalled();
expect(instance['renderChart']).not.toHaveBeenCalled();
expect(instance.chart.update).toHaveBeenCalled();
});

Expand All @@ -123,19 +123,19 @@ describe('chartist component', function(): void {

fixture.detectChanges();

await instance.renderChart();
await instance['renderChart']();

instance.data = data.Bar;
instance.type = 'Bar';

spyOn(instance.chart, 'update').and.callThrough();
spyOn(instance, 'renderChart').and.callThrough();
spyOn(<any>instance, 'renderChart').and.callThrough();

fixture.detectChanges();

instance.update(changes);
instance['update'](changes);

expect(instance.renderChart).not.toHaveBeenCalled();
expect(instance['renderChart']).not.toHaveBeenCalled();
expect(instance.chart.update).toHaveBeenCalled();
});

Expand Down
30 changes: 16 additions & 14 deletions projects/ng-chartist/src/lib/chartist.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,25 @@ export interface ChartEvent {
template: '<ng-content></ng-content>'
})
export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
@Input()
// @ts-ignore
public data: Promise<Chartist.IChartistData> | Chartist.IChartistData;
@Input()
data: Promise<Chartist.IChartistData> | Chartist.IChartistData;

// @ts-ignore
@Input() public type: Promise<ChartType> | ChartType;

@Input()
// @ts-ignore
public options: Promise<Chartist.IChartOptions> | Chartist.IChartOptions;
type: Promise<ChartType> | ChartType;

// @ts-ignore
@Input()
options: Promise<Chartist.IChartOptions> | Chartist.IChartOptions;

// @ts-ignore
public responsiveOptions: Promise<ResponsiveOptions> | ResponsiveOptions;
@Input()
responsiveOptions: Promise<ResponsiveOptions> | ResponsiveOptions;

// @ts-ignore
@Input() public events: ChartEvent;
@Input()
events: ChartEvent;

// @ts-ignore
public chart: ChartInterfaces;
Expand All @@ -68,7 +70,7 @@ export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
this.element = element.nativeElement;
}

public ngOnInit(): Promise<ChartInterfaces> {
ngOnInit(): Promise<ChartInterfaces> {
if (!this.type || !this.data) {
Promise.reject('Expected at least type and data.');
}
Expand All @@ -82,17 +84,17 @@ export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
});
}

public ngOnChanges(changes: SimpleChanges): void {
ngOnChanges(changes: SimpleChanges): void {
this.update(changes);
}

public ngOnDestroy(): void {
ngOnDestroy(): void {
if (this.chart) {
this.chart.detach();
}
}

public renderChart(): Promise<ChartInterfaces> {
private renderChart(): Promise<ChartInterfaces> {
const promises: any[] = [
this.type,
this.element,
Expand All @@ -114,7 +116,7 @@ export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
});
}

public update(changes: SimpleChanges): void {
private update(changes: SimpleChanges): void {
if (!this.chart || 'type' in changes) {
this.renderChart();
} else {
Expand All @@ -130,7 +132,7 @@ export class ChartistComponent implements OnInit, OnChanges, OnDestroy {
}
}

public bindEvents(chart: any): void {
private bindEvents(chart: any): void {
for (const event of Object.keys(this.events)) {
chart.on(event, this.events[event]);
}
Expand Down

0 comments on commit de1f891

Please sign in to comment.