AngularJS directive test using ng-html2js

1k Views Asked by At

there are a lot of questions about the ng-html2js plugin but unfortunately all answers didn't help me to solve y issue.

I followed the official installation guide and the example https://github.com/vojtajina/ng-directive-testing/blob/master/test/tabsSpec.js.

My goal is to test a directive that has a templateUrl property, this is my config

karma.conf.js

files: [
  ... // lots of studd here

  'app/views/**/*.html' // templates are all here
],

[...] // other karma configs here

preprocessors: {
  'app/views/**/*.html': ['ng-html2js']
},

The directive I would like to test it is really basic

'use strict';

angular.module('myAwesomeApp').directive('rzMenu', function () {
    return {
      templateUrl: 'views/directives/rzmenu.html',
      restrict: 'E'
    };   
});

The unit test file for this

'use strict';

describe('Directive: rzmenu', function () {

  // load the directive's module
  beforeEach(module('myAwesomeApp'));
  beforeEach(module('app/views/directives/rzmenu.html'));

  var element,
    scope;

  beforeEach(inject(function ($rootScope, $compile) {
    element = angular.element('<rzmenu></rzmenu>');
    scope = $rootScope.$new();
    element = $compile(element)(scope);
    scope.$digest();
  }));

  it('should have content', inject(function () {
    //scope.$digest();
    var content = element.text();
    expect(content).toBe('');
  }));
});

Look at the 'should have content' test, shouldn't be the content the same as the template?

Why I get and empty string?

Thanks in advance

1

There are 1 best solutions below

2
On BEST ANSWER

Nevermind, there was 2 error that I solved:

1) cacheIds must match templateUrl property, so

stripPrefix: 'app/'

was needed in the configuration as well as call the module without the app/ prefix within the test.

beforeEach(module('views/directives/rzmenu.html'));

2) directive name is camelCase so the instance call was wrong, here below the correct one

element = angular.element('<rz-menu></rz-menu>');

:)