Mocking Angular $window in unit test cases

1.1k Views Asked by At

I have been trying to unit test my angular custom service written in Typescript. The service reads a global variable defined on the Window object. I have made it promise, so that in future I can make a AJAX call to get this information. Here is my stripped down service: -

export class ProxyDetectiveService {
    public static $inject = [
        $window,
        $q
    ];

    constructor(private $window:ng.IWindowService,
                private $q:ng.IQService) {
    }

    public getProxyUserObject = ():ng.IPromise<any> => {
        this.log.debug('Proxy User Service called, to get proxy user details');

        var deferred = this.$q.defer();
        var proxyDetails = this.$window.portalObject;
        deferred.resolve(proxyDetails);

        return deferred.promise;
    };

}

My unit Test Case: -

describe('Proxy Detective Service - Unit Test Cases', () => {
    var proxyDetectiveService:any,
        $window:ng.IWindowService;

    beforeEach(() => {
        module('myApp');
    });

    beforeEach(inject(($injector:ng.auto.IInjectorService, _$window_) => {
        proxyDetectiveService = $injector.get('ProxyDetectiveService');
        _$window_ = {
            portalObject: {
                proxyUserDetails: {
                    firstName: 'testFN',
                    lastName: 'testLN'
                }
            }
        };
    }));

    it('should have proxy object defined', function () {
        var promise = proxyDetectiveService.getProxyUserObject();
        promise.then(function (response) {
            expect(response).toBeDefined();
        }).catch(function (response) {
            expect(response).toBeUndefined();
        });
    });
});

Here are my questions: -

  1. My Test case gets executed, but I dont see the mocked window object in the service?

  2. My promise then or catch clause never gets executed?

  3. Are there any better ways I can implement my service? My intention is to return a promise, in future I may use AJAX call.

1

There are 1 best solutions below

0
On BEST ANSWER

You need to use $provide to provide a mocked value in a unit test:

beforeEach(() => {
    module('myApp', ($provide) => {
      $provide.value('$window', myMockedWindowObject)
    });
});

You also need to call $rootScope.$apply() to move promises forward in unit tests.