material icon indicating copy to clipboard operation
material copied to clipboard

md-tab compiled() and then digest() throws an error in unit tests

Open Pyo25 opened this issue 11 years ago • 1 comments

Hi guys,

I'm using Material Design in my app and now I'm writing unit tests using Jasmine & Karma. When I try to compile and digest a md-tab component, it throws an error:

'undefined' is not a function (evaluating 'element.addClass.bind(element, 'md-tab-themed')')

Directive example: (I voluntary removed tab contents)

'use strict';

angular.module('ria.dev').directive('myDirective', function () {
  return {
    template: '<md-content style="height: 600px;">' +
      '<md-tabs md-selected="selectedIndex">' +
        '<md-tab label="hello"></md-tab>' +
      '</md-tabs>' +
  '</md-content>',
    restrict: 'E',
    controller: function($scope, $element) {
      $scope.selectedIndex = 0;
    }
  };
});

The Jasmine unit test is:

describe('MyDirective', function() {
  var $compile, $rootScope;

  beforeEach(module('ria.dev'));
  beforeEach(inject(function(_$compile_, _$rootScope_){
    $compile = _$compile_;
    $rootScope = _$rootScope_;
  }));

  describe('Fake Unit Test', function() {    
    beforeEach(function() {
      element = $compile(angular.element('<my-directive></my-directive>'))($rootScope);
      $rootScope.$digest();
      // the digest() method fails
    });

    it('should be true', function() {
      expect(true).toBe(true);
    });
  });
});

The error is: (this is the bind method that's undefined)

    TypeError: 'undefined' is not a function (evaluating 'element.addClass.bind(element, 'md-tab-themed')')
        at postLink (/xxx/bower_components/angular-material/angular-material.js:11071)
        at /xxx/bower_components/angular/angular.js:8213

And the corresponding line is:

$timeout(element.addClass.bind(element, 'md-tab-themed'), 0, false);

When I comment this line, everything goes better.

I use:

  • Angular.js v1.3.14
  • Angular-Material v0.8.3

Does anyone have an idea? :-)

PS: It works correctly in the browser.

Pyo25 avatar Mar 10 '15 09:03 Pyo25

Hey, I'm having exactly this same issue but with the md-menu. Material 1.1, Angular 1.5! anyone knows how to fix it?!

leonel-torrealba avatar Nov 16 '16 13:11 leonel-torrealba