I am currently working on a component that includes two methods.
How can I test the ngOnInit() method to ensure that the nameList() method is called with the students parameter?
constructor(route: ActivatedRoute, location: Location) {
}
ngOnInit() {
this.route.data
subscribe((data: { students: Students }) => {
const students: Students = data.students;
this.nameList(students);
});
}
nameList(students: Student) {
.....
}
Here is what I have come up with so far:
describe('ngOnInit', () => {
it('should extract data from route', () => {
component = fixture.componentInstance;
spyOn(component.route.data, 'subscribe').and.callFake((data: { students: Students }) => { } );
component.ngOnInit();
fixture.detectChanges();
expect(component.nameList).toHaveBeenCalledWith(students);
});
});