How do I test the HTML pointed to by the templateUrl of an angular directive?

Caleb Jay

I have a directive:

app/controllers/battle.js

angular.module('myModule')
  .controller('myController',
  ['$scope',
  function($scope){
    $scope.sayHello = function(){console.log('hello');};
  }])
  .directive('myDirective', function(){
    return {
      templateUrl: '../views/testView.html'
    }
  });

That directive is called as follows:

app/views/routeOne.html

<my-directive ng-controller='myController'></my-directive>

The directive templateUrl points to a file that looks like:

app/views/testView.html

<div id='testView'>Hello</div>
<div id='eggs'>Here are some eggs.</div>

How do I unit test with karma/jasmine to ensure that the div id in testView.html exists?

Methods tried:

  1. Using ngHtml2Js as per this stackoverflow answer. Reason failed: Unclear on how to access the created module, how the newly created module helps access/query the DOM elements in the testView.html file, unclear what path to use in each of the 4 places testView.html must be pointed to.

  2. Using Html2Js as per this blogpost. Reason failed: Similar to (1).

  3. Accessing the template with jquery.ajax as per this article. Reason failed: angular router prevented me from simply querying straight to the file, though I could access it directly via curl or my browser. Failed on many attempted routes. Probably not only angular router preventing this method from working.

  4. Using $compile as per the angular docs. Reason failed: $compile not found, or, directive not found, or, compiling the directive not returning anything.

Trawling across the internet for how to test an HTML file that's the template for an angular directive yields many different methods, none of which I've managed to make work. I'm happy to use any of the above methods, but I've yet to find a guide that can take me from start to finish in a holistic manner and cover enough of what's happening to actually answer my question. So: how can I test the HTML used by my directive?

Zohaib Ijaz

I would suggest solution 1 with some changes.

A small change in directive.

angular.module('myModule')
   .controller('myController',['$scope',function($scope){
         $scope.sayHello = function(){console.log('hello');};
    }])
   directive('myDirective', function(){
      return {
        templateUrl: '../views/testView.html',
        controller: 'myController'
      }
   });

In karma.conf.js

plugins: [
        // some other plugins as well
        'karma-ng-html2js-preprocessor'
    ],
preprocessors: {
     "path/to/templates/**/*.html": ["ng-html2js"]
},

ngHtml2JsPreprocessor: {
    // If your build process changes the path to your templates,
    // use stripPrefix and prependPrefix to adjust it.
    stripPrefix: "app",
    prependPrefix: "..",
    // the name of the Angular module to create
    moduleName: "templates"
},

Now in battel.spec.js //test file

describe('Directive Tests', function () {
    beforeEach(module('templates'));
    beforeEach(module('myModule'));

    var $compile, $rootScope;

    beforeEach(inject(function (_$compile_, _$rootScope_) {
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $compile = _$compile_;
        $rootScope = _$rootScope_;
   }));
  it('Some test', function(){
       var element = $compile("<my-directive></my-directive>")($rootScope);
       $rootScope.$apply();
       expect(element.html()).toContain('Here are some eggs');
  });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access the attribute of angular directive in templateUrl html content

From Dev

How do I trigger a click event in a unit test for an Angular directive

From Dev

How do I do a jasmine unit test for my angular directive that needs to test its emit?

From Dev

How can I test a directive using templateUrl in Chutzpah's headless browser

From Dev

Angular Directive templateUrl Not Working

From Dev

Angular Directive templateUrl Not Working

From Dev

AngularJS - How do I access the form defined inside a templateUrl in my directive?

From Dev

How do I use jquery in an angular directive

From Dev

Angular directive with dynamic templateUrl and Transclude

From Dev

How do you test Angular 2 directive that calls a service?

From Dev

How can I run $scope functions from a templateUrl in a directive?

From Dev

How can I run $scope functions from a templateUrl in a directive?

From Dev

Angular2+Webpack how should I output html file with templateUrl?

From Dev

Angular: How do I dynamically add ng-hide to a template that was loaded via templateUrl?

From Dev

angularjs requirejs karma directive templateUrl test fail

From Dev

Angular directive rendering HTML mark up, how can I stop it?

From Dev

How do I inject a mock dependency into an angular directive with Jasmine on Karma

From Dev

How Do I call and pass data to a Directive from a Controller Angular

From Dev

how do i insert a directive into a view from angular controller?

From Dev

How do I pass a method to an angular attribute directive?

From Dev

How do I inject a mock dependency into an angular directive with Jasmine on Karma

From Dev

How do I retrieve the unevaluated value of an attribute from a directive in Angular?

From Dev

how do i insert a directive into a view from angular controller?

From Dev

How do i append my attribute to a element in my Angular directive

From Dev

AngularJS - How do I insert compiled HTML into my directive template?

From Dev

How do I make a directive use filtered HTML attributes?

From Dev

How do I include HTML from an attribute as a token in a directive template

From Dev

karma – angular unittesting directive with a templateUrl fails, even with ng-html2js with “unexpected request”

From Java

Angular.js directive dynamic templateURL

Related Related

  1. 1

    How to access the attribute of angular directive in templateUrl html content

  2. 2

    How do I trigger a click event in a unit test for an Angular directive

  3. 3

    How do I do a jasmine unit test for my angular directive that needs to test its emit?

  4. 4

    How can I test a directive using templateUrl in Chutzpah's headless browser

  5. 5

    Angular Directive templateUrl Not Working

  6. 6

    Angular Directive templateUrl Not Working

  7. 7

    AngularJS - How do I access the form defined inside a templateUrl in my directive?

  8. 8

    How do I use jquery in an angular directive

  9. 9

    Angular directive with dynamic templateUrl and Transclude

  10. 10

    How do you test Angular 2 directive that calls a service?

  11. 11

    How can I run $scope functions from a templateUrl in a directive?

  12. 12

    How can I run $scope functions from a templateUrl in a directive?

  13. 13

    Angular2+Webpack how should I output html file with templateUrl?

  14. 14

    Angular: How do I dynamically add ng-hide to a template that was loaded via templateUrl?

  15. 15

    angularjs requirejs karma directive templateUrl test fail

  16. 16

    Angular directive rendering HTML mark up, how can I stop it?

  17. 17

    How do I inject a mock dependency into an angular directive with Jasmine on Karma

  18. 18

    How Do I call and pass data to a Directive from a Controller Angular

  19. 19

    how do i insert a directive into a view from angular controller?

  20. 20

    How do I pass a method to an angular attribute directive?

  21. 21

    How do I inject a mock dependency into an angular directive with Jasmine on Karma

  22. 22

    How do I retrieve the unevaluated value of an attribute from a directive in Angular?

  23. 23

    how do i insert a directive into a view from angular controller?

  24. 24

    How do i append my attribute to a element in my Angular directive

  25. 25

    AngularJS - How do I insert compiled HTML into my directive template?

  26. 26

    How do I make a directive use filtered HTML attributes?

  27. 27

    How do I include HTML from an attribute as a token in a directive template

  28. 28

    karma – angular unittesting directive with a templateUrl fails, even with ng-html2js with “unexpected request”

  29. 29

    Angular.js directive dynamic templateURL

HotTag

Archive