lundi 30 mai 2016

Testing element.bind('error')

Trying to figure out how to test this directive, but when I log out element I just keep getting this.

Object{0: <img ng-src="img/img.jpg" err-src="img/err.png" src="img/img.jpg">, length: 1}

How would I test this with different http codes to make sure my directive is working properly?

directive.js

.directive('errSrc', function() {
  return {
    link: function(scope, element, attrs) {

      scope.$watch(function() {
        return attrs.ngSrc;
      }, function (value) {
        if (!value) {
          element.attr('src', attrs.errSrc);
        }
      });

      element.bind('error', function() {
        element.attr('src', attrs.errSrc);
      });
    }
  };
})

test.js

describe("Directives", function () {


    describe("err-src Directive", function () {


        var element, $scope;
        beforeEach(module('app'));

        beforeEach(inject(function ($compile, _$rootScope_, _$httpBackend_) {

            $scope = _$rootScope_.$new();
            $httpBackend = _$httpBackend_;

            element = angular.element('<img ng-src="img/img.jpg" err-src="img/err.png">');

            element = $compile(element)($scope);

            $scope.$digest();
            console.log(element);

        }));

        it("Should replace src with err icon if 404", function () {
            $httpBackend
                    .when('GET', 'img/img.jpg')
                    .respond(404);
        });
        it("Should replace src ng-src if 200", function () {
            $httpBackend
                    .when('GET', 'img/img.jpg')
                    .respond(200);


        });
    });
});

Aucun commentaire:

Enregistrer un commentaire