ng-keyboard-shortcuts icon indicating copy to clipboard operation
ng-keyboard-shortcuts copied to clipboard

Unable to trigger shortcut event from unit tests

Open pjiepsma opened this issue 2 years ago • 6 comments

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",

pjiepsma avatar Aug 30 '22 10:08 pjiepsma

I actually never tested it, can you please provide a minimal reproduction repository where I can debug it? Thanks a bunch.

omridevk avatar Aug 30 '22 12:08 omridevk

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

omridevk avatar Aug 30 '22 12:08 omridevk

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

pjiepsma avatar Aug 31 '22 18:08 pjiepsma

Dear omridevk,

Do you have any update regarding this issue?

Kinds regards,

Pieter

pjiepsma avatar Oct 06 '22 08:10 pjiepsma

sorry, for the delay, I hope i'll find a time this weekend.

omridevk avatar Oct 06 '22 19:10 omridevk

+1, same issue, when I try to test the command callback

mayerzx avatar Oct 26 '22 06:10 mayerzx