I'm trying to simulate a keydown event in a unit test (angular2/TypeScript). I do not always have a DebugElement available, so I'm trying to emit the event on a native element. The problem I have is how to define the keyCode when creating the KeyboardEvent. The keyCode is not defined as part of KeyboardEventInit definition, and on the KeyboardEvent itself it is only exposed as a readonly property.
Simply just adding a keyCode property (and set the obj type as ) doesn't work either.
let elm = <HTMLElement>content.nativeElement;
let ev = new KeyboardEvent('keydown', {
code: '123',
//keyCode: 345,
key: 'a',
});
elm.dispatchEvent(ev);
Any suggestions ?
Edit: According to the mdn link, keyCode is deprecated and should not be used, instead 'code' should be used. https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode