Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HariharanSubramanian committed Sep 1, 2023
1 parent bf602dc commit 6354acf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Web apps are getting closer and closer to be desktop-class applications. With th

| @ngneat/hotkeys | Angular |
| --------------- | ------- |
| 1.4.x | >=16 |
| 1.3.x | >=14 |
| 1.2.x | <=13 |

Expand Down
84 changes: 39 additions & 45 deletions projects/ngneat/hotkeys/src/lib/tests/hotkeys.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ describe('Service: Hotkeys', () => {
});

it('should delete hotkey and disposer when unsubscribed', () => {
spectator.service
.addShortcut({ keys: 'meta.a' })
.subscribe()
.unsubscribe();
spectator.service.addShortcut({ keys: 'meta.a' }).subscribe().unsubscribe();
expect(spectator.service.getHotkeys().length).toBe(0);
});

it('should listen to keydown', () => {
const spyFcn = createSpy('subscribe', e => {});
const spyFcn = createSpy('subscribe', (e) => {});
spectator.service.addShortcut({ keys: 'a' }).subscribe(spyFcn);
fakeKeyboardPress('a');
expect(spyFcn).toHaveBeenCalled();
});

it('should listen to keyup', () => {
const spyFcn = createSpy('subscribe', e => {});
const spyFcn = createSpy('subscribe', (e) => {});
spectator.service.addShortcut({ keys: 'a', trigger: 'keyup' }).subscribe(spyFcn);
fakeKeyboardPress('a', 'keyup');
expect(spyFcn).toHaveBeenCalled();
Expand Down Expand Up @@ -95,7 +92,7 @@ describe('Service: Hotkeys', () => {
trigger: 'keydown',
group: undefined,
description: undefined,
preventDefault: true
preventDefault: true,
});
});

Expand All @@ -108,7 +105,7 @@ describe('Service: Hotkeys', () => {
trigger: 'keydown',
group: 'test group',
description: 'test description',
preventDefault: false
preventDefault: false,
};

spectator.service.addShortcut(options).subscribe();
Expand All @@ -121,7 +118,7 @@ describe('Service: Hotkeys', () => {
showInHelpMenu: true,
keys: 'a',
group: 'test group',
description: 'test description'
description: 'test description',
};

spectator.service.addShortcut(options).subscribe();
Expand All @@ -131,14 +128,14 @@ describe('Service: Hotkeys', () => {
hotkeys: [
{
keys: 'a',
description: 'test description'
}
]
description: 'test description',
},
],
});
});

it('should listen to up', () => {
const spyFcn = createSpy('subscribe', e => {});
const spyFcn = createSpy('subscribe', (e) => {});
spectator.service.addShortcut({ keys: 'up' }).subscribe(spyFcn);
fakeKeyboardPress('ArrowUp');
expect(spyFcn).toHaveBeenCalled();
Expand Down Expand Up @@ -172,10 +169,7 @@ describe('Service: Sequence Hotkeys', () => {
});

it('should delete hotkey and disposer when unsubscribed', () => {
spectator.service
.addSequenceShortcut({ keys: 'g>t' })
.subscribe()
.unsubscribe();
spectator.service.addSequenceShortcut({ keys: 'g>t' }).subscribe().unsubscribe();
expect(spectator.service.getHotkeys().length).toBe(0);
});

Expand All @@ -184,10 +178,10 @@ describe('Service: Sequence Hotkeys', () => {
// * Need to space out time to prevent other test keystrokes from interfering with sequence
spectator.service.setSequenceDebounce(200);
await sleep(250);
const spyFcn = createSpy('subscribe', e => {});
const spyFcn = createSpy('subscribe', (e) => {});
spectator.service.addSequenceShortcut({ keys: 'g>z' }).subscribe(spyFcn);
await fakeKeyboardSequencePress(['g', 'z'], 'keydown', 100);
await sleep(200);
await sleep(250);
expect(spyFcn).toHaveBeenCalled();
};

Expand All @@ -199,7 +193,7 @@ describe('Service: Sequence Hotkeys', () => {
// * Need to space out time to prevent other test keystrokes from interfering with sequence
spectator.service.setSequenceDebounce(200);
await sleep(250);
const spyFcn = createSpy('subscribe', e => {});
const spyFcn = createSpy('subscribe', (e) => {});
spectator.service.addSequenceShortcut({ keys: 'g>z' }).subscribe(spyFcn);
await fakeKeyboardSequencePress(['g', 'z'], 'keydown', 250);
await sleep(200);
Expand All @@ -213,7 +207,7 @@ describe('Service: Sequence Hotkeys', () => {
const run = async () => {
// * Need to space out time to prevent other test keystrokes from interfering with sequence
await sleep(250);
const spyFcn = createSpy('subscribe', e => {});
const spyFcn = createSpy('subscribe', (e) => {});
spectator.service.addSequenceShortcut({ keys: 'g>f' }).subscribe(spyFcn);
fakeKeyboardSequencePress(['g', 'f']);
await sleep(250);
Expand All @@ -227,7 +221,7 @@ describe('Service: Sequence Hotkeys', () => {
const run = async () => {
// * Need to space out time to prevent other test keystrokes from interfering with sequence
await sleep(250);
const spyFcn = createSpy('subscribe', e => {});
const spyFcn = createSpy('subscribe', (e) => {});
spectator.service.addSequenceShortcut({ keys: 'g>g', trigger: 'keyup' }).subscribe(spyFcn);
fakeKeyboardSequencePress(['g', 'g'], 'keyup');
await sleep(250);
Expand Down Expand Up @@ -291,7 +285,7 @@ describe('Service: Sequence Hotkeys', () => {
trigger: 'keydown',
group: undefined,
description: undefined,
preventDefault: true
preventDefault: true,
});
});

Expand All @@ -304,7 +298,7 @@ describe('Service: Sequence Hotkeys', () => {
trigger: 'keydown',
group: 'test group',
description: 'test description',
preventDefault: false
preventDefault: false,
};

spectator.service.addSequenceShortcut(options).subscribe();
Expand All @@ -317,7 +311,7 @@ describe('Service: Sequence Hotkeys', () => {
showInHelpMenu: true,
keys: 'g>l',
group: 'test group',
description: 'test description'
description: 'test description',
};

spectator.service.addSequenceShortcut(options).subscribe();
Expand All @@ -327,17 +321,17 @@ describe('Service: Sequence Hotkeys', () => {
hotkeys: [
{
keys: 'g>l',
description: 'test description'
}
]
description: 'test description',
},
],
});
});

it('should listen to up', () => {
const run = async () => {
// * Need to space out time to prevent other test keystrokes from interfering with sequence
await sleep(250);
const spyFcn = createSpy('subscribe', e => {});
const spyFcn = createSpy('subscribe', (e) => {});
spectator.service.addSequenceShortcut({ keys: 'up>up' }).subscribe(spyFcn);
fakeKeyboardSequencePress(['ArrowUp', 'ArrowUp']);
await sleep(250);
Expand All @@ -346,23 +340,23 @@ describe('Service: Sequence Hotkeys', () => {

return run();
});

it('should call callback after clearing and adding sequence shortcut', () => {
const run = async () => {
// * Need to space out time to prevent other test keystrokes from interfering with sequence
await sleep(250);
const spyFcn = createSpy('subscribe', (...args) => {});
spectator.service.addSequenceShortcut({ keys: 'g>h' }).subscribe();
spectator.service.removeShortcuts('g>h');
spectator.service.addSequenceShortcut({ keys: 'g>j' }).subscribe();
spectator.service.onShortcut(spyFcn);
fakeKeyboardSequencePress(['g', 'j']);
await sleep(250);
expect(spyFcn).toHaveBeenCalled();
};
const run = async () => {
// * Need to space out time to prevent other test keystrokes from interfering with sequence
await sleep(250);
const spyFcn = createSpy('subscribe', (...args) => {});
spectator.service.addSequenceShortcut({ keys: 'g>h' }).subscribe();
spectator.service.removeShortcuts('g>h');
spectator.service.addSequenceShortcut({ keys: 'g>j' }).subscribe();
spectator.service.onShortcut(spyFcn);
fakeKeyboardSequencePress(['g', 'j']);
await sleep(250);
expect(spyFcn).toHaveBeenCalled();
};

return run();
});
return run();
});

it('should add sequence shortcut', () => {
spectator.service.addSequenceShortcut({ keys: 'g>i' }).subscribe();
Expand Down Expand Up @@ -397,5 +391,5 @@ function fakeBodyKeyboardSequencePress(keys: string[], type = 'keydown') {
}

function sleep(ms: number): Promise<unknown> {
return new Promise(resolve => setTimeout(resolve, ms));
return new Promise((resolve) => setTimeout(resolve, ms));
}

0 comments on commit 6354acf

Please sign in to comment.