import { describe, expect, test } from "vitest"; import { screen, waitFor } from "@testing-library/react"; import { route } from "./fetch.ts"; import { makeCategory, makeSession, page } from "./fixtures.ts"; import { currentPath, renderPage } from "./render.tsx"; const music = makeCategory({ id: "c-music", slug: "music", name: "Music" }); const next = (path: string) => `/login?next=${encodeURIComponent(path)}`; function open(url: string, session: ReturnType | null) { route("GET /api/dumps", () => page([])); renderPage(url, { session, categories: [music] }); } describe("App's routes", () => { test.each([ "/", "/hot", "/new", "/journal", "/follow", "/follow/playlists", "/music", "/music/new", "/music/follow", "/music/follow/playlists", ])("%s is a feed", async (url) => { open(url, makeSession()); // The tab bar, which the feed draws at two widths. expect(await screen.findAllByRole("button", { name: "Followed" })).not .toHaveLength(0); expect(currentPath()).toBe(url); expect(screen.queryByRole("heading", { name: "404" })).toBeNull(); }); test.each([ "/hot/more", "/follow/users", "/follow/nope", "/follow/playlists/more", "/nope", "/music/nope", "/music/hot/more", "/music/follow/users", "/search/users/more", ])("%s is not found", async (url) => { open(url, makeSession()); expect(await screen.findByRole("heading", { name: "404" })) .toBeInTheDocument(); }); test.each([ "/follow", "/follow/playlists", "/music/follow/playlists", "/notifications", "/settings", "/manage", "/community", "/community/monthly", "/@ann/dumps/a-find/edit", ])("%s sends a guest to sign in, and back", async (url) => { open(url, null); await waitFor(() => expect(currentPath()).toBe(next(url))); }); test.each(["/login", "/register"])( "%s sends a signed-in user home", async (url) => { open(url, makeSession()); await waitFor(() => expect(currentPath()).toBe("/")); }, ); });