Skip to content

Commit dd5e0ce

Browse files
committed
test(chartjs): add some testing
1 parent 9858f30 commit dd5e0ce

File tree

6 files changed

+59
-38
lines changed

6 files changed

+59
-38
lines changed

projects/coreui-angular-chartjs/src/lib/chartjs.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[id]="id"
66
[width]="width"
77
role="img"
8-
style="display: none"
8+
style="display: none;"
99
>
1010
<ng-content></ng-content>
1111
</canvas>
Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,69 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { Chart, registerables } from 'chart.js';
23

34
import { ChartjsComponent } from './chartjs.component';
45

56
describe('ChartjsComponent', () => {
67
let component: ChartjsComponent;
78
let fixture: ComponentFixture<ChartjsComponent>;
89

10+
const colors = {
11+
label: 'My dataset',
12+
backgroundColor: 'rgba(77,189,116,.2)',
13+
borderColor: '#4dbd74',
14+
pointHoverBackgroundColor: '#fff'
15+
};
16+
17+
const labels = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];
18+
19+
const data = {
20+
labels: labels,
21+
datasets: [{
22+
data: [65, 59, 84, 84, 51, 55, 40],
23+
...colors,
24+
fill: { value: 65 }
25+
}]
26+
};
27+
928
beforeEach(async () => {
1029
await TestBed.configureTestingModule({
11-
declarations: [ ChartjsComponent ]
30+
declarations: [ChartjsComponent]
1231
})
13-
.compileComponents();
14-
});
32+
.compileComponents();
33+
34+
Chart.register(...registerables);
1535

16-
beforeEach(() => {
1736
fixture = TestBed.createComponent(ChartjsComponent);
1837
component = fixture.componentInstance;
38+
component.data = { ...data };
1939
fixture.detectChanges();
2040
});
2141

2242
it('should create', () => {
2343
expect(component).toBeTruthy();
44+
45+
fixture.detectChanges();
46+
47+
expect(component.chart).toBeDefined();
48+
});
49+
50+
it('chart should receive data', () => {
51+
52+
fixture.detectChanges();
53+
expect(component.chart?.data.labels?.length).toBe(7);
54+
expect(component.chart?.data.labels).toEqual(labels);
55+
expect(component.chart?.data.datasets[0]?.data.length).toBe(7);
2456
});
57+
58+
// it('should trigger an update when labels or datasets change', () => {
59+
// const newData = { ...data}
60+
// newData.labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May'];
61+
// newData.datasets[0] = {...data.datasets[0], data: [42, 88, 42, 66, 77]};
62+
//
63+
// component.data = newData;
64+
// fixture.detectChanges();
65+
//
66+
// expect(component.chart?.data.labels?.length).toBe(5);
67+
//
68+
// });
2569
});

projects/coreui-angular-chartjs/src/lib/chartjs.component.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
112112
}
113113

114114
ngOnChanges(changes: SimpleChanges): void {
115-
this.updateChart();
115+
if (!changes.data.firstChange) {
116+
this.updateChart();
117+
}
116118
}
117119

118120
ngOnDestroy(): void {
@@ -157,7 +159,7 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
157159
});
158160
setTimeout(() => {
159161
this.renderer.setStyle(this.canvasElement.nativeElement, 'display', 'block');
160-
});
162+
})
161163
});
162164
}
163165

@@ -178,7 +180,9 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
178180

179181
if (!this.chart.config.data) {
180182
this.chart.config.data = this.computedData;
181-
this.ngZone.runOutsideAngular(() => {this.chart?.update();});
183+
this.ngZone.runOutsideAngular(() => {
184+
this.chart?.update();
185+
});
182186
return;
183187
}
184188

@@ -213,11 +217,10 @@ export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnC
213217
data: currentDataSet.data
214218
};
215219
});
216-
217-
this.ngZone.runOutsideAngular(() => {
218-
setTimeout(() => {
220+
setTimeout(() => {
221+
this.ngZone.runOutsideAngular(() => {
219222
this.chart?.update();
220223
});
221-
});
224+
})
222225
}
223226
}

projects/coreui-angular-chartjs/src/lib/chartjs.service.spec.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

projects/coreui-angular-chartjs/src/lib/chartjs.service.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

projects/coreui-angular-chartjs/src/public-api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
* Public API Surface of coreui-angular-chartjs
33
*/
44

5-
export * from './lib/chartjs.service';
65
export * from './lib/chartjs.component';
76
export * from './lib/chartjs.module';

0 commit comments

Comments
 (0)