Maxim
Maxim

Reputation: 125

How can I test jquery call using jasmine?

I have the next method in angular's component:

private onTopClick() {
  $('body,html').animate({ scrollTop: 0 });
}

How I can test this with jasmine? Just to check that 'animate' method has been called.

Upvotes: 0

Views: 1074

Answers (1)

Slava Ivanov
Slava Ivanov

Reputation: 6912

You would need to use a couple of functions ...

  • First, set up spy for animate function. Probably you would want to do this for all your tests or before each, well may be just for one test; You may also want to do after the spy intercept the call, for example .and.callFake() or .and.callThrough(), etc...

    beforeEach(function() {
         spyOn($.fn, "animate");
    });
    
  • After inside your actual test(s) check if animate function was called. May looks like ...

     it("should call '$(selector).animate'", function () {
          onTopClick();
          expect($.fn.animate).toHaveBeenCalled();
     });
    

More on Jasmine Testing is over here.

Upvotes: 3

Related Questions