Matheus Lima
Matheus Lima

Reputation: 2143

Karma opening window

I'm using this, to open a new tab:

$window.open($scope.jobInviteData.externalLink);

The problem is that I'm using Karma to test, and it's opening a new window. How can I make karma not open any windows?

Upvotes: 1

Views: 2706

Answers (2)

AliR
AliR

Reputation: 2085

Alternately, you can just spyOn window.open; and expect it toHaveBeenCalled. No need to create a windowMock object.

Assuming you have the $scope loaded properly in beforeEach.

Start of the suite:

var window;

beforeEach(function() {
   inject(function($injector) {
      window = $injector.get('$window');
}});

In your spec:

spyOn($window, 'open');
expect($window.open).toHaveBeenCalled();

Upvotes: 1

fiskers7
fiskers7

Reputation: 918

You would want to mock the $window service:

var windowmock;
beforeEach(module("myModule",function ($provide) {
    $provide.service('$window', function () {
        windowmock = jasmine.createSpyObj('$window', ['open']);
        return windowmock;
    });
});

Then you can also do checks on the mock in your tests to ensure it was or wasn't called.

expect(windowmock.open).toHaveBeenCalled(); // was called
expect(windowmock.open).not.toHaveBeenCalled(); // was not called
expect(windowmock.open).toHaveBeenCalledWith(somevalue); // was called with some value

Upvotes: 4

Related Questions