0

I have a nested custom directive within the template of a custom directive. Something like:

customDirective definition

<custom-directive></custom-directive>

customDirective.js

angular.module('example')
   .directive('customDirective', function() {
      return {
         restrict: 'E',
         replace: true,
         transclude: true,
         templateUrl: 'directives/customDirective.html'
         link: function(scope, element, attrs) {}
      };
   });

Inside 'directives/customDirective.html

<div class="customDirective">
    <!-- do a bunch of stuff-->

    <!-- but wait, i have an image with a custom-fallback-src directive -->
    <img src="image.jpg" custom-fallback-src='newImage.jpg' />
</div>

customFallbackSrc.js directive

angular.module('example')
   .directive('customFallbackSrc', function() {
      return {
         restrict: 'A',
         link: function(scope, element, attrs) {

           // if image throws an error, use fallback image
           element.bind('error', function() {
              attrs.$set('src', attrs.customFallbackSrc);
           });

         }
      };
   });

Within my unit test for customDirective, how can I properly compile the directive to include any nested directives?

1 Answer 1

0

If you wanted full coverage I believe your best strategy would be to:

  1. Write unit tests for customDirective that test it's core functionality. It is very sparse in the example though, so how necessary this is, I'm not sure.
  2. Write unit tests for customFallbackSrc that test it's core functionality. Based on the example provided, you should be testing for two cases:
    • That the original image does not get replaced if it loads.
    • That the fallback image loads when there is an error loading the original image .
  3. Write integration tests using Protractor to cover the relationship between the two.
Sign up to request clarification or add additional context in comments.

1 Comment

This right here! There is nothing in the implementation details of either directive that sets up any sort of contract between the two. Two unit tests and an optional integration test between the two is definitely the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.