title: how to write a unit test for keydown bind directive?
body: <p>This is the code that I need to do the unit testing.</p>
  
  <pre><code>todomvc.directive('todoEscape', function () {
      var ESCAPE_KEY = 27;
      return function (scope, elem, attrs) {
          elem.bind('keydown', **function (event) {
              if (event.keyCode === ESCAPE_KEY) {
                  scope.$apply(attrs.todoEscape);**
              }
          });
  
          scope.$on('$destroy', function () {
              elem.unbind('keydown');
          });
      };
  });
  </code></pre>
  
  <p>but the above part with ** is always not covered. The coverage report shows</p>
  
  <p>Statements:75%, Branches:0, Functions:75%, Lines:75%</p>
  
  <p>And following is my testing code</p>
  
  <pre><code>'use strict';
  beforeEach(module('todomvc'));
  
  describe('todoEscape directive', function () {
    var scope, compile;
  
    beforeEach(inject(function ($rootScope, $compile) {
        scope = $rootScope.$new();
        compile = $compile;
    }));
  
    beforeEach(function() {
        var elem = angular.element('&lt;input todo-escape="escape"&gt;');
        compile(elem)(scope);
        spyOn(element, 'bind').and.returnValue('keydown');
        var event = document.createEvent("Events");
        event.initEvent('keydown');
        event.keyCode = 27;
        element.triggerHandler(event);
    });
  
    it('should call callback function when the event happens', function() {
        expect(scope.escape()).toHaveBeenCalled();
    });
  
    it('deregisters on scope $destroy', function() {
        scope.$destroy();
        expect(scope.escape()).not.toHaveBeenCalled();
    });
  });
  </code></pre>
  
  <p>I am really new to AngularJS and unit-testing. Please help.</p>