How to test behavior in the link function of a directive

33,386

Solution 1

Basically, rather than test the link function itself, you'd test the outcome(s) of the directive programmatically. What you would do is write out the directive to a string, and use $compile to have angular process it. Then you test the output to make sure everything is wired up correctly.

Angular's source is full of good examples of how to do this... for example Angular's test of the ngRepeat directive

You can see what they're doing is setting up the directive, Changing the scope (in this case $rootScope) making sure it's $digested, and then testing the DOM it outputs to make sure everything is wired up correctly. You can also test what's in the scope, if the directive is altering that.

The test for ngClick is also pretty interesting, because it shows testing of a browser interaction and it's effect on the scope.

For sake of completeness, here's a snippet from the ngClick tests that I think sums up testing a directive fairly well:

 it('should get called on a click', inject(function($rootScope, $compile) {
   element = $compile('<div ng-click="clicked = true"></div>')($rootScope);
   $rootScope.$digest();
   expect($rootScope.clicked).toBeFalsy();

   browserTrigger(element, 'click');
   expect($rootScope.clicked).toEqual(true);
 }));

So in the case of your scope.doStuff function, I wouldn't test what it's doing, so much as I'd test whatever it's affected on the scope, and it's subsequently effected DOM elements.

Solution 2

If needed, it's possible to directly unit test the link method of a directive. See the unit tester of the angular-ice module: "testing a directive configuration"

http://bverbist.github.io/angular-ice/#/unitTester

example usage: https://github.com/bverbist/angular-ice/blob/master/app/components/icebank/bank-account-number-directive_link_test.js

In your case you can keep a reference to the scope object you pass to the directive's link method, and then you can directly test the doStuff function on that scope.

Solution 3

I resolved this issue a bit differently.

If you have a very simple link function in your directive and you don't happen to need the 3rd argument(attrs), just get rid of the link function and assign the directive a controller instead.

app.directive('loadIndicator', function() {
  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'blahblah/indicator.html',
    controller: 'LoadIndicatorController'
  };
});

just as you have the args for scope and element in a directive's link function, these 2 args can be injected into an easy-to-test controller as $scope and $element.

If you are capable of creating controllers and unit testing those controllers, then this will be really easy to do.

Share:
33,386

Related videos on Youtube

dnc253
Author by

dnc253

Updated on July 09, 2022

Comments

  • dnc253
    dnc253 about 2 years

    In some of my directives, I'm adding functions to the scope to handle logic specific for the directive. For example:

    link: function(scope, element, attrs) {
             scope.doStuff = function() {
                //do a bunch of stuff I want to test
             }        
          }
    

    How do I go about testing that function? I googled around for how test a directive, but the things I found were more about testing changes on the element. I can certainly compile my directive before each of my tests, but that would wipe out my scope every time. I want to test the function as properties in my scope changes.

    Is there any way to get a hold of the object that is returned from the directive definition? Then I could just call the link function directly and test the behavior of each of the functions defined on the scope. Is there a better way to do all this?

    I'm using Jasmine to run my tests, and I'm wanting to my scope setup in the describe functions, so I can have multiple it functions for the same scope data.

  • dnc253
    dnc253 over 11 years
    my doStuff basically just updates different parts of my model. I was hoping to have separate its for the different changes to the model it makes, but that would basically mean re setting up the model each time for each of those its Looking at that ng-repeat example, I think I just need to adjust how I was wanting to test it all, and just have one it for the whole method and just adjust the scope as I go along the different expects
  • Ben Lesh
    Ben Lesh over 11 years
    Nah, you should be able to use a beforeEach function to do the setup before each it. github.com/pivotal/jasmine/wiki/Before-and-After
  • dnc253
    dnc253 over 11 years
    I got things working, but it still seems like a lot of overhead to have to compile the directive before every single test, instead of having access to the internal object where I could test without having to compile.
  • Ben Lesh
    Ben Lesh over 11 years
    Yeah, it's perhaps some overhead... but they're tests, so generally speaking the test matters more than the overhead. It's not like your users will be running tests.
  • Eugene
    Eugene over 9 years
    @BenLesh what I first $scope = $rootScope.$new(); and pass it into compile, rather than $rootScope
  • Ben Lesh
    Ben Lesh over 9 years
    @Eugene - It shouldn't make any difference. the scope will be "fresh" in each test.
  • Eugene
    Eugene over 9 years
    @BenLesh okey. Thought so. Any suggestion on how to test directive, that has required property set?
  • Ben Lesh
    Ben Lesh over 9 years
    @Eugene You'd just have your html with the required directive around it. ... if that doesn't answer your question, submit something on SO, because it sounds like you might have a specific issue in mind.
  • britztopher
    britztopher over 9 years
    You should really only use controllers in your directives if you plan on sharing info between two directives. Thus making sort of an API between the two. At least this is according to Angular Docs: Best Practice: use a controller when you want to expose an API to other directives, otherwise, use a link function [Found at bottom of page here](docs.angularjs.org/guide/directive
  • sqlexception
    sqlexception over 9 years
    if someone could show me an easy way to unit test the link function, then I'd be happy to follow that practice
  • britztopher
    britztopher over 9 years
    You need to really just need to get the handle on the scope of the directive. I just wrote a blog on this found here Testing Linking Function
  • sqlexception
    sqlexception over 9 years
    with the plunker you made here: plnkr.co/edit/3P2bGbYNcLajpsLQFp1I?p=preview it makes sense. and it is easy. good to know. thanks
  • Nimo
    Nimo over 8 years
    I'm trying to do the same with <div ng-click="doSomething()"></div> but it doesn't work. Any idea why..?
  • Winnemucca
    Winnemucca over 8 years
    I am curious do we need to do anything different or additional for mocking out $attrs or $el?
  • Gurnard
    Gurnard almost 8 years
    this does not function for me at all. I create an element using $compile('<multi_value_field values="testdata"></multi_value_field>')(scope) with the scope created from $rootScope.$new(); When I access element.scope() it gives me the scope created using new not the isolated scope. And that is kind of what I expect. So am I missing something or should I stop persuing this avenue.
  • Gurnard
    Gurnard almost 8 years
    After some googling it is apparently the isolateScope() that you need to reference: element.isolateScope().... Found it here blog.ninja-squad.com/2015/01/27/…