notAChance
notAChance

Reputation: 1430

Karma - test function with no return and which sets no scope variable

I've been reading up on karma mainly and jasmine a little and have begun to implement testing on my app.

I have the following function :

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

How on earth do I test this using karma? The expected result is a popup window opening and the relevant data being passed to my service. How do I expect this?

Upvotes: 0

Views: 934

Answers (2)

jperezov
jperezov

Reputation: 3181

Even if the function doesn't return something, it should at the minimum cause some side-effect. You need to test the side-effects.

To do this, create and inject a mock object + object method. An example would be as follows:

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]);
        }
     }
 };

Now you can expect(window.href).toEqual(url), expect(window.target).toEqual(target), expect(window.height).toEqual(400), etc.

Additionally, you need to see if sharedDataService.setIsinClickValue was invoked. If you cannot access this service within your test, you're going to have to create another mock object + method.

Upvotes: 0

Victor
Victor

Reputation: 9269

You spy on window.open and expect it to be called with the right arguments.

Upvotes: 1

Related Questions