|
1 | 1 | import { ComponentFixture, TestBed } from '@angular/core/testing';
|
2 |
| -import { Chart, registerables } from 'chart.js'; |
3 | 2 |
|
4 | 3 | import { ChartjsComponent } from './chartjs.component';
|
5 | 4 |
|
@@ -28,42 +27,61 @@ describe('ChartjsComponent', () => {
|
28 | 27 | beforeEach(async () => {
|
29 | 28 | await TestBed.configureTestingModule({
|
30 | 29 | declarations: [ChartjsComponent]
|
31 |
| - }) |
32 |
| - .compileComponents(); |
| 30 | + }).compileComponents(); |
33 | 31 |
|
34 |
| - Chart.register(...registerables); |
| 32 | + // Chart.register(...registerables); |
35 | 33 |
|
36 | 34 | fixture = TestBed.createComponent(ChartjsComponent);
|
37 | 35 | component = fixture.componentInstance;
|
38 |
| - component.data = { ...data }; |
39 |
| - fixture.detectChanges(); |
| 36 | + component.data = undefined; |
| 37 | + component.type = 'line'; |
40 | 38 | });
|
41 | 39 |
|
42 |
| - it('should create', () => { |
43 |
| - expect(component).toBeTruthy(); |
44 |
| - |
| 40 | + it('chart should create', () => { |
45 | 41 | fixture.detectChanges();
|
46 |
| - |
| 42 | + expect(component).toBeTruthy(); |
47 | 43 | expect(component.chart).toBeDefined();
|
48 | 44 | });
|
49 | 45 |
|
50 | 46 | it('chart should receive data', () => {
|
51 |
| - |
| 47 | + component.data = { ...data }; |
52 | 48 | fixture.detectChanges();
|
53 | 49 | expect(component.chart?.data.labels?.length).toBe(7);
|
54 | 50 | expect(component.chart?.data.labels).toEqual(labels);
|
55 | 51 | expect(component.chart?.data.datasets[0]?.data.length).toBe(7);
|
56 | 52 | });
|
57 | 53 |
|
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 | + }); |
69 | 87 | });
|
0 commit comments