69 lines
4.7 KiB
TypeScript
69 lines
4.7 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
import { afterEach, expect, it, vi } from "vitest";
|
|
import { SuggestionReader } from "./suggestion-reader";
|
|
const id = "100000000000000009";
|
|
const original = { id, author: { id: "100000000000000003", name: "Builder" }, content: "<script>unsafe()</script> Please build stations.", createdAt: "2026-01-01T00:00:00Z", editedAt: null, reactions: [{ emoji: "👍", count: 4 }], discordUrl: `https://discord.com/channels/100000000000000001/${id}/${id}` };
|
|
const detail = { id, title: "More railway stations", authorId: original.author.id, createdAt: original.createdAt, archived: false, locked: false, tags: [{ id: "100000000000000004", name: "World" }], messageCount: 3, discordUrl: `https://discord.com/channels/100000000000000001/${id}`, originalPost: original };
|
|
it("paginates discussion without duplicating the original post", async () => {
|
|
const fetcher = vi.fn().mockImplementation(async (url: string) => {
|
|
if (!url.includes("/messages")) return Response.json(detail);
|
|
const older = url.includes("cursor=");
|
|
return Response.json({ items: [original, { ...original, id: "100000000000000020", content: older ? "Earlier reply" : "Latest reply", editedAt: "2026-01-01T01:00:00Z" }], nextCursor: older ? null : "100000000000000020" });
|
|
});
|
|
vi.stubGlobal("fetch", fetcher);
|
|
render(<SuggestionReader id={id} />);
|
|
await screen.findByText("Latest reply");
|
|
expect(screen.getAllByText(original.content)).toHaveLength(1);
|
|
fireEvent.click(screen.getByRole("button", { name: "Next page" }));
|
|
await screen.findByText("Earlier reply");
|
|
expect(fetcher.mock.lastCall?.[0]).toBe(`/api/suggestions/${id}/messages?limit=25&cursor=100000000000000020`);
|
|
expect(screen.getByText("2026-01-01T01:00:00Z")).toBeTruthy();
|
|
fireEvent.click(screen.getByRole("button", { name: "Previous page" }));
|
|
await screen.findByText("Latest reply");
|
|
});
|
|
it("distinguishes deleted starters and empty discussion", async () => {
|
|
vi.stubGlobal("fetch", vi.fn().mockImplementation(async (url: string) => Response.json(url.includes("/messages") ? { items: [], nextCursor: null } : { ...detail, originalPost: null })));
|
|
render(<SuggestionReader id={id} />);
|
|
expect(await screen.findByText("The original post was deleted or is unavailable.")).toBeTruthy();
|
|
expect(await screen.findByText("No replies on this page.")).toBeTruthy();
|
|
});
|
|
it("handles detail access denial without loading the discussion", async () => {
|
|
const fetcher = vi.fn().mockResolvedValue(Response.json({}, { status: 403 }));
|
|
vi.stubGlobal("fetch", fetcher);
|
|
render(<SuggestionReader id={id} />);
|
|
await screen.findByRole("link", { name: "Admin sign-in" });
|
|
expect(screen.queryByRole("heading", { name: "More railway stations" })).toBeNull();
|
|
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
});
|
|
it("makes empty message content explicit and retries discussion failures without writes", async () => {
|
|
let messageReads = 0;
|
|
const fetcher = vi.fn().mockImplementation(async (url: string) => {
|
|
if (!url.includes("/messages")) return Response.json({ ...detail, originalPost: { ...original, content: "" } });
|
|
messageReads += 1;
|
|
return messageReads === 1 ? new Response(null, { status: 503 }) : Response.json({ items: [], nextCursor: null });
|
|
});
|
|
vi.stubGlobal("fetch", fetcher);
|
|
render(<SuggestionReader id={id} />);
|
|
await screen.findByText("No text was returned. View this message in Discord.");
|
|
await screen.findByRole("alert");
|
|
fireEvent.click(screen.getByRole("button", { name: "Try again" }));
|
|
await screen.findByText("No replies on this page.");
|
|
expect(fetcher.mock.calls.every(([, options]) => options.method === undefined || options.method === "GET")).toBe(true);
|
|
});
|
|
afterEach(() => { cleanup(); vi.unstubAllGlobals(); });
|
|
it("reads suggestion details and safely renders starter text, tags, reactions and Discord links", async () => {
|
|
const fetcher = vi.fn().mockImplementation(async (url: string) => Response.json(url.includes("/messages") ? { items: [], nextCursor: null } : detail));
|
|
vi.stubGlobal("fetch", fetcher);
|
|
render(<SuggestionReader id={id} />);
|
|
expect(screen.getByRole("status").textContent).toContain("Loading suggestion");
|
|
await screen.findByRole("heading", { name: "More railway stations", level: 1 });
|
|
expect(screen.getByText(original.content)).toBeTruthy();
|
|
expect(document.querySelector("script")).toBeNull();
|
|
expect(screen.getByText("World")).toBeTruthy();
|
|
expect(screen.getByText("👍 4")).toBeTruthy();
|
|
expect(screen.getByRole("link", { name: "Open in Discord" }).getAttribute("href")).toBe(detail.discordUrl);
|
|
expect(fetcher.mock.calls[0]?.[0]).toBe(`/api/suggestions/${id}`);
|
|
expect(screen.queryByRole("textbox")).toBeNull();
|
|
});
|