Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useKeyPress): 添加observe属性控制keyPress 全局监听 #2365

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(useKeyPress): add test
BastKakrolot committed Nov 13, 2023
commit 80e5a7c03f12d206fa6159c4823b3692a9c25279
24 changes: 24 additions & 0 deletions packages/hooks/src/useKeyPress/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -16,6 +16,30 @@ describe('useKeyPress ', () => {
unmount();
});

it('test single key by observe', async () => {
const callbackDefault = jest.fn();
const callbackFalse = jest.fn();
const callbackTrue = jest.fn();
const hook1 = renderHook(() => useKeyPress(['c'], callbackDefault));
const hook2 = renderHook(() =>
useKeyPress(['c'], callbackFalse, {
observe: false,
}),
);
const hook3 = renderHook(() =>
useKeyPress(['c'], callbackTrue, {
observe: true,
}),
);
fireEvent.keyDown(document, { key: 'c', keyCode: 67 });
expect(callbackDefault.mock.calls.length).toBe(1);
expect(callbackFalse.mock.calls.length).toBe(0);
expect(callbackTrue.mock.calls.length).toBe(1);
hook1.unmount();
hook2.unmount();
hook3.unmount();
});

it('test modifier key', async () => {
const { unmount } = renderHook(() => useKeyPress(['ctrl'], callback));
fireEvent.keyDown(document, { key: 'ctrl', keyCode: 17, ctrlKey: true });