angularjsjasmineintercom.js

Jasmine SpyOn Window Function in AngularJS Application


I've created a function as so upon route change:

window.intercom = function(action, msg) {
  return window.Intercom(action, msg);
};

Am trying to spy on this function but don't understand how. I followed the advice in this SO post.

If I use this:

beforeEach(inject(function($compile, $rootScope, $q, _$location_, $routeParams) {
  element = angular.element("<add-box></add-box>");
  $compile(element)($rootScope);
}));

it("should cancel adding a box!", function() {
  spyOn(window, 'intercom')
});

I get an error:

intercom() method does not exist

So I tried this:

it("should cancel adding a box!", function() {
  var intercom = jasmine.createSpy();
});

Which says:

TypeError: 'undefined' is not a function (evaluating 'window.intercom('hide')')

How can I get this to work? I'm sure it's simple, I'm just new to Jasmine.


Solution

  • How about in the beforeEach or better in a before function you do something like this:

    window.intercom = jasmine.createSpy();
    

    That'll get you an intercom function on the window.