Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to trigger shortcut event from unit tests #142

Open
pjiepsma opened this issue Aug 30, 2022 · 6 comments
Open

Unable to trigger shortcut event from unit tests #142

pjiepsma opened this issue Aug 30, 2022 · 6 comments

Comments

@pjiepsma
Copy link

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

@omridevk
Copy link
Owner

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

@omridevk
Copy link
Owner

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

@pjiepsma
Copy link
Author

pjiepsma commented Aug 31, 2022

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
Copy link
Author

pjiepsma commented Oct 6, 2022

Dear omridevk,

Do you have any update regarding this issue?

Kinds regards,

Pieter

@omridevk
Copy link
Owner

omridevk commented Oct 6, 2022

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

@mayerzx
Copy link

mayerzx commented Oct 26, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants