0

I've got my mock window object and method:

var window = {
    open: function (url, target, specs) {
        var spec, specKey;
        this.href = url;
        this.target = target;
        // Parse through the spec string to grab the parameters you passed through
        var specArray = specs.split(',');
        for (specKey in specArray) {
            spec = specArray[specKey].split('=');
            this[String.trim(spec[0])] = String.trim(spec[1]);
        }
    }
};

and my test case:

describe('$scope.popup1', function () {

    it('should open a popup window when ISIN hyperlink is clicked within grid, passing ISIN object s values to shareDataService', inject(function ($window) {
        spyOn($window, 'open').and.callFake(function () {
            return true;
        });
        scope.popup1()
        expect(window.open).toHaveBeenCalled();
        expect(window.open.href).toEqual("views/Box_Ladder.html");
    })
   )
})

But I get Expected undefined to equal 'views/Box_Ladder.html'. I'm not sure how window.open.href is undefined seeing as I declared it above?

EDIT - updated code, still same error:

describe('mainCtrl', function () {

beforeEach(module('app'));
var controller, scope;
var window = {
    open: function (url, target, specs) {
console.log("function being called")
        var spec, specKey;
        this.href = url;
        this.target = target;
        // Parse through the spec string to grab the parameters you passed through
        var specArray = specs.split(',');
        for (specKey in specArray) {
            spec = specArray[specKey].split('=');
            this[String.trim(spec[0])] = String.trim(spec[1]);
        }
    }
};

beforeEach(inject(function ($controller, $window, $rootScope) {
    scope = $rootScope.$new();
    controller = $controller('mainCtrl', {$scope: scope});
    window = $window;
}));

describe('$scope.popup1', function () {

    it('should open a popup window when ISIN hyperlink is clicked within grid, passing ISIN object s values to shareDataService', inject(function ($window) {
        spyOn($window, 'open').and.callThrough()
        scope.popup1()
        expect(window.open).toHaveBeenCalled();
        expect(window.href).toEqual("views/Box_Ladder.html");
        //expect(window.location.target).toEqual("_blank");
        //expect(window.location.height).toEqual(400);
        //expect(window.width).toEqual(700);
    })
   )
})
});

And popup1() function:

$scope.popup1 = function (isinData) {
    var popup1 = window.open("views/Box_Ladder.html", "_blank",
                        "height = 400, width = 700");
    shareDataService.setIsinClickValue(isinData);
}
notAChance
  • 1,236
  • 4
  • 13
  • 35

1 Answers1

1

In open function this is referring to window object, not the function itself, so try this:

expect(window.href).toEqual("views/Box_Ladder.html");

More info about this here: How does the "this" keyword work?

Also, you have used callFake which according to the docs is:

Spies: andCallFake

By chaining the spy with andCallFake, all calls to the spy will delegate to the supplied function.

You can either register your implementation as fake function or use callThrough which is calling real implementation (or the one that you have mocked before).

You can find all details in Jasmine docs: http://jasmine.github.io/

Community
  • 1
  • 1
scareddragon
  • 415
  • 2
  • 7
  • Yeah this was how I originally had it, but same result :( – notAChance Dec 15 '15 at 12:46
  • Edited the answer - I hope now it should help you. – scareddragon Dec 15 '15 at 13:07
  • This seems to have done the trick somewhat, the popup is opening. However I still get `Expected undefined to equal 'views/Box_Ladder.html'.` in console – notAChance Dec 15 '15 at 13:33
  • Please edit your question and add current code below original question – scareddragon Dec 15 '15 at 14:03
  • Firts, with callThrough you don't pass any function, so: `spyOn($window, 'open').and.callThrough();`. Then make sure your mocked function is being called - in debug or by adding simple console.log with some message. Please also add code for `$scope.popup1()` and show how do you initialize scope itself in the test code. – scareddragon Dec 15 '15 at 14:23
  • I've now included my whole test file and popup1() method with the included changes. I see no output for my `console.log()` :S – notAChance Dec 15 '15 at 14:25
  • Look at this: `window = $window;`. I think that might be the case if the function is not being call at all. Try to add it in beforeEach like `$window.open = function (...) {...}.` – scareddragon Dec 15 '15 at 14:37