Reputation: 2143
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
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
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