How to unit test alert in angular 2 by using Jasmine?

Viewed 3627

I have tried to write a test case for alert by using Jasmine in angular 2. But any how my test case is giving an error and even though I am not sure which I have written correct approach or not. If any idea please help me out.

This is my test case:

it('checking showscheduledrequest flow an alert is called',() =>{
      let component = fixture.componentInstance;
      component['ou'] = 'd';
      component['sen'] = 'ddsd';
      var oldalert = alert;
      oldalert = jasmine.createSpy();
      component.handleActionChane('showscheduledrequest');
      fixture.autoDetectChanges();
      expect(alert).toHaveBeenCalledWith('This is not a valid request');
    });

1 Answers

You could try to spy on window and then check if alert has been called with excepted value, for instance:

    it("should call alert", () => {
     spyOn(window, "alert");
     //your code
     expect(window.alert).toHaveBeenCalledWith("expected message");
  });  
Related