ng-keyboard-shortcuts
ng-keyboard-shortcuts copied to clipboard
Unable to trigger shortcut event from unit tests
Describe the bug From the angular .spec file I'm not able to test if the shortcuts are working. Below is an example of the code. They keyboard event does not seem to be trigged.
I also ran the same test with a clone of this repo https://www.npmjs.com/package/ng-hotkeys, and there the unit test work as expected.
Did I make any mistake or am I running the wrong version?
<ng-keyboard-shortcuts [shortcuts]="shortcuts"></ng-keyboard-shortcuts>
private getKeyboardShortcuts(): ShortInput[] {
return [
{
key: 'cmd + shift + n',
preventDefault: true,
label: 'Help',
description: 'Create new appointment',
command: (output: ShortcutEventOutput) => this.openModal(AgendaModalType.AFSPRAAK),
},
];
}
it('Should dispatch a new appointment modal event on key combination "ctrl + shift + n"', async () => {
const keyN = new KeyboardEvent('keydown', {
key: 'n',
ctrlKey: true,
shiftKey: true,
});
await fixture.whenStable();
document.dispatchEvent(keyN);
expect(ngbModalSpy.open).toHaveBeenCalled();
});
"ng-keyboard-shortcuts": "^13.0.8",
"@angular/animations": "^13.2.4", "@angular/cdk": "^13.2.4", "@angular/common": "^13.2.4", "@angular/compiler": "^13.2.4", "@angular/core": "^13.2.4", "@angular/forms": "^13.2.4", "@angular/localize": "^13.2.4", "@angular/platform-browser": "^13.2.4", "@angular/platform-browser-dynamic": "^13.2.4",
I actually never tested it, can you please provide a minimal reproduction repository where I can debug it? Thanks a bunch.
This is the correct version by the way, the clone repo was created due to lack of activity on my side, but I am back in business
Thanks you for the quick response, im quite a novice so I couldnt quit figure out to create a reproduction enviroment from our project, so I just cloned and cleaned it.
Here is the link, I also added the cloned lib ng-hotkeys in there, to show the same test works for one but not the other.
Thank you for you time, I hope you can figure it out.
Kinds regards,
Pieter
https://github.com/pjiepsma/unit-test-ng-keyboard-shortcut
Dear omridevk,
Do you have any update regarding this issue?
Kinds regards,
Pieter
sorry, for the delay, I hope i'll find a time this weekend.
+1, same issue, when I try to test the command callback