2

For custom directive, how to mock values for offset width and scroll width? By default its taking as 0.

    link: function(scope, element, attr){
        element.bind('click',function(){
            if (element[0].offsetWidth < element[0].scrollWidth){
                console.log("Element");
            }
        });
    }
Bas Slagter
  • 9,831
  • 7
  • 47
  • 78

1 Answers1

0

In your current setup you have to compile the element in your unit test to be able to give it some width. You probably are better of by moving the logic to a controller and use the $element service. In your tests you can mock that service and provide specific values for each test.

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
  • 2
    I have compiled the element. I am able to test only the event. In test case by default, element[o].offsetWidth is taking as 0. Could you tell the way to give specified width? var element = $compile("
    Qwerty
    ")($rootScope); $rootScope.$digest(); element = $compile(element)($rootScope); element.triggerHandler('click');
    – user3619841 Oct 07 '15 at 08:27