import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, expect, it, vi } from "vitest"; import { ALBUM } from "../test/fixtures.ts"; import { serve } from "../test/http.ts"; import { Album } from "./album.tsx"; afterEach(() => { vi.unstubAllGlobals(); }); it("says it is opening before the album arrives", () => { serve({ "/api/album/state": { body: ALBUM } }); render(); expect(screen.getByText("Opening the album…")).toBeInTheDocument(); }); it("says so when there is nothing to tag yet", async () => { serve({ "/api/album/state": { body: { ...ALBUM, files: [] } } }); render(); await waitFor(() => { expect(screen.getByTestId("empty")).toBeInTheDocument(); }); }); it("asks for media by position, never by name", async () => { // The server takes an int for exactly this reason: a filename from a // client is a path, and this directory holds private photographs. serve({ "/api/album/state": { body: ALBUM } }); render(); await waitFor(() => { expect(screen.getByRole("img")).toHaveAttribute("src", "/api/album/media/0"); }); }); it("shows who is already tagged on the current item", async () => { serve({ "/api/album/state": { body: ALBUM } }); render(); await waitFor(() => { expect(screen.getByLabelText("1. !alfa")).toBeChecked(); }); expect(screen.getByLabelText("2. !beta")).not.toBeChecked(); }); it("shows a half-added person by key rather than by nothing", async () => { serve({ "/api/album/state": { body: ALBUM } }); render(); await waitFor(() => { expect(screen.getByLabelText("3. p3")).toBeInTheDocument(); }); }); it("saves on the click rather than on a done button", async () => { // The tab gets closed. A "done" button is a way to lose an evening's work. const fetcher = serve({ "/api/album/state": { body: ALBUM }, "/api/album/tag": { body: { status: "ok" } }, }); render(); await userEvent.click(await screen.findByLabelText("2. !beta")); await waitFor(() => { expect(screen.getByLabelText("2. !beta")).toBeChecked(); }); const [path, init] = fetcher.mock.calls.at(-1) as [string, { body: string }]; expect(path).toBe("/api/album/tag"); expect(JSON.parse(init.body)).toEqual({ file: "one.png", keys: ["p1", "p2"], }); }); it("untags somebody who was tagged by mistake", async () => { const fetcher = serve({ "/api/album/state": { body: ALBUM }, "/api/album/tag": { body: { status: "ok" } }, }); render(); await userEvent.click(await screen.findByLabelText("1. !alfa")); await waitFor(() => { expect(screen.getByLabelText("1. !alfa")).not.toBeChecked(); }); const [, init] = fetcher.mock.calls.at(-1) as [string, { body: string }]; expect(JSON.parse(init.body)).toEqual({ file: "one.png", keys: [] }); }); it("walks the album forwards and back", async () => { serve({ "/api/album/state": { body: ALBUM } }); render(); const previous = await screen.findByRole("button", { name: "Previous" }); expect(previous).toBeDisabled(); await userEvent.click(screen.getByRole("button", { name: "Next" })); await waitFor(() => { expect(screen.getByRole("img")).toHaveAttribute("src", "/api/album/media/1"); }); expect(screen.getByRole("button", { name: "Next" })).toBeDisabled(); await userEvent.click(screen.getByRole("button", { name: "Previous" })); await waitFor(() => { expect(screen.getByRole("img")).toHaveAttribute("src", "/api/album/media/0"); }); });