jeudi 2 juillet 2015

elem.click is not happening in angular unit test?

Wrote a simple directive with a click event. It looks like this:

var app = angular.module('plunker', []);

app.directive('myDirective',
  function() {
    return {
      restrict: 'E',
      template: '<span ng-click="klik();">{{value}}</span>',
      controller:function($scope)
      {
        $scope.klik= function()
        {
          $scope.value='you clicked on it!!';
        }
      }
    }
  }
);

I can't trigger the click event, why? This is the unit test:

describe('testing directive', function (){
  var scope, html, elem, compiled;

  beforeEach(function (){
    module('plunker');

    html = '<my-directive></my-directive>';

    inject(function($compile, $rootScope) {
      //create the scope.
      scope = $rootScope.$new();

      //get the element.
      elem = angular.element(html);

      //compile the view.
      compiled = $compile(elem);

      //run the view against the scope.
      compiled(scope);

      //call digest to update the view!
      scope.$digest();
    });
  });

  it('should set text to "you clicked on it!"', function (){
    elem.click();
    expect(elem.value).toBe('you clicked on this!');
  });


})

This is the error:

TypeError: elem.click is not a function

plunkr ref:http://ift.tt/1dAS1E8

Aucun commentaire:

Enregistrer un commentaire