lundi 5 octobre 2015

Mock window properties in javascrip jasmine tests

I have an object that require JSON. The object populates an isSupported function and i'm trying to write a test for it.

(function() {
    'use strict';

    this.myFactory = function() {};

    this.myFactory.isSupported = function() {
        return !!this.JSON;
    };
}).call(window);

If i just set window.JSON = false for a specific test than all other tests obviously fail because they need that.

describe('Test IsSupported', function() {

    it('Should return false if no JSON', function() {
        window.JSON = undefined;
        expect(myFactory.isSupported()).toEqual(false);
    });

});

How can I test this function in a context of a mock window ?

Aucun commentaire:

Enregistrer un commentaire