Need help writing unit test cases for click events in Angular using Jasmine & Karma
onClickCancel(): any {
this.router.navigateByUrl('login');
}
Seeking advice on how to write unit tests for click events that open Material dialog in Angular, using Jasmine & Karma. I'm a beginner in Angular development.
addNewRole(): any {
const dialogRef = this.dialog.open(AddNewRoleComponent, {
width: '450px', disableClose: true
});
dialogRef.afterClosed().subscribe(() => {
this.getRoles();
});
}
editRole(roleData): any {
const dialogRef = this.dialog.open(AddNewRoleComponent, {
width: '450px', disableClose: true, data: roleData
});
dialogRef.afterClosed().subscribe(() => {
this.getRoles();
});
}
Here is my approach to testing the opening of the dialog:
it('should open the add role component when addNewRole is clicked', () => {
component.addNewRole();
expect(dialog.open.calls.count()).toBe(1);
});
While my method works fine for other test cases, I encounter a TypeError: Cannot read property 'count' of undefined error in this scenario.
I would greatly appreciate any suggestions or guidance. Thank you!!!