1
- import { ComponentFixture , TestBed } from '@angular/core/testing' ;
1
+ import { ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
2
2
3
3
import { ChartjsComponent } from './chartjs.component' ;
4
4
import { Chart , registerables } from 'chart.js' ;
@@ -42,30 +42,33 @@ describe('ChartjsComponent', () => {
42
42
component . type = 'line' ;
43
43
} ) ;
44
44
45
- it ( 'chart should create' , ( ) => {
45
+ it ( 'chart should create' , fakeAsync ( ( ) => {
46
46
fixture . detectChanges ( ) ;
47
+ tick ( ) ;
47
48
expect ( component ) . toBeTruthy ( ) ;
48
49
expect ( component . chart ) . toBeDefined ( ) ;
49
- } ) ;
50
+ } ) ) ;
50
51
51
- it ( 'chart should receive data' , ( ) => {
52
+ it ( 'chart should receive data' , fakeAsync ( ( ) => {
52
53
component . data = { ...data } ;
53
54
fixture . detectChanges ( ) ;
55
+ tick ( ) ;
54
56
expect ( component . chart ?. config . data . labels ?. length ) . toBe ( 7 ) ;
55
57
expect ( component . chart ?. config . data . labels ) . toEqual ( labels ) ;
56
58
expect ( component . chart ?. config . data . datasets [ 0 ] ?. data . length ) . toBe ( 7 ) ;
57
- } ) ;
59
+ } ) ) ;
58
60
59
- it ( 'chart to Base64Image' , ( ) => {
61
+ it ( 'chart to Base64Image' , fakeAsync ( ( ) => {
60
62
component . data = { ...data } ;
61
63
fixture . detectChanges ( ) ;
64
+ tick ( ) ;
62
65
const image = component . chartToBase64Image ( ) ;
63
66
expect ( image ) . toBeDefined ( ) ;
64
67
expect ( typeof image ) . toBe ( 'string' ) ;
65
68
expect ( image ) . toContain ( 'data:image/png;base64,' ) ;
66
- } ) ;
69
+ } ) ) ;
67
70
68
- it ( 'chart should update on data change' , ( ) => {
71
+ it ( 'chart should update on data change' , fakeAsync ( ( ) => {
69
72
component . data = {
70
73
...data ,
71
74
labels : [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' ] ,
@@ -75,7 +78,8 @@ describe('ChartjsComponent', () => {
75
78
]
76
79
} ;
77
80
fixture . detectChanges ( ) ;
78
- expect ( component . chart . config ?. data . labels ?. length ) . toBe ( 5 ) ;
79
- expect ( component . chart . config ?. data . datasets [ 1 ] ?. data . length ) . toBe ( 5 ) ;
80
- } ) ;
81
+ tick ( ) ;
82
+ expect ( component . chart ?. config ?. data . labels ?. length ) . toBe ( 5 ) ;
83
+ expect ( component . chart ?. config ?. data . datasets [ 1 ] ?. data . length ) . toBe ( 5 ) ;
84
+ } ) ) ;
81
85
} ) ;
0 commit comments