From 73e373a6bbc355475d4e89b4b0f6e5553375adef Mon Sep 17 00:00:00 2001 From: str0yka Date: Fri, 5 Jul 2024 15:57:35 +0400 Subject: [PATCH 1/2] [test]: useHotkeys --- src/hooks/useHotkeys/useHotkeys.test.ts | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/hooks/useHotkeys/useHotkeys.test.ts diff --git a/src/hooks/useHotkeys/useHotkeys.test.ts b/src/hooks/useHotkeys/useHotkeys.test.ts new file mode 100644 index 00000000..429515e0 --- /dev/null +++ b/src/hooks/useHotkeys/useHotkeys.test.ts @@ -0,0 +1,33 @@ +import { act, renderHook } from '@testing-library/react'; + +import { useHotkeys } from './useHotkeys'; + +it('Should use hotkeys', () => { + const callback = vi.fn(); + + renderHook(() => useHotkeys('a', callback, { target: document })); + + expect(callback).not.toBeCalled(); +}); + +it('The callback should be called when the hotkey is pressed', () => { + const callback = vi.fn(); + + renderHook(() => useHotkeys('a', callback, { target: document })); + + expect(callback).not.toBeCalled(); + + act(() => document.dispatchEvent(new KeyboardEvent('keydown', { key: 'a' }))); + + expect(callback).toBeCalledTimes(1); +}); + +it("Shouldn't call callback when clicking outside of target", () => { + const callback = vi.fn(); + + renderHook(() => useHotkeys('a', callback, { target: document })); + + act(() => window.dispatchEvent(new KeyboardEvent('keydown', { key: 'a' }))); + + expect(callback).not.toBeCalled(); +}); From d640af2b0f33eb60b2ae87f4bd376be7de2a32d3 Mon Sep 17 00:00:00 2001 From: str0yka Date: Fri, 5 Jul 2024 16:03:10 +0400 Subject: [PATCH 2/2] [test]: useHotkeys --- src/hooks/useHotkeys/useHotkeys.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useHotkeys/useHotkeys.test.ts b/src/hooks/useHotkeys/useHotkeys.test.ts index 429515e0..f437947d 100644 --- a/src/hooks/useHotkeys/useHotkeys.test.ts +++ b/src/hooks/useHotkeys/useHotkeys.test.ts @@ -22,7 +22,7 @@ it('The callback should be called when the hotkey is pressed', () => { expect(callback).toBeCalledTimes(1); }); -it("Shouldn't call callback when clicking outside of target", () => { +it("Shouldn't call callback when pressing outside of target", () => { const callback = vi.fn(); renderHook(() => useHotkeys('a', callback, { target: document }));