diff --git a/src/hooks/useHotkeys/useHotkeys.test.ts b/src/hooks/useHotkeys/useHotkeys.test.ts new file mode 100644 index 00000000..f437947d --- /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 pressing 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(); +});