|
| 1 | +// @vitest-environment jsdom |
| 2 | +// The Regenerate button is a bare refresh icon beside a language list. While a transcription runs |
| 3 | +// it is greyed, but it stays focusable and hoverable so its tooltip still opens: a natively |
| 4 | +// disabled button takes neither, which is what `aria-disabled` is for. |
| 5 | + |
| 6 | +import "@testing-library/jest-dom"; |
| 7 | +import { act, cleanup, fireEvent, render, screen } from "@testing-library/react"; |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 9 | + |
| 10 | +const transcription = vi.hoisted(() => ({ |
| 11 | + status: "idle" as "idle" | "running", |
| 12 | + request: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("@/lib/ai-edition/store/transcriptionStore", async (importOriginal) => { |
| 16 | + const actual = await importOriginal<typeof import("@/lib/ai-edition/store/transcriptionStore")>(); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + useAssetTranscriptions: () => ({ |
| 20 | + "asset-1": { assetId: "asset-1", status: transcription.status }, |
| 21 | + }), |
| 22 | + useTranscriptionStore: (select: (state: { request: unknown }) => unknown) => |
| 23 | + select({ request: transcription.request }), |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +// An echoing translator: the assertions read against keys, not prose that drifts with copy edits. |
| 28 | +vi.mock("@/contexts/I18nContext", () => ({ |
| 29 | + useI18n: () => ({ locale: "en", setLocale: vi.fn() }), |
| 30 | + useScopedT: () => (key: string) => key, |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock("sonner", () => ({ toast: { success: vi.fn(), error: vi.fn(), info: vi.fn() } })); |
| 34 | + |
| 35 | +import { TooltipProvider } from "@/components/ui/tooltip"; |
| 36 | +import { assetSchema, createEmptyDocument } from "@/lib/ai-edition/schema"; |
| 37 | +import { useProjectStore } from "@/lib/ai-edition/store/projectStore"; |
| 38 | +import { MediaStage } from "./MediaStage"; |
| 39 | + |
| 40 | +class StubResizeObserver { |
| 41 | + observe = vi.fn(); |
| 42 | + unobserve = vi.fn(); |
| 43 | + disconnect = vi.fn(); |
| 44 | +} |
| 45 | + |
| 46 | +beforeEach(() => { |
| 47 | + vi.stubGlobal("ResizeObserver", StubResizeObserver); |
| 48 | + transcription.status = "idle"; |
| 49 | + transcription.request.mockClear(); |
| 50 | + const document = createEmptyDocument({ projectId: "p", title: "t" }); |
| 51 | + useProjectStore.setState({ |
| 52 | + projectId: "p", |
| 53 | + document: { |
| 54 | + ...document, |
| 55 | + assets: [assetSchema.parse({ id: "asset-1", label: "take", originalPath: "/rec/take.mp4" })], |
| 56 | + }, |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +afterEach(() => { |
| 61 | + cleanup(); |
| 62 | + vi.unstubAllGlobals(); |
| 63 | + useProjectStore.setState({ document: null }); |
| 64 | +}); |
| 65 | + |
| 66 | +async function openDetail() { |
| 67 | + render( |
| 68 | + <TooltipProvider> |
| 69 | + <MediaStage onAddToTimeline={vi.fn(async () => undefined)} /> |
| 70 | + </TooltipProvider>, |
| 71 | + ); |
| 72 | + fireEvent.click(await screen.findByRole("button", { name: /take/ })); |
| 73 | + return screen.findByRole("button", { name: "mediaStage.regenerate" }); |
| 74 | +} |
| 75 | + |
| 76 | +describe("MediaStage Regenerate button", () => { |
| 77 | + it("asks for a new transcription, and says what it redoes in its tooltip", async () => { |
| 78 | + const regenerate = await openDetail(); |
| 79 | + expect(regenerate).not.toHaveAttribute("title"); |
| 80 | + act(() => regenerate.focus()); |
| 81 | + expect((await screen.findByRole("tooltip")).textContent).toBe("mediaStage.regenerateTip"); |
| 82 | + fireEvent.click(regenerate); |
| 83 | + expect(transcription.request).toHaveBeenCalledWith("asset-1", "auto"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("stays focusable and keeps its tooltip while a transcription runs, and fires nothing", async () => { |
| 87 | + transcription.status = "running"; |
| 88 | + const regenerate = await openDetail(); |
| 89 | + |
| 90 | + // Greyed and announced as disabled, but not natively disabled. |
| 91 | + expect(regenerate).toHaveAttribute("aria-disabled", "true"); |
| 92 | + expect(regenerate).not.toBeDisabled(); |
| 93 | + expect(regenerate).toHaveStyle({ opacity: "0.6", cursor: "not-allowed" }); |
| 94 | + expect(regenerate.querySelector("svg")).toHaveClass("animate-spin"); |
| 95 | + |
| 96 | + act(() => regenerate.focus()); |
| 97 | + expect(regenerate).toHaveFocus(); |
| 98 | + expect((await screen.findByRole("tooltip")).textContent).toBe("mediaStage.regenerateTip"); |
| 99 | + |
| 100 | + fireEvent.click(regenerate); |
| 101 | + expect(transcription.request).not.toHaveBeenCalled(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments