vendredi 23 octobre 2015

angular testing promise timeout

Can anyone tell me why this test would give me timeout telling me that done() callback needs calling?

/* jshint -W117, -W030 */
describe('boolToYesNoFilter', function () {
    'use strict';
    var mockUtils;
    var mockContentService;

    beforeEach(module('app.filters'));

    beforeEach(module('app.services'));
    beforeEach(function($q, done) {
        mockContentService = {
            getContentByGroup: function(groupName) {
                var defer = $q.defer();
                defer.resolve(['ABC']);
                done();
                return defer.promise;
            }
        };

        mockUtils = {
            getText: function(region) {
                if (region === 'spt_commoncontent_affirmative_affirmativedisplayvalue') {
                    return 'Yes';
                } else if (region === 'spt_commoncontent_negative_negativedisplayvalue') {
                    return 'No';
                } else {
                    return 'Unknown';
                }
            }
        };

        module(function ($provide) {
            $provide.value('contentService', mockContentService);
            $provide.value('utils', mockUtils);
        });
    });

    describe('when input is boolean true', function () {
        it('should return Yes', inject(function (boolToYesNoFilter) {
            // Arrange.
            var input = true;
            // Assert.
            assert.equal(boolToYesNoFilter(input), 'Yes');
        }));
    });
});

My boolToYesNoFilter calls the contentService that I have mocked out then once that resolves formats the response.

/*jshint maxcomplexity:false */
(function () {
    'use strict';

    angular.module('app.filters')
        .filter('boolToYesNo', boolToYesNo);

    boolToYesNo.$inject = ['utils', 'contentService'];

    var commonContent;

    function boolToYesNo(utils, contentService) {
        var affirmative;
        var negative;
        var unknown;

        return function (value) {
            contentService.getContentByGroup('appWideContent').then(function (data) {
                commonContent = data;

                affirmative = getText('spt_commoncontent_affirmative_affirmativedisplayvalue');
                negative = getText('spt_commoncontent_negative_negativedisplayvalue');
                unknown = getText('spt_commoncontent_unknown_unknowndisplayvalue');

                if (typeof value === 'string') {
                    switch (value.toLowerCase()) {
                        case 'true':
                            return affirmative ? affirmative : 'Yes';
                        case 'false':
                            return negative ? negative : 'No';
                        default:
                            return unknown ? unknown : 'Unknown';
                    }
                } else if (typeof value === 'boolean') {
                    switch (value) {
                        case true:
                            return affirmative ? affirmative : 'Yes';
                        case false:
                            return negative ? negative : 'No';
                    }
                } else {
                    return unknown ? unknown : 'Unknown';
                }
                //}, function() {
                //    return 'ERR';
                //});
            }, function () {
                return 'ERR';
            });
        };

        function getText(region) {
            return utils.getText(region, commonContent);
        }
    }
}());

Aucun commentaire:

Enregistrer un commentaire