Skip to content

Commit c39bbb6

Browse files
committed
Added tests for #1313
1 parent 25678b1 commit c39bbb6

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed
+43-4
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,66 @@
11
import { BaseChartDirective } from './base-chart.directive';
22
import { By } from '@angular/platform-browser';
3-
import { TestBed } from '@angular/core/testing';
3+
import { ComponentFixture, TestBed } from '@angular/core/testing';
44
import { Component } from '@angular/core';
5+
import { Chart, registerables } from "chart.js";
56

67
@Component({
7-
template: '<canvas baseChart></canvas>'
8+
template: '<canvas baseChart' +
9+
' [datasets]="datasets"' +
10+
' [labels]="labels"></canvas>'
811
})
912
class TestComponent {
13+
public datasets: any[] = []
14+
public labels: string[] = []
1015
}
1116

1217
describe('BaseChartDirective', () => {
18+
let fixture: ComponentFixture<TestComponent>;
19+
let element: TestComponent;
20+
let directive: BaseChartDirective;
21+
1322
beforeEach(() => {
1423
TestBed.configureTestingModule({
1524
declarations: [
1625
TestComponent,
1726
BaseChartDirective
1827
]
1928
});
29+
30+
Chart.register(...registerables);
31+
32+
fixture = TestBed.createComponent(TestComponent);
33+
element = fixture.componentInstance;
34+
directive = fixture.debugElement.query(By.directive(BaseChartDirective))
35+
.injector.get(BaseChartDirective);
2036
});
2137

2238
it('should create an instance', () => {
23-
const fixture = TestBed.createComponent(TestComponent);
24-
const directive = fixture.debugElement.query(By.directive(BaseChartDirective));
2539
expect(directive).toBeTruthy();
40+
41+
fixture.detectChanges();
42+
43+
expect(directive.chart).toBeDefined();
44+
});
45+
46+
it('should trigger an update when labels or datasets change', () => {
47+
fixture.detectChanges();
48+
49+
element.labels = [ 'Answers' ]
50+
51+
fixture.detectChanges();
52+
53+
expect(directive.chart?.data.labels?.length).toBe(1);
54+
expect(directive.chart?.data.labels).toEqual(element.labels);
55+
56+
element.datasets = [ {
57+
data: [ 42 ]
58+
} ]
59+
60+
fixture.detectChanges();
61+
62+
expect(directive.chart?.data.datasets?.length).toBe(1);
63+
expect(directive.chart?.data.datasets).toEqual(element.datasets);
2664
});
65+
2766
});

Diff for: projects/ng2-charts/src/lib/base-chart.directive.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { distinctUntilChanged } from 'rxjs/operators';
1717

1818
import assign from 'lodash-es/assign';
1919
import merge from 'lodash-es/merge';
20-
import pick from 'lodash-es/pick';
2120

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

31-
@Input() public type!: ChartConfiguration<TType, TData, TLabel>['type'];
30+
@Input() public type: ChartConfiguration<TType, TData, TLabel>['type'] = 'bar' as TType;
3231
@Input() public legend?: boolean;
3332
@Input() public data: ChartConfiguration<TType, TData, TLabel>['data'] = { datasets: [] };
3433
@Input() public options?: ChartConfiguration<TType, TData, TLabel>['options'];
35-
@Input() public plugins?: ChartConfiguration<TType, TData, TLabel>['plugins'];
34+
@Input() public plugins?: ChartConfiguration<TType, TData, TLabel>['plugins'] = [];
3635

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

6766
if (this.chart) {
68-
assign(this.chart.config, pick(config, [ 'data', 'options', 'plugins' ]));
67+
assign(this.chart.config.data, config.data);
68+
assign(this.chart.config.plugins, config.plugins);
69+
assign(this.chart.config.options, config.options);
6970
}
7071

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

9192
public update(duration?: any): void {
9293
if (this.chart) {
94+
console.log(this.chart.config)
9395
this.zone.runOutsideAngular(() => this.chart?.update(duration));
9496
}
9597
}

0 commit comments

Comments
 (0)