Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
82 lines
3.3 KiB
TypeScript
82 lines
3.3 KiB
TypeScript
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
import { pickDate } from "@/test/pickDate";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
saveAdminEvent: vi.fn(),
|
|
useAdminAuth: vi.fn(),
|
|
push: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminEvents", () => ({
|
|
saveAdminEvent: mocks.saveAdminEvent,
|
|
listSalespersonOptions: vi.fn().mockResolvedValue([]),
|
|
}));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
vi.mock("next/navigation", () => ({ useRouter: () => ({ push: mocks.push }) }));
|
|
|
|
import NewEventPage from "./page";
|
|
|
|
beforeEach(() => {
|
|
mocks.saveAdminEvent.mockReset();
|
|
mocks.push.mockReset();
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, hasModule: () => true });
|
|
});
|
|
|
|
describe("NewEventPage", () => {
|
|
it("renders the title through the shared page header", async () => {
|
|
render(<NewEventPage />);
|
|
expect(screen.getByRole("heading", { level: 1, name: "New event" })).toBeInTheDocument();
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
});
|
|
|
|
it("renders an empty EventForm", async () => {
|
|
render(<NewEventPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toHaveValue(""));
|
|
expect(screen.getByLabelText("Location")).toHaveValue("");
|
|
});
|
|
|
|
it("calls saveAdminEvent without a uuid and redirects to the returned event's uuid", async () => {
|
|
mocks.saveAdminEvent.mockResolvedValue({ uuid: "evt-42", name: "Jane" });
|
|
render(<NewEventPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
|
|
await userEvent.type(screen.getByLabelText("Name"), "Jane");
|
|
await userEvent.type(screen.getByLabelText("Location"), "Place");
|
|
await pickDate("Start date", "2025-06-15");
|
|
await pickDate("End date", "2025-06-16");
|
|
await userEvent.click(screen.getByRole("button", { name: /create/i }));
|
|
|
|
await waitFor(() => expect(mocks.saveAdminEvent).toHaveBeenCalledTimes(1));
|
|
const payload = mocks.saveAdminEvent.mock.calls[0][0];
|
|
expect(payload.name).toBe("Jane");
|
|
await waitFor(() => expect(mocks.push).toHaveBeenCalledWith("/events/evt-42?created=1"));
|
|
});
|
|
|
|
it("when TEAM_NOT_SAVED is returned, redirects with teamNotSaved=1", async () => {
|
|
mocks.saveAdminEvent.mockRejectedValue(
|
|
new ApiError("Team not saved", 400, {
|
|
code: "TEAM_NOT_SAVED",
|
|
details: { eventUuid: "evt-99" },
|
|
}),
|
|
);
|
|
render(<NewEventPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
|
|
await userEvent.type(screen.getByLabelText("Name"), "Jane");
|
|
await userEvent.type(screen.getByLabelText("Location"), "Place");
|
|
await pickDate("Start date", "2025-06-15");
|
|
await pickDate("End date", "2025-06-16");
|
|
await userEvent.click(screen.getByRole("button", { name: /create/i }));
|
|
|
|
await waitFor(() => expect(mocks.push).toHaveBeenCalledWith("/events/evt-99?teamNotSaved=1"));
|
|
});
|
|
|
|
it("renders NotAuthorized when the Events module is absent from aclMenu", () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => false });
|
|
render(<NewEventPage />);
|
|
expect(screen.getByTestId("not-authorized")).toBeInTheDocument();
|
|
});
|
|
});
|