|
| 1 | +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 2 | +import { act } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +import type { ThreadListV2ListItem } from "../threads/threadListV2"; |
| 7 | +import { |
| 8 | + dispatchHardwareKeyboardCommand, |
| 9 | + getHardwareKeyboardCommandRegistrationVersion, |
| 10 | +} from "./hardwareKeyboardCommands"; |
| 11 | +import { useThreadJumpShortcuts } from "./threadKeyboardShortcuts"; |
| 12 | + |
| 13 | +function threadItem(id: string): ThreadListV2ListItem { |
| 14 | + const thread = { environmentId: "environment-1", id } as unknown as EnvironmentThreadShell; |
| 15 | + return { |
| 16 | + type: "v2-thread", |
| 17 | + key: id, |
| 18 | + item: { thread, variant: "card", snoozed: false, pinned: false, isLast: false, wokeAt: null }, |
| 19 | + snoozeWakeLabelText: undefined, |
| 20 | + timeLabel: "", |
| 21 | + snoozePresetMinute: undefined, |
| 22 | + showTrailingDivider: false, |
| 23 | + hasQueuedMessages: false, |
| 24 | + canMoveUp: false, |
| 25 | + canMoveDown: false, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +function Probe(props: { |
| 30 | + readonly items: ReadonlyArray<ThreadListV2ListItem>; |
| 31 | + readonly onSelectThread: (thread: EnvironmentThreadShell) => void; |
| 32 | +}) { |
| 33 | + useThreadJumpShortcuts(props.items, props.onSelectThread); |
| 34 | + return null; |
| 35 | +} |
| 36 | + |
| 37 | +let root: Root | null = null; |
| 38 | + |
| 39 | +afterEach(() => { |
| 40 | + act(() => root?.unmount()); |
| 41 | + root = null; |
| 42 | +}); |
| 43 | + |
| 44 | +describe("useThreadJumpShortcuts", () => { |
| 45 | + it("registers once and jumps within the latest list", () => { |
| 46 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 47 | + root = createRoot(document.createElement("div")); |
| 48 | + act(() => root!.render(<Probe items={[threadItem("first")]} onSelectThread={vi.fn()} />)); |
| 49 | + const registrationVersion = getHardwareKeyboardCommandRegistrationVersion(); |
| 50 | + |
| 51 | + const onSelectThread = vi.fn(); |
| 52 | + for (const id of ["second", "third"]) { |
| 53 | + act(() => root!.render(<Probe items={[threadItem(id)]} onSelectThread={onSelectThread} />)); |
| 54 | + } |
| 55 | + |
| 56 | + expect(getHardwareKeyboardCommandRegistrationVersion()).toBe(registrationVersion); |
| 57 | + expect(dispatchHardwareKeyboardCommand("thread.jump.1")).toBe(true); |
| 58 | + expect(onSelectThread).toHaveBeenCalledExactlyOnceWith( |
| 59 | + expect.objectContaining({ id: "third" }), |
| 60 | + ); |
| 61 | + }); |
| 62 | +}); |
0 commit comments