Angular, Test Code in Angular Translated

I am desperately trying to write a unit test for code in the angular translation service "then" part. I created a simple testing service:

var myService = function (sweetAlert, $translate) {

    function execute() {

        $translate(['HEADER', 'TEXT']).then(function (translations) {

            sweetAlert.swal({
                title:              translations.HEADER,
                text:               translations.TEXT,
                type:               "warning",
                showCancelButton:   false,
                confirmButtonColor: "#DD6B55",
                confirmButtonText:  "OK",
                closeOnConfirm:     true

            });
        });
    }

    return {
        execute: execute
    };
};

var module = angular.module('app');
module.service('myService', myService);

      

So my job is to call the function swal

. After reading a lot of posts on how to test jasmine promises, this was the best I was able to build, but it won't do the job (yes, I know, it is incomplete):

describe('myService test : ', function () {

var mocSweetAlert;
var mocTranslate;

beforeEach(function(){
    module('app');
});

beforeEach(function () {
    module(function($provide){
        $provide.service('sweetAlert', function(){
            this.swal = jasmine.createSpy('swal');
        });

        $provide.service('translate', function(){
            return {
                then: function(callback){
                    return callback([{
                        HEADER: 'Header',
                        TEXT: 'This is a text'
                    }]);
                }
            }
        });
    });
});

beforeEach(inject(function(sweetAlert, traslate){
    mocSweetAlert = sweetAlert;
    mocTranslate = traslate
}));

it('Calls swal function...', inject(function (myService) {

    myService.execute();
    expect(mocSweetAlert.swal).toHaveBeenCalled();

}));

      

});

+3


source to share





All Articles