Add tests

This commit is contained in:
Half-Shot 2024-11-14 17:21:27 +00:00
parent a081ac8847
commit a7221b3ed7
2 changed files with 35 additions and 11 deletions

View File

@ -12,19 +12,24 @@ import { Button } from "@vector-im/compound-web";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { useCallViewKeyboardShortcuts } from "../src/useCallViewKeyboardShortcuts"; import { useCallViewKeyboardShortcuts } from "../src/useCallViewKeyboardShortcuts";
import { ReactionOption, ReactionSet, ReactionsRowSize } from "./reactions";
// Test Explanation: // Test Explanation:
// - The main objective is to test `useCallViewKeyboardShortcuts`. // - The main objective is to test `useCallViewKeyboardShortcuts`.
// The TestComponent just wraps a button around that hook. // The TestComponent just wraps a button around that hook.
interface TestComponentProps { interface TestComponentProps {
setMicrophoneMuted: (muted: boolean) => void; setMicrophoneMuted?: (muted: boolean) => void;
onButtonClick?: () => void; onButtonClick?: () => void;
sendReaction?: () => void;
toggleHandRaised?: () => void;
} }
const TestComponent: FC<TestComponentProps> = ({ const TestComponent: FC<TestComponentProps> = ({
setMicrophoneMuted, setMicrophoneMuted = (): void => {},
onButtonClick = (): void => {}, onButtonClick = (): void => {},
sendReaction = (reaction: ReactionOption): void => {},
toggleHandRaised = (): void => {},
}) => { }) => {
const ref = useRef<HTMLDivElement | null>(null); const ref = useRef<HTMLDivElement | null>(null);
useCallViewKeyboardShortcuts( useCallViewKeyboardShortcuts(
@ -32,6 +37,8 @@ const TestComponent: FC<TestComponentProps> = ({
() => {}, () => {},
() => {}, () => {},
setMicrophoneMuted, setMicrophoneMuted,
sendReaction,
toggleHandRaised,
); );
return ( return (
<div ref={ref}> <div ref={ref}>
@ -74,6 +81,28 @@ test("spacebar prioritizes pressing a button", async () => {
expect(onClick).toBeCalled(); expect(onClick).toBeCalled();
}); });
test("reactions can be sent via keyboard presses", async () => {
const user = userEvent.setup();
const sendReaction = vi.fn();
render(<TestComponent sendReaction={sendReaction} />);
for (let index = 1; index <= ReactionsRowSize; index++) {
await user.keyboard(index.toString());
expect(sendReaction).toHaveBeenNthCalledWith(index, ReactionSet[index - 1]);
}
});
test("raised hand can be sent via keyboard presses", async () => {
const user = userEvent.setup();
const toggleHandRaised = vi.fn();
render(<TestComponent toggleHandRaised={toggleHandRaised} />);
await user.keyboard("h");
expect(toggleHandRaised).toHaveBeenCalledOnce();
});
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();
const defaultPrevented = vi.fn(); const defaultPrevented = vi.fn();

View File

@ -8,7 +8,7 @@ Please see LICENSE in the repository root for full details.
import { RefObject, useCallback, useMemo, useRef } from "react"; import { RefObject, useCallback, useMemo, useRef } from "react";
import { useEventTarget } from "./useEvents"; import { useEventTarget } from "./useEvents";
import { ReactionOption, ReactionSet } from "./reactions"; import { ReactionOption, ReactionSet, ReactionsRowSize } from "./reactions";
/** /**
* Determines whether focus is in the same part of the tree as the given * Determines whether focus is in the same part of the tree as the given
@ -19,14 +19,9 @@ const mayReceiveKeyEvents = (e: HTMLElement): boolean => {
return focusedElement !== null && focusedElement.contains(e); return focusedElement !== null && focusedElement.contains(e);
}; };
const KeyToReactionMap: Record<string, ReactionOption> = { const KeyToReactionMap: Record<string, ReactionOption> = Object.fromEntries(
["1"]: ReactionSet[0], ReactionSet.slice(0, ReactionsRowSize).map((r, i) => [(i + 1).toString(), r]),
["2"]: ReactionSet[1], );
["3"]: ReactionSet[2],
["4"]: ReactionSet[3],
["5"]: ReactionSet[4],
["6"]: ReactionSet[5],
};
export function useCallViewKeyboardShortcuts( export function useCallViewKeyboardShortcuts(
focusElement: RefObject<HTMLElement | null>, focusElement: RefObject<HTMLElement | null>,