2

I have such code inside directive :

$document.bind('keydown', function ($event) {
  if ($event && $scope.visible && $event.which === escapeKey) {
     $scope.toggle();
     $scope.$apply();
  }
});

I want to test if user click escape toggle will run. At moment I have such test:

it('should toggle window visibility to false when keypress escape', function () {
  var doc,
      $event;

  $httpBackend.when(method, url)
              .respond(template);

  $event = {
    event: 'keydown'
  };
  directive = createDirective();
  $httpBackend.flush();
  $isolateScope = directive.isolateScope();
  $isolateScope.toggle();
  $document.triggerHandler('keydown');
});

But how can I pass that certain key was pressed thought triggerHandler. Don't want to use any jQuery . Is there another way of testing this?

MarJano
  • 1,257
  • 2
  • 18
  • 39
  • You can write a vanilla JS code to trigger the event, although have a look at this answer for some specific browser issues: http://stackoverflow.com/questions/22574431/testing-keydown-events-in-jasmine-with-specific-keycode/23700583#comment37546086_23700583 – MarcoL Jul 02 '14 at 17:38
  • I try to test and I'm getting same error in Phantomjs like someone else is getting but this is still good hint and going to try to follow this. – MarJano Jul 02 '14 at 19:01
  • Have a look to the link above: I've just updated the answer with support for PhantomJS and Safari. ;) – MarcoL Jul 06 '14 at 09:06

1 Answers1

1
element.triggerHandler({type: 'keydown', which: escapeKey});
someone
  • 19
  • 1
  • 3
    Hello only a piece of code is not considered a good answer on Stack Overflow. Please consider adding an explanation of how your answer works. – Dipen Shah Jul 15 '15 at 13:37