Files
gerbeur/src/test/pages/UserRegister.test.tsx
khannurien 3e15e9a4f2
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 57s
Build and Deploy Static Documentation Site / build-and-push (push) Successful in 28s
v4: /follow/playlists opens the followed playlists' feed, and a page's test opens it through the app's routes — the path was a 404 since the two views of the followed feed became pages: the route table held each feed tab as an exact path, /follow, so /follow/playlists fell to /:handle/*, which read "follow" as a category and "playlists" as a tab it hasn't. /music/follow/playlists, which goes through that route, worked. The tabs' routes now take what follows them (/${tab}/*), which Feed already reads and refuses when it names no view.
No test saw it because each page's test mounted the page on a route pattern of its own, a copy of App's, with stand-in pages where a redirect lands: Index's parsed the path itself, and passed on one the app doesn't serve. renderPage (src/test/render.tsx) now opens a URL through AppRoutes, which App.tsx exports for it, inside the real providers, and every page's test uses it: the page is the one the app routes that URL to, behind its guard, and a redirect is checked by the path it lands on. An entry may be a location, to arrive with the state a link hands over. Every page being loaded with its route, the first query after opening one waits. src/test/App.test.tsx tests the table itself: the paths that are a feed, those that are not found, those that send a guest to sign in and back, and those that send a signed-in user home.

The profile's test held /@ann/dumps/x to be a path the profile answers as not found, which only its own pattern made true: in the app it is a dump's page. It now asks for /@ann/dumps/x/y. The tests of the two guards still mount them around a stand-in child, being tests of the guard alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 19:59:52 +00:00

89 lines
3.1 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { screen, waitFor } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { calls, deferred, fail, route } from "../fetch.ts";
import { makeSession, wire } from "../fixtures.ts";
import { currentPath, renderPage } from "../render.tsx";
describe("UserRegister", () => {
function setup(search = "?token=inv-1") {
const user = userEvent.setup();
renderPage(`/register${search}`);
return user;
}
test("no token: an invalid invite, no request", async () => {
setup("");
expect(await screen.findByRole("alert")).toHaveTextContent(
"Invalid invite",
);
expect(calls("GET /api/invites/:t")).toHaveLength(0);
});
test("an unknown token is invalid", async () => {
const invite = deferred();
route("GET /api/invites/:t", () => invite.promise);
setup();
expect(await screen.findByText("Checking invite…")).toBeInTheDocument();
invite.resolve(fail("nope", 404));
expect(await screen.findByRole("alert")).toHaveTextContent("already used");
});
test("registers with a valid invite", async () => {
route("GET /api/invites/:t", () => ({}));
const session = makeSession({ username: "new_one" });
route("POST /api/users", () => wire(session));
const user = setup();
await user.type(await screen.findByPlaceholderText("Username"), "new_one");
await user.type(screen.getByPlaceholderText("Email address"), "n@o.pe");
await user.type(
screen.getByPlaceholderText("Password (min. 8 characters)"),
"longenough{Enter}",
);
await waitFor(() => expect(currentPath()).toBe("/"));
expect(calls("POST /api/users")[0].body).toEqual({
username: "new_one",
password: "longenough",
inviteToken: "inv-1",
email: "n@o.pe",
});
expect(calls("GET /api/invites/:t")[0].params.t).toBe("inv-1");
});
test("validates the username and password length", async () => {
route("GET /api/invites/:t", () => ({}));
const user = setup();
await user.type(
await screen.findByPlaceholderText("Username"),
"bad name!",
);
await user.type(screen.getByPlaceholderText("Email address"), "n@o.pe");
await user.type(
screen.getByPlaceholderText("Password (min. 8 characters)"),
"short{Enter}",
);
expect(await screen.findByText(/letters, numbers, or underscores/))
.toBeInTheDocument();
expect(screen.getByText("At least 8 characters")).toBeInTheDocument();
expect(calls("POST /api/users")).toHaveLength(0);
});
test("shows a server refusal (username taken)", async () => {
route("GET /api/invites/:t", () => ({}));
route(
"POST /api/users",
() => fail("Username already taken", 409),
);
const user = setup();
await user.type(await screen.findByPlaceholderText("Username"), "taken");
await user.type(screen.getByPlaceholderText("Email address"), "n@o.pe");
await user.type(
screen.getByPlaceholderText("Password (min. 8 characters)"),
"longenough{Enter}",
);
expect(await screen.findByRole("alert")).toHaveTextContent(
"Username already taken",
);
});
});