Files
kandev/apps/web/components/quick-chat/quick-chat-focus.test.ts
T
2026-08-06 11:16:29 +01:00

42 lines
1.2 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { captureQuickChatLauncherFocus, restoreQuickChatLauncherFocus } from "./quick-chat-focus";
afterEach(() => {
vi.unstubAllGlobals();
document.body.replaceChildren();
});
describe("quick chat launcher focus", () => {
it("restores focus through the animation frame after closing", () => {
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
callback(0);
return 0;
});
const launcher = document.createElement("button");
document.body.append(launcher);
launcher.focus();
captureQuickChatLauncherFocus();
launcher.blur();
restoreQuickChatLauncherFocus();
expect(document.activeElement).toBe(launcher);
});
it("does not focus a launcher that was removed while the dialog was open", () => {
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
callback(0);
return 0;
});
const launcher = document.createElement("button");
document.body.append(launcher);
launcher.focus();
captureQuickChatLauncherFocus();
launcher.remove();
restoreQuickChatLauncherFocus();
expect(document.activeElement).not.toBe(launcher);
});
});