I'm new to unit testing and hope someone help me. I have a angular component called StudentComponent and it has following.
public getAllStudents(){
this.apollo.query({
query: GETALL_STUDENTS,
}).subscribe((result: any) => {
this.students = result?.data?.getAllStudents;
this.students.forEach(element => {
element.Age = this.CalculateAge(element.DateOfBirth);
element.DateOfBirthString = this.getDateFormat(element.DateOfBirth);
});
});
}
public CalculateAge(dateOfBirth: any): number {
let age = 0;
if (dateOfBirth) {
var timeDiff = Math.abs(Date.now() - new Date(dateOfBirth).getTime());
age = Math.floor(timeDiff / (1000 * 3600 * 24) / 365.25);
}
return age;
}
getDateFormat(date: Date): string{
let dateString = ""
if(date){
dateString = formatDate(date, 'yyyy/MM/dd','en_US').toString();
}
return dateString;
}
I wrote the test case like following
describe('getAllStudents', () => {
it('makes expected calls', () => {
spyOn(component, 'getDateFormat').and.callThrough();
spyOn(component, 'CalculateAge').and.callThrough();
spyOn(apollo, 'query').and.callThrough();
component.getAllStudents();
expect(apollo.query).toHaveBeenCalled();
expect(component.getDateFormat).toHaveBeenCalled();
expect(component.CalculateAge).toHaveBeenCalled();
});
});
this gave me an error. 'Expected spy getDateFormat to have been called.' Could anyone please guide me to resolve this?