Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
   **1. How to write unit test case for below click event in angular using Jasmin & Karma.**

     onClickCancel(): any {
        this.router.navigateByUrl('login');
      }

**2. How to write unit test case for below click event which open material dialog in angular using Jasmin & Karma. I am new to Angular**

  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();
    });
  }

**Below was my approach for unit testing of opening dialog** 

  it('should open the add role component on click of addNewRole', () => {
    component.addNewRole();
    expect(dialog.open.calls.count()).toBe(1);
  });


What I have tried:

**Below was my approach for unit testing of opening dialog** 

  it('should open the add role component on click of addNewRole', () => {
    component.addNewRole();
    expect(dialog.open.calls.count()).toBe(1);
  });


My approach of opening dialog works properly for other component test cases. But here - TypeError: Cannot read property 'count' of undefined error is thrown.
Posted
Updated 25-Aug-21 2:15am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900