How do I trigger a keyup/keydown event in an angularjs unit test?

52,239

Solution 1

You need to create an event programatically and trigger it. To do so including jQuery for unit tests is quite useful. For example, you could write a simple utility like this:

  var triggerKeyDown = function (element, keyCode) {
    var e = $.Event("keydown");
    e.which = keyCode;
    element.trigger(e);
  };

and then use it in your unit test like so:

triggerKeyDown(element, 13);

You can see this technique in action in the http://angular-ui.github.io/bootstrap/ project here: https://github.com/angular-ui/bootstrap/blob/master/src/typeahead/test/typeahead.spec.js

Disclaimer: let's be precise here: I'm not advocating using jQuery with AngularJS! I'm just saying that it is a useful DOM manipulation utility for writing tests interacting with the DOM.

To make the above code work without jQuery, change:

$.Event('keydown')

to:

angular.element.Event('keydown')

Solution 2

I had issues with using accepted answer. I found other soultion.

var e = new window.KeyboardEvent('keydown', {
  bubbles: true,
  cancelable: true,
  shiftKey: true
});

delete e.keyCode;
Object.defineProperty(e, 'keyCode', {'value': 27});

$document[0].dispatchEvent(e);

Working example can be found here

Solution 3

I got something like this working.

element.triggerHandler({type:"keydown", which:keyCode});

Solution 4

if you are using angular2, you can trigger any event by calling dispatchEvent(new Event('mousedown')) on HTMLElement instance. for example: Tested with angular 2.rc1

it('should ...', async(inject([TestComponentBuilder], (tcb:TestComponentBuilder) => {
return tcb.createAsync(TestComponent).then((fixture: ComponentFixture<any>) => {
  fixture.detectChanges();

  let com = fixture.componentInstance;

  /* query your component to select element*/
  let div:HTMLElement = fixture.nativeElement.querySelector('div');

 /* If you want to test @output you can subscribe to its event*/
  com.resizeTest.subscribe((x:any)=>{
    expect(x).toBe('someValue');
  });
  /* If you want to test some component internal you can register an event listener*/
  div.addEventListener('click',(x)=>{
    expect(x).toBe('someOtherValue');
  });
  /* if you want to trigger an event on selected HTMLElement*/
  div.dispatchEvent(new Event('mousedown'));
  /* For click events you can use this short form*/
  div.click();

  fixture.detectChanges();
});

Solution 5

I recently wanted to test this HostListener on a component (Angular 2):

  @HostListener('keydown.esc') onEsc() {
    this.componentCloseFn();
  };

And after searching for some time, this is working:

..
nativeElement.dispatchEvent(new KeyboardEvent('keydown', {'key': 'Escape'}));
...
Share:
52,239
Chandra
Author by

Chandra

Updated on December 02, 2020

Comments

  • Chandra
    Chandra over 3 years

    I want to unit test a directive that emulates a placeholder, where the input value is cleared only on keyup/down events.

  • Cornelius
    Cornelius over 10 years
    The downside is that jQuery would easily replace the jQueryLite coming with angular. As there are actual functional differences, you are essentially testing a (slightly) different system by including it in your unit tests.
  • Johnus
    Johnus almost 10 years
    Is angular.element.Event still available? I'm getting undefined, and I can't see anything about it in the docs. docs.angularjs.org/api/ng/function/angular.element
  • Markus Peröbner
    Markus Peröbner almost 9 years
    I used this approache so +1 but it seems that it's not working within PhantomJS.
  • Maciej Dzikowicki
    Maciej Dzikowicki almost 9 years
    I'm using Chrome in my karma configuration, because PhantomJS doesn't support bind
  • akcasoy
    akcasoy over 6 years
    and how does this answer the question? How can you trigger the keyup/keydown events with dispatchEvent? new Event('keydown.esc') does not work
  • Majesty
    Majesty about 6 years
    Works for me as well
  • MrHIDEn
    MrHIDEn about 4 years
    triggerHandler is not a function, Agular 8.