fix keyboard input events.

This commit is contained in:
Timo
2024-08-28 16:01:20 +02:00
parent 913e21a8b5
commit 9b7e2019f7

View File

@@ -22,6 +22,12 @@ import userEvent from "@testing-library/user-event";
import { useCallViewKeyboardShortcuts } from "../src/useCallViewKeyboardShortcuts"; import { useCallViewKeyboardShortcuts } from "../src/useCallViewKeyboardShortcuts";
// Test Explanation:
// - The main objective is to test `useCallViewKeyboardShortcuts`.
// The TestComponent just wraps a button around that hook.
// - We need to set userEvent to the `{document = window.document}` since we are testing the
// `useCallViewKeyboardShortcuts` hook here. Which is listening to window.
interface TestComponentProps { interface TestComponentProps {
setMicrophoneMuted: (muted: boolean) => void; setMicrophoneMuted: (muted: boolean) => void;
onButtonClick?: () => void; onButtonClick?: () => void;
@@ -40,24 +46,33 @@ const TestComponent: FC<TestComponentProps> = ({
); );
return ( return (
<div ref={ref}> <div ref={ref}>
<Button onClick={onButtonClick}>I'm a button</Button> <Button onClick={onButtonClick}>TEST</Button>
</div> </div>
); );
}; };
test("spacebar unmutes", async () => { test("spacebar unmutes", async () => {
const user = userEvent.setup(); const user = userEvent.setup({ document: window.document });
let muted = true; let muted = true;
render(<TestComponent setMicrophoneMuted={(m) => (muted = m)} />); render(
<TestComponent
onButtonClick={() => (muted = false)}
setMicrophoneMuted={(m) => {
muted = m;
}}
/>,
);
await user.keyboard("[Space>]"); await user.keyboard("[Space>]");
expect(muted).toBe(false); expect(muted).toBe(false);
await user.keyboard("[/Space]"); await user.keyboard("[/Space]");
expect(muted).toBe(true); expect(muted).toBe(true);
}); });
test("spacebar prioritizes pressing a button", async () => { test("spacebar prioritizes pressing a button", async () => {
const user = userEvent.setup(); const user = userEvent.setup({ document: window.document });
const setMuted = vi.fn(); const setMuted = vi.fn();
const onClick = vi.fn(); const onClick = vi.fn();
render( render(
@@ -71,7 +86,7 @@ test("spacebar prioritizes pressing a button", async () => {
}); });
test("unmuting happens in place of the default action", async () => { test("unmuting happens in place of the default action", async () => {
const user = userEvent.setup(); const user = userEvent.setup({ document: window.document });
const defaultPrevented = vi.fn(); const defaultPrevented = vi.fn();
// In the real application, we mostly just want the spacebar shortcut to avoid // In the real application, we mostly just want the spacebar shortcut to avoid
// scrolling the page. But to test that here in JSDOM, we need some kind of // scrolling the page. But to test that here in JSDOM, we need some kind of