Skip to content

Commit 44b8174

Browse files
committed
refactor(chartjs): signal inputs, host bindings, cleanup
1 parent 6cd3cd4 commit 44b8174

File tree

4 files changed

+132
-93
lines changed

4 files changed

+132
-93
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<canvas
22
#canvasElement
33
(click)="handleClick($event)"
4-
[height]="height"
5-
[id]="id"
6-
[width]="width"
4+
[height]="height() || null"
5+
[id]="id()"
6+
[width]="width() || null"
77
role="img"
88
style="display: none;"
99
>

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

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
1+
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
22

33
import { ChartjsComponent } from './chartjs.component';
44
import { Chart, registerables } from 'chart.js';
5+
import { ComponentRef } from '@angular/core';
56

67
describe('ChartjsComponent', () => {
78
let component: ChartjsComponent;
9+
let componentRef: ComponentRef<ChartjsComponent>;
810
let fixture: ComponentFixture<ChartjsComponent>;
911

1012
const colors = {
@@ -38,50 +40,61 @@ describe('ChartjsComponent', () => {
3840
beforeEach(() => {
3941
fixture = TestBed.createComponent(ChartjsComponent);
4042
component = fixture.componentInstance;
41-
component.data = undefined;
42-
component.type = 'line';
43-
component.wrapper = true;
43+
componentRef = fixture.componentRef;
44+
componentRef.setInput('type', 'line');
45+
componentRef.setInput('wrapper', true);
46+
componentRef.setInput('data', undefined);
47+
fixture.detectChanges();
4448
});
4549

4650
it('chart should create', fakeAsync(() => {
47-
fixture.detectChanges();
48-
tick();
4951
expect(component).toBeTruthy();
5052
expect(component.chart).toBeDefined();
5153
}));
5254

5355
it('chart should receive data', fakeAsync(() => {
54-
component.data = { ...data };
56+
componentRef.setInput('data', { ...data });
5557
fixture.detectChanges();
56-
tick();
58+
// tick();
5759
expect(component.chart?.config.data.labels?.length).toBe(7);
5860
expect(component.chart?.config.data.labels).toEqual(labels);
5961
expect(component.chart?.config.data.datasets[0]?.data.length).toBe(7);
6062
}));
6163

6264
it('chart to Base64Image', fakeAsync(() => {
63-
component.data = { ...data };
65+
componentRef.setInput('height', 100);
66+
componentRef.setInput('width', 100);
67+
componentRef.setInput('data', { ...data });
6468
fixture.detectChanges();
65-
tick();
69+
// tick();
6670
const image = component.chartToBase64Image();
6771
expect(image).toBeDefined();
6872
expect(typeof image).toBe('string');
6973
expect(image).toContain('data:image/png;base64,');
7074
}));
7175

7276
it('chart should update on data change', fakeAsync(() => {
73-
component.data = {
77+
componentRef.setInput('data', { ...data });
78+
fixture.detectChanges();
79+
// tick();
80+
expect(component.chart?.config.data.labels?.length).toBe(7);
81+
expect(component.chart?.config.data.labels).toEqual(labels);
82+
expect(component.chart?.config.data.datasets[0]?.data.length).toBe(7);
83+
84+
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May'];
85+
componentRef.setInput('data', {
7486
...data,
75-
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
87+
labels: [...months],
7688
datasets: [
7789
{ ...data.datasets[0], data: [42, 88, 42, 66, 77] },
7890
{ ...data.datasets[1], data: [55, 44, 55, 66, 22] }
7991
]
80-
};
92+
});
8193
fixture.detectChanges();
82-
component.chartUpdate();
83-
tick();
94+
// component.chartUpdate();
95+
// tick();
8496
expect(component.chart?.config?.data.labels?.length).toBe(5);
97+
expect(component.chart?.config.data.labels).toEqual(months);
8598
expect(component.chart?.config?.data.datasets[1]?.data.length).toBe(5);
8699
}));
87100

0 commit comments

Comments
 (0)