While writing unit tests for components, I encountered an error in the terminal when running the tests. The error message displayed was: TypeError: Cannot read property 'labels' of undefined. There were a total of 3 function errors that occurred during the test case.
private checkRating(): boolean {
if ((Object.keys(this.ratingData.labels).length) > 0) {
return true;
} else {
return false;
}
}
ngAfterContentChecked(): void {
this.handlerBallonBar();
}
private handlerBallonBar() {
return this.ratingOptions = {
title: {
display: false
},
legend: {
position: 'right',
display: false
},
scales: {
xAxes: [
{
ticks: {
beginAtZero: true,
},
display: false,
}
],
yAxes: [
{ display: this.checkRating() }
]
},
responsive: true,
plugins: {
datalabels: {
labels: {
value: {
color: '#ffffff'
}
},
font: {
size: '12'
}
}
}
}
}
This section defines a specific test case.
describe('BalloonComponent', () => {
let component: BalloonComponent;
let fixture: ComponentFixture<BalloonComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [BalloonComponent]
}).overrideTemplate(BalloonComponent, '');
}));
beforeEach(() => {
fixture = TestBed.createComponent(BalloonComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy()
});
The following is an error: View image description here