-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathhooks.spec.ts
31 lines (30 loc) · 939 Bytes
/
hooks.spec.ts
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
import { renderHook } from '@testing-library/react';
import { useKeyMap } from '../hooks';
import { CodeMirrorEditor, KeyMap } from '../types';
describe('hooks', () => {
describe('useKeyMap', () => {
it('works correctly', () => {
type KeyMap = Record<string, typeof jest.fn>;
let keys: KeyMap = {};
const editor: Pick<CodeMirrorEditor, 'addKeyMap' | 'removeKeyMap'> = {
addKeyMap: jest.fn(keyMap => {
keys = { ...keys, ...(keyMap as KeyMap) };
}),
removeKeyMap: jest.fn((key: string) => {
delete keys[key];
}),
};
const callback = jest.fn();
const { unmount } = renderHook(() =>
useKeyMap(
editor as unknown as CodeMirrorEditor,
['foo', 'bar'],
callback,
),
);
expect(Object.keys(keys).length).toBe(2);
unmount();
expect(Object.keys(keys).length).toBe(0);
});
});
});