1- import { ComponentFixture , TestBed } from '@angular/core/testing' ;
1+ import { ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
22
33import { ChartjsComponent } from './chartjs.component' ;
44import { Chart , registerables } from 'chart.js' ;
@@ -42,30 +42,33 @@ describe('ChartjsComponent', () => {
4242 component . type = 'line' ;
4343 } ) ;
4444
45- it ( 'chart should create' , ( ) => {
45+ it ( 'chart should create' , fakeAsync ( ( ) => {
4646 fixture . detectChanges ( ) ;
47+ tick ( ) ;
4748 expect ( component ) . toBeTruthy ( ) ;
4849 expect ( component . chart ) . toBeDefined ( ) ;
49- } ) ;
50+ } ) ) ;
5051
51- it ( 'chart should receive data' , ( ) => {
52+ it ( 'chart should receive data' , fakeAsync ( ( ) => {
5253 component . data = { ...data } ;
5354 fixture . detectChanges ( ) ;
55+ tick ( ) ;
5456 expect ( component . chart ?. config . data . labels ?. length ) . toBe ( 7 ) ;
5557 expect ( component . chart ?. config . data . labels ) . toEqual ( labels ) ;
5658 expect ( component . chart ?. config . data . datasets [ 0 ] ?. data . length ) . toBe ( 7 ) ;
57- } ) ;
59+ } ) ) ;
5860
59- it ( 'chart to Base64Image' , ( ) => {
61+ it ( 'chart to Base64Image' , fakeAsync ( ( ) => {
6062 component . data = { ...data } ;
6163 fixture . detectChanges ( ) ;
64+ tick ( ) ;
6265 const image = component . chartToBase64Image ( ) ;
6366 expect ( image ) . toBeDefined ( ) ;
6467 expect ( typeof image ) . toBe ( 'string' ) ;
6568 expect ( image ) . toContain ( 'data:image/png;base64,' ) ;
66- } ) ;
69+ } ) ) ;
6770
68- it ( 'chart should update on data change' , ( ) => {
71+ it ( 'chart should update on data change' , fakeAsync ( ( ) => {
6972 component . data = {
7073 ...data ,
7174 labels : [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' ] ,
@@ -75,7 +78,8 @@ describe('ChartjsComponent', () => {
7578 ]
7679 } ;
7780 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+ } ) ) ;
8185} ) ;
0 commit comments