|
1 |
| -import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; |
| 1 | +import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing'; |
2 | 2 |
|
3 | 3 | import { ChartjsComponent } from './chartjs.component';
|
4 | 4 | import { Chart, registerables } from 'chart.js';
|
| 5 | +import { ComponentRef } from '@angular/core'; |
5 | 6 |
|
6 | 7 | describe('ChartjsComponent', () => {
|
7 | 8 | let component: ChartjsComponent;
|
| 9 | + let componentRef: ComponentRef<ChartjsComponent>; |
8 | 10 | let fixture: ComponentFixture<ChartjsComponent>;
|
9 | 11 |
|
10 | 12 | const colors = {
|
@@ -38,50 +40,61 @@ describe('ChartjsComponent', () => {
|
38 | 40 | beforeEach(() => {
|
39 | 41 | fixture = TestBed.createComponent(ChartjsComponent);
|
40 | 42 | 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(); |
44 | 48 | });
|
45 | 49 |
|
46 | 50 | it('chart should create', fakeAsync(() => {
|
47 |
| - fixture.detectChanges(); |
48 |
| - tick(); |
49 | 51 | expect(component).toBeTruthy();
|
50 | 52 | expect(component.chart).toBeDefined();
|
51 | 53 | }));
|
52 | 54 |
|
53 | 55 | it('chart should receive data', fakeAsync(() => {
|
54 |
| - component.data = { ...data }; |
| 56 | + componentRef.setInput('data', { ...data }); |
55 | 57 | fixture.detectChanges();
|
56 |
| - tick(); |
| 58 | + // tick(); |
57 | 59 | expect(component.chart?.config.data.labels?.length).toBe(7);
|
58 | 60 | expect(component.chart?.config.data.labels).toEqual(labels);
|
59 | 61 | expect(component.chart?.config.data.datasets[0]?.data.length).toBe(7);
|
60 | 62 | }));
|
61 | 63 |
|
62 | 64 | it('chart to Base64Image', fakeAsync(() => {
|
63 |
| - component.data = { ...data }; |
| 65 | + componentRef.setInput('height', 100); |
| 66 | + componentRef.setInput('width', 100); |
| 67 | + componentRef.setInput('data', { ...data }); |
64 | 68 | fixture.detectChanges();
|
65 |
| - tick(); |
| 69 | + // tick(); |
66 | 70 | const image = component.chartToBase64Image();
|
67 | 71 | expect(image).toBeDefined();
|
68 | 72 | expect(typeof image).toBe('string');
|
69 | 73 | expect(image).toContain('data:image/png;base64,');
|
70 | 74 | }));
|
71 | 75 |
|
72 | 76 | 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', { |
74 | 86 | ...data,
|
75 |
| - labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'], |
| 87 | + labels: [...months], |
76 | 88 | datasets: [
|
77 | 89 | { ...data.datasets[0], data: [42, 88, 42, 66, 77] },
|
78 | 90 | { ...data.datasets[1], data: [55, 44, 55, 66, 22] }
|
79 | 91 | ]
|
80 |
| - }; |
| 92 | + }); |
81 | 93 | fixture.detectChanges();
|
82 |
| - component.chartUpdate(); |
83 |
| - tick(); |
| 94 | + // component.chartUpdate(); |
| 95 | + // tick(); |
84 | 96 | expect(component.chart?.config?.data.labels?.length).toBe(5);
|
| 97 | + expect(component.chart?.config.data.labels).toEqual(months); |
85 | 98 | expect(component.chart?.config?.data.datasets[1]?.data.length).toBe(5);
|
86 | 99 | }));
|
87 | 100 |
|
|
0 commit comments