javascript - Unit Testing a function inside a angular directive using jasmine -
i have directive in angular js. adds 'src' attribute image element shown below.
myapp.directive('imageonload', ['$q',function($q) { var dpromise; function loadimg(scope,target,url) { var deferred = $q.defer(); dpromise = deferred.promise; var img = new image(); // onload handler (on image, not element) img.onload = function() { img.onload = null; //target.src = url; deferred.resolve(url); }; img.onerror = function() { img.onerror = null; //target.src = url; deferred.resolve(url); }; // set source image initiate load img.src = url; return dpromise; } return { restrict: 'a', link: function(scope, element, attrs) { loadimg(scope,element,attrs.url).then(function(data){ element[0].setattribute("src",attrs.url); scope.updateloadcount(); }); } };
}]);
now wanted test whether attribute src added element ?using jasmine test case.
the spec have written
it("should set url after loading image",function(){ elm = angular.element("<img id='tnl_pic_2' class='ng-scope' url='http://tnl.sf-stg.com/assetrenderer/v2/thumbnail/snapfish/idy2vvuwysbxrowyw54tnw/a/xwjojeip4rcdo5d_fxw16a/d/qhac9fjtvzxe-z2exga-lg/time/belu_cfm0vcbwk85rnr7bg2015?height=160' imageonload=''>"); elm = compile(elm)(scope); scope.$digest(); scope.img = new image(); expect(elm.attr('src')).not.tobe(''); });
but spec fails , there no coverage directive, src attribute never added element
as beginner in jasmine , angular , please me out in how test functionality of directive. in adv !
Comments
Post a Comment