Skip to content

Commit

Permalink
Added tests for #1313
Browse files Browse the repository at this point in the history
  • Loading branch information
santam85 committed May 20, 2021
1 parent 25678b1 commit c39bbb6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
47 changes: 43 additions & 4 deletions projects/ng2-charts/src/lib/base-chart.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,66 @@
import { BaseChartDirective } from './base-chart.directive';
import { By } from '@angular/platform-browser';
import { TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Component } from '@angular/core';
import { Chart, registerables } from "chart.js";

@Component({
template: '<canvas baseChart></canvas>'
template: '<canvas baseChart' +
' [datasets]="datasets"' +
' [labels]="labels"></canvas>'
})
class TestComponent {
public datasets: any[] = []
public labels: string[] = []
}

describe('BaseChartDirective', () => {
let fixture: ComponentFixture<TestComponent>;
let element: TestComponent;
let directive: BaseChartDirective;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent,
BaseChartDirective
]
});

Chart.register(...registerables);

fixture = TestBed.createComponent(TestComponent);
element = fixture.componentInstance;
directive = fixture.debugElement.query(By.directive(BaseChartDirective))
.injector.get(BaseChartDirective);
});

it('should create an instance', () => {
const fixture = TestBed.createComponent(TestComponent);
const directive = fixture.debugElement.query(By.directive(BaseChartDirective));
expect(directive).toBeTruthy();

fixture.detectChanges();

expect(directive.chart).toBeDefined();
});

it('should trigger an update when labels or datasets change', () => {
fixture.detectChanges();

element.labels = [ 'Answers' ]

fixture.detectChanges();

expect(directive.chart?.data.labels?.length).toBe(1);
expect(directive.chart?.data.labels).toEqual(element.labels);

element.datasets = [ {
data: [ 42 ]
} ]

fixture.detectChanges();

expect(directive.chart?.data.datasets?.length).toBe(1);
expect(directive.chart?.data.datasets).toEqual(element.datasets);
});

});
10 changes: 6 additions & 4 deletions projects/ng2-charts/src/lib/base-chart.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { distinctUntilChanged } from 'rxjs/operators';

import assign from 'lodash-es/assign';
import merge from 'lodash-es/merge';
import pick from 'lodash-es/pick';

@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
Expand All @@ -28,11 +27,11 @@ export class BaseChartDirective<TType extends ChartType = ChartType,
TData = DefaultDataPoint<TType>,
TLabel = unknown> implements OnDestroy, OnChanges {

@Input() public type!: ChartConfiguration<TType, TData, TLabel>['type'];
@Input() public type: ChartConfiguration<TType, TData, TLabel>['type'] = 'bar' as TType;
@Input() public legend?: boolean;
@Input() public data: ChartConfiguration<TType, TData, TLabel>['data'] = { datasets: [] };
@Input() public options?: ChartConfiguration<TType, TData, TLabel>['options'];
@Input() public plugins?: ChartConfiguration<TType, TData, TLabel>['plugins'];
@Input() public plugins?: ChartConfiguration<TType, TData, TLabel>['plugins'] = [];

@Input() public labels?: ChartConfiguration<TType, TData, TLabel>['data']['labels'];
@Input() public datasets?: ChartConfiguration<TType, TData, TLabel>['data']['datasets'];
Expand Down Expand Up @@ -65,7 +64,9 @@ export class BaseChartDirective<TType extends ChartType = ChartType,
const config = this.getChartConfiguration();

if (this.chart) {
assign(this.chart.config, pick(config, [ 'data', 'options', 'plugins' ]));
assign(this.chart.config.data, config.data);
assign(this.chart.config.plugins, config.plugins);
assign(this.chart.config.options, config.options);
}

this.update();
Expand All @@ -90,6 +91,7 @@ export class BaseChartDirective<TType extends ChartType = ChartType,

public update(duration?: any): void {
if (this.chart) {
console.log(this.chart.config)
this.zone.runOutsideAngular(() => this.chart?.update(duration));
}
}
Expand Down

0 comments on commit c39bbb6

Please sign in to comment.