Skip to content

Commit 3e9432e

Browse files
committed
test(chartjs): update
1 parent 01635c2 commit 3e9432e

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed
Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { Chart, registerables } from 'chart.js';
32

43
import { ChartjsComponent } from './chartjs.component';
54

@@ -28,42 +27,61 @@ describe('ChartjsComponent', () => {
2827
beforeEach(async () => {
2928
await TestBed.configureTestingModule({
3029
declarations: [ChartjsComponent]
31-
})
32-
.compileComponents();
30+
}).compileComponents();
3331

34-
Chart.register(...registerables);
32+
// Chart.register(...registerables);
3533

3634
fixture = TestBed.createComponent(ChartjsComponent);
3735
component = fixture.componentInstance;
38-
component.data = { ...data };
39-
fixture.detectChanges();
36+
component.data = undefined;
37+
component.type = 'line';
4038
});
4139

42-
it('should create', () => {
43-
expect(component).toBeTruthy();
44-
40+
it('chart should create', () => {
4541
fixture.detectChanges();
46-
42+
expect(component).toBeTruthy();
4743
expect(component.chart).toBeDefined();
4844
});
4945

5046
it('chart should receive data', () => {
51-
47+
component.data = { ...data };
5248
fixture.detectChanges();
5349
expect(component.chart?.data.labels?.length).toBe(7);
5450
expect(component.chart?.data.labels).toEqual(labels);
5551
expect(component.chart?.data.datasets[0]?.data.length).toBe(7);
5652
});
5753

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-
// });
54+
it('chart to Base64Image', () => {
55+
component.data = { ...data };
56+
fixture.detectChanges();
57+
const image = component.chartToBase64Image();
58+
expect(image).toBeDefined();
59+
expect(typeof image).toBe('string');
60+
expect(image).toContain('data:image/png;base64,');
61+
});
62+
63+
it('chart should update on data change', () => {
64+
component.data = { ...data };
65+
// todo
66+
fixture.detectChanges();
67+
68+
component.data = {
69+
...data,
70+
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
71+
datasets: [
72+
{ ...data.datasets[0], data: [42, 88, 42, 66, 77] },
73+
{ ...data.datasets[0], data: [55, 44, 55, 66, 22] }
74+
]
75+
};
76+
// todo
77+
// @ts-ignore
78+
component.chart.data.labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May'];
79+
// @ts-ignore
80+
component.chart.data.datasets.fill({ ...data.datasets[0], data: [42, 88, 42, 66, 77] });
81+
// @ts-ignore
82+
component.chart.data.datasets.push({ ...data.datasets[0], data: [55, 44, 55, 66, 22] });
83+
fixture.detectChanges();
84+
expect(component.chart?.data.labels?.length).toBe(5);
85+
expect(component.chart?.data.datasets[1].data.length).toBe(5);
86+
});
6987
});

0 commit comments

Comments
 (0)