Files
kandev/apps/web/components/quick-chat/quick-chat-provider-focus.test.tsx
T
2026-09-15 19:05:40 +01:00

86 lines
2.8 KiB
TypeScript

import { cleanup, render } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
const mockUnsubscribeSession = vi.hoisted(() => vi.fn());
const mockSubscribeSession = vi.hoisted(() => vi.fn(() => mockUnsubscribeSession));
const providerState = vi.hoisted(() => ({
quickChat: {
isOpen: true,
sessions: [{ sessionId: "chat-1", workspaceId: "ws-1" }],
terminalTabs: [],
activeKind: "conversation" as const,
activeSessionId: "chat-1",
activeTerminalTabId: null,
},
workspaces: { activeId: "ws-1" },
connection: { status: "disconnected" as "connected" | "disconnected" },
}));
vi.mock("@/components/state-provider", () => ({
useAppStore: (selector: (state: typeof providerState) => unknown) => selector(providerState),
}));
vi.mock("@/hooks/domains/settings/use-settings-data", () => ({ useSettingsData: vi.fn() }));
vi.mock("@/hooks/use-quick-chat-resync", () => ({ useQuickChatResync: vi.fn() }));
vi.mock("@/lib/ws/connection", () => ({
getWebSocketClient: () => ({ subscribeSession: mockSubscribeSession }),
}));
vi.mock("./quick-chat-modal", () => ({ QuickChatModal: () => null }));
import { captureQuickChatLauncherFocus } from "./quick-chat-focus";
import { QuickChatProvider } from "./quick-chat-provider";
afterEach(() => {
cleanup();
providerState.quickChat.isOpen = true;
providerState.connection.status = "disconnected";
mockSubscribeSession.mockClear();
mockUnsubscribeSession.mockClear();
vi.unstubAllGlobals();
});
describe("QuickChatProvider focus contract", () => {
it("restores focus to the launcher after the shared surface closes", () => {
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
callback(0);
return 0;
});
const { rerender } = render(
<QuickChatProvider>
<button type="button">launch</button>
</QuickChatProvider>,
);
const launcher = document.querySelector("button") as HTMLButtonElement;
launcher.focus();
captureQuickChatLauncherFocus();
providerState.quickChat.isOpen = false;
rerender(
<QuickChatProvider>
<button type="button">launch</button>
</QuickChatProvider>,
);
expect(document.activeElement).toBe(launcher);
});
it("keeps Quick Chat sessions subscribed after the modal closes", () => {
providerState.connection.status = "connected";
const { rerender } = render(
<QuickChatProvider>
<button type="button">launch</button>
</QuickChatProvider>,
);
expect(mockSubscribeSession).toHaveBeenCalledWith("chat-1");
providerState.quickChat.isOpen = false;
rerender(
<QuickChatProvider>
<button type="button">launch</button>
</QuickChatProvider>,
);
expect(mockUnsubscribeSession).not.toHaveBeenCalled();
});
});