-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseKeyDown.test.ts
More file actions
37 lines (25 loc) · 1.01 KB
/
useKeyDown.test.ts
File metadata and controls
37 lines (25 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { renderHook } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { KeyboardEvent } from 'react';
import { describe, expect, test, vitest } from 'vitest';
import useKeyDown from './useKeyDown';
describe('useKeyDown', () => {
const setup = () => {
const user = userEvent.setup();
const callback = vitest.fn();
const { result } = renderHook(() => useKeyDown({ key: 'k', action: callback }));
document.onkeydown = (e) => result.current(e as unknown as KeyboardEvent);
expect(callback).toHaveBeenCalledTimes(0);
return { user, callback };
};
test('should call the callback when the desired key is pressed', async () => {
const { user, callback } = setup();
await user.keyboard('k');
expect(callback).toHaveBeenCalledTimes(1);
});
test('should NOT call the callback when another key is pressed', async () => {
const { user, callback } = setup();
await user.keyboard('f');
expect(callback).toHaveBeenCalledTimes(0);
});
});