jeudi 25 février 2016

Directive with templateUrl won't compile correctly in Angular test

I'm trying to test a directive that uses a templateUrl, replace, and transclude. I've got karma-ng-html2js-preprocessor working properly I believe. I can see the template in $templateCache. The element never seems to compile properly though.

describe('Test module', function() {
  var $compile, $templateCache, scope;

  beforeEach(module('templates'));
  beforeEach(module('namespace.test'));
  // this is the exact text of the directive's templateUrl
  // it causes a nomod error if included
  // beforeEach(module('dir/test.html'));
  beforeEach(inject(function(_$compile_, _$templateCache_, $rootScope) {
    $compile = _$compile_;
    $templateCache = _$templateCache_;

    scope = $rootScope.$new();
  }));

  describe('[test] directive', function() {
    it('does something', function() {
      // this shows me the template that the test directive should be using
      console.log($templateCache.get('dir/test.html'));
      var element = elementCompile();
      // this shows me the string I entered below, uncompiled
      console.log(element);
      expect(element.attr('class')).toEqual('something');
    });
  });

  function elementCompile() {
    var element = angular.element(''
      + '<div>'
        + '<test>some text</test>'
      + '</div>'
    );
    element = $compile(element)(scope);
    scope.$digest();
    return element.children().eq(0);
  }
});

Aucun commentaire:

Enregistrer un commentaire