Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
569 lines
19 KiB
TypeScript
569 lines
19 KiB
TypeScript
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
listSalespersonOptions: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminEvents", () => ({
|
|
listSalespersonOptions: mocks.listSalespersonOptions,
|
|
}));
|
|
|
|
import { EventForm } from "./EventForm";
|
|
import { pickDate } from "@/test/pickDate";
|
|
|
|
const SALESPEOPLE = [
|
|
{ id: 1, name: "Alice", username: "alice.smith" },
|
|
{ id: 2, name: "Bob", username: "bob.jones" },
|
|
{ id: 3, name: "Charlie", username: "charlie.brown" },
|
|
];
|
|
|
|
beforeEach(() => {
|
|
mocks.listSalespersonOptions.mockReset().mockResolvedValue(SALESPEOPLE);
|
|
});
|
|
|
|
async function waitForOptions() {
|
|
await waitFor(() => expect(screen.getByText("Alice (alice.smith)")).toBeInTheDocument());
|
|
}
|
|
|
|
describe("EventForm", () => {
|
|
it("renders all fields on the create form", async () => {
|
|
render(<EventForm onSubmit={vi.fn()} />);
|
|
await waitForOptions();
|
|
|
|
expect(screen.getByLabelText("Name")).toBeInTheDocument();
|
|
expect(screen.getByLabelText("Location")).toBeInTheDocument();
|
|
expect(screen.getByLabelText("Start date")).toBeInTheDocument();
|
|
expect(screen.getByLabelText("End date")).toBeInTheDocument();
|
|
expect(screen.getByRole("checkbox", { name: "Alice (alice.smith)" })).toBeInTheDocument();
|
|
expect(screen.getByRole("checkbox", { name: "Bob (bob.jones)" })).toBeInTheDocument();
|
|
expect(screen.getByRole("checkbox", { name: "Charlie (charlie.brown)" })).toBeInTheDocument();
|
|
// Enabled switch must NOT be present on create form.
|
|
expect(screen.queryByLabelText("Enabled")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("renders the Enabled switch on the edit form", async () => {
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
}}
|
|
onSubmit={vi.fn()}
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
expect(screen.getByLabelText("Enabled")).toBeInTheDocument();
|
|
});
|
|
|
|
it("prefills from initial on edit form", async () => {
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: false,
|
|
}}
|
|
onSubmit={vi.fn()}
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
expect(screen.getByLabelText("Name")).toHaveValue("Conference");
|
|
expect(screen.getByLabelText("Location")).toHaveValue("Convention Center");
|
|
});
|
|
|
|
it("blocks submit and shows errors when required fields are missing", async () => {
|
|
const onSubmit = vi.fn();
|
|
render(<EventForm onSubmit={onSubmit} />);
|
|
await waitForOptions();
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /create/i }));
|
|
|
|
expect(await screen.findByText("Name is required.")).toBeInTheDocument();
|
|
expect(await screen.findByText("Location is required.")).toBeInTheDocument();
|
|
expect(await screen.findByText("Start date is required.")).toBeInTheDocument();
|
|
expect(await screen.findByText("End date is required.")).toBeInTheDocument();
|
|
expect(onSubmit).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("blocks submit when end date is before start date", async () => {
|
|
const onSubmit = vi.fn();
|
|
// Start with valid dates, then make start after end.
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
}}
|
|
onSubmit={onSubmit}
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
// Change start to 2025-06-10, which is after the existing end 2025-06-03.
|
|
pickDate("Start date", "2025-06-10");
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
|
|
expect(await screen.findByText("End date must not be before start date.")).toBeInTheDocument();
|
|
expect(onSubmit).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows name-too-long validation error", async () => {
|
|
const onSubmit = vi.fn();
|
|
render(<EventForm onSubmit={onSubmit} />);
|
|
await waitForOptions();
|
|
|
|
// Name > 100 chars
|
|
await userEvent.type(screen.getByLabelText("Name"), "a".repeat(101));
|
|
await userEvent.type(screen.getByLabelText("Location"), "Somewhere");
|
|
// Set dates via initial + changing end to be before start doesn't help for name length.
|
|
// We need to get dates past the required check. Use an initial with valid dates,
|
|
// make name too long, clear name, type long name, and save.
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "a".repeat(101),
|
|
location: "Somewhere",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
}}
|
|
onSubmit={onSubmit}
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
|
|
expect(await screen.findByText("Name must be 100 characters or fewer.")).toBeInTheDocument();
|
|
expect(onSubmit).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows location-too-long validation error", async () => {
|
|
const onSubmit = vi.fn();
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Event",
|
|
location: "x".repeat(201),
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
}}
|
|
onSubmit={onSubmit}
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
|
|
expect(await screen.findByText("Location must be 200 characters or fewer.")).toBeInTheDocument();
|
|
expect(onSubmit).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("builds the correct payload on create", async () => {
|
|
const onSubmit = vi.fn().mockResolvedValue(undefined);
|
|
render(<EventForm onSubmit={onSubmit} submitLabel="Create" />);
|
|
await waitForOptions();
|
|
|
|
await userEvent.type(screen.getByLabelText("Name"), "Conference");
|
|
await userEvent.type(screen.getByLabelText("Location"), "Convention Center");
|
|
|
|
// Pick dates through the calendar
|
|
pickDate("Start date", "2025-06-01");
|
|
pickDate("End date", "2025-06-03");
|
|
|
|
await userEvent.click(screen.getByRole("checkbox", { name: "Alice (alice.smith)" }));
|
|
await userEvent.click(screen.getByRole("checkbox", { name: "Bob (bob.jones)" }));
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /create/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(onSubmit).toHaveBeenCalledWith({
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
salespersonIds: [1, 2],
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("builds the correct payload on edit (includes enabled)", async () => {
|
|
const onSubmit = vi.fn().mockResolvedValue(undefined);
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
team: [],
|
|
}}
|
|
onSubmit={onSubmit}
|
|
submitLabel="Edit"
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
// Toggle the enabled switch off.
|
|
await userEvent.click(screen.getByRole("switch", { name: "Enabled" }));
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(onSubmit).toHaveBeenCalledWith({
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: false,
|
|
salespersonIds: [],
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("submits a one-day event when start equals end", async () => {
|
|
const onSubmit = vi.fn().mockResolvedValue(undefined);
|
|
render(<EventForm onSubmit={onSubmit} submitLabel="Create" />);
|
|
await waitForOptions();
|
|
|
|
await userEvent.type(screen.getByLabelText("Name"), "Workshop");
|
|
await userEvent.type(screen.getByLabelText("Location"), "Room 1");
|
|
pickDate("Start date", "2025-07-01");
|
|
pickDate("End date", "2025-07-01");
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /create/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(onSubmit).toHaveBeenCalledWith({
|
|
name: "Workshop",
|
|
location: "Room 1",
|
|
startDate: "2025-07-01",
|
|
endDate: "2025-07-01",
|
|
salespersonIds: [],
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("unchecking an inactive member removes it from salespersonIds", async () => {
|
|
const onSubmit = vi.fn().mockResolvedValue(undefined);
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
team: [
|
|
{ id: 1, name: "Alice", username: "alice.smith", enabled: true },
|
|
{ id: 99, name: "Deactivated", username: "old.user", enabled: false },
|
|
],
|
|
}}
|
|
onSubmit={onSubmit}
|
|
submitLabel="Edit"
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
// Both should be checked initially.
|
|
expect(screen.getByRole("checkbox", { name: "Alice (alice.smith)" })).toBeChecked();
|
|
expect(screen.getByRole("checkbox", { name: "Deactivated (old.user)" })).toBeChecked();
|
|
|
|
// Uncheck the inactive member, keep Alice.
|
|
await userEvent.click(screen.getByRole("checkbox", { name: "Deactivated (old.user)" }));
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(onSubmit).toHaveBeenCalledWith({
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
salespersonIds: [1],
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("shows username or id for inactive member with null name", async () => {
|
|
const onSubmit = vi.fn().mockResolvedValue(undefined);
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
team: [
|
|
{ id: 5, name: null, username: null, enabled: false },
|
|
{ id: 6, name: "Ghost", username: null, enabled: false },
|
|
],
|
|
}}
|
|
onSubmit={onSubmit}
|
|
submitLabel="Edit"
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
// Member with null name and null username should show id.
|
|
expect(screen.getByRole("checkbox", { name: "5" })).toBeChecked();
|
|
// Member with name but null username should show just name.
|
|
expect(screen.getByRole("checkbox", { name: "Ghost" })).toBeChecked();
|
|
});
|
|
|
|
it("builds the correct payload on edit with team", async () => {
|
|
const onSubmit = vi.fn().mockResolvedValue(undefined);
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
team: [
|
|
{ id: 1, name: "Alice", username: "alice.smith", enabled: true },
|
|
{ id: 99, name: "Deactivated", username: "old.user", enabled: false },
|
|
],
|
|
}}
|
|
onSubmit={onSubmit}
|
|
submitLabel="Edit"
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
// Alice should be checked, deactivated member should be checked with "Inactive" marker.
|
|
expect(screen.getByRole("checkbox", { name: "Alice (alice.smith)" })).toBeChecked();
|
|
expect(screen.getByRole("checkbox", { name: "Deactivated (old.user)" })).toBeChecked();
|
|
expect(screen.getByText(/Inactive/)).toBeInTheDocument();
|
|
|
|
// Uncheck Alice, keep deactivated member
|
|
await userEvent.click(screen.getByRole("checkbox", { name: "Alice (alice.smith)" }));
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(onSubmit).toHaveBeenCalledWith({
|
|
name: "Conference",
|
|
location: "Convention Center",
|
|
startDate: "2025-06-01",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
salespersonIds: [99],
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("shows an API REQUIRED_FIELD error under each listed field", async () => {
|
|
class ApiError extends Error {
|
|
status: number;
|
|
data?: unknown;
|
|
constructor(message: string, status: number, data?: unknown) {
|
|
super(message);
|
|
this.name = "ApiError";
|
|
this.status = status;
|
|
this.data = data;
|
|
}
|
|
}
|
|
|
|
const onSubmit = vi.fn().mockRejectedValue(
|
|
new ApiError("Validation failed", 422, {
|
|
errors: ["location", "startDate"],
|
|
}),
|
|
);
|
|
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "",
|
|
startDate: "",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
}}
|
|
onSubmit={onSubmit}
|
|
submitLabel="Edit"
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
// Pre-fill all required fields so client-side validation passes,
|
|
// but the API returns REQUIRED_FIELD errors for location and startDate.
|
|
await userEvent.type(screen.getByLabelText("Name"), "X");
|
|
await userEvent.type(screen.getByLabelText("Location"), "Somewhere");
|
|
|
|
pickDate("Start date", "2025-06-01");
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
|
|
// Each field listed in errors[] gets the required message.
|
|
expect(await screen.findByText("Location is required.")).toBeInTheDocument();
|
|
expect(await screen.findByText("Start date is required.")).toBeInTheDocument();
|
|
expect(screen.getByLabelText("Location")).toHaveAttribute("aria-invalid", "true");
|
|
expect(screen.getByLabelText("Start date")).toHaveAttribute("aria-invalid", "true");
|
|
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("shows a team error from the API under the Team list, and an unknown missing field at the top", async () => {
|
|
class ApiError extends Error {
|
|
status: number;
|
|
data?: unknown;
|
|
constructor(message: string, status: number, data?: unknown) {
|
|
super(message);
|
|
this.name = "ApiError";
|
|
this.status = status;
|
|
this.data = data;
|
|
}
|
|
}
|
|
const onSubmit = vi
|
|
.fn()
|
|
.mockRejectedValueOnce(new ApiError("Unknown or inactive admin user 7", 400, { code: "UNKNOWN_USER", details: { field: "salespersonIds" } }))
|
|
.mockRejectedValueOnce(new ApiError("Required: venue", 400, { code: "REQUIRED_FIELD", errors: ["venue"], details: { field: "venue" } }));
|
|
|
|
render(
|
|
<EventForm
|
|
initial={{ uuid: "e1", name: "Conference", location: "Hall", startDate: "2025-06-01", endDate: "2025-06-03", enabled: true }}
|
|
onSubmit={onSubmit}
|
|
submitLabel="Edit"
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
const team = screen.getByRole("group", { name: "Team" });
|
|
expect(await within(team).findByText("Unknown or inactive admin user 7")).toBeInTheDocument();
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
expect(await screen.findByText("Required: venue")).toBeInTheDocument();
|
|
expect(within(team).queryByText("Required: venue")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows an API field error under the correct field", async () => {
|
|
class ApiError extends Error {
|
|
status: number;
|
|
data?: unknown;
|
|
constructor(message: string, status: number, data?: unknown) {
|
|
super(message);
|
|
this.name = "ApiError";
|
|
this.status = status;
|
|
this.data = data;
|
|
}
|
|
}
|
|
|
|
const onSubmit = vi.fn().mockRejectedValue(
|
|
new ApiError("Validation failed", 422, {
|
|
details: { field: "name" },
|
|
errors: ["location", "startDate"],
|
|
}),
|
|
);
|
|
|
|
render(
|
|
<EventForm
|
|
initial={{
|
|
uuid: "e1",
|
|
name: "Conference",
|
|
location: "",
|
|
startDate: "",
|
|
endDate: "2025-06-03",
|
|
enabled: true,
|
|
}}
|
|
onSubmit={onSubmit}
|
|
submitLabel="Edit"
|
|
/>,
|
|
);
|
|
await waitForOptions();
|
|
|
|
// Pre-fill required fields so only the API error shows.
|
|
await userEvent.type(screen.getByLabelText("Name"), "X");
|
|
await userEvent.type(screen.getByLabelText("Location"), "Somewhere");
|
|
|
|
pickDate("Start date", "2025-06-01");
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
|
|
|
|
// errors[] takes priority over details.field, so location and startDate get the errors.
|
|
expect(await screen.findByText("Location is required.")).toBeInTheDocument();
|
|
expect(await screen.findByText("Start date is required.")).toBeInTheDocument();
|
|
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
// The error should be under the listed fields (aria-invalid=true).
|
|
expect(screen.getByLabelText("Location")).toHaveAttribute("aria-invalid", "true");
|
|
expect(screen.getByLabelText("Start date")).toHaveAttribute("aria-invalid", "true");
|
|
});
|
|
|
|
it("shows a general error at the top when onSubmit throws a non-field API error", async () => {
|
|
class ApiError extends Error {
|
|
status: number;
|
|
data?: unknown;
|
|
constructor(message: string, status: number, data?: unknown) {
|
|
super(message);
|
|
this.name = "ApiError";
|
|
this.status = status;
|
|
this.data = data;
|
|
}
|
|
}
|
|
|
|
const onSubmit = vi.fn().mockRejectedValue(new ApiError("Network error", 500));
|
|
|
|
render(<EventForm onSubmit={onSubmit} />);
|
|
await waitForOptions();
|
|
|
|
await userEvent.type(screen.getByLabelText("Name"), "Event");
|
|
await userEvent.type(screen.getByLabelText("Location"), "Here");
|
|
pickDate("Start date", "2025-06-01");
|
|
pickDate("End date", "2025-06-03");
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /create/i }));
|
|
|
|
expect(await screen.findByText("Network error")).toBeInTheDocument();
|
|
// onSubmit was called (it IS the mock).
|
|
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("disables submit while saving", async () => {
|
|
let resolveSubmit!: () => void;
|
|
const onSubmit = vi.fn(
|
|
() =>
|
|
new Promise<void>((resolve) => {
|
|
resolveSubmit = resolve;
|
|
}),
|
|
);
|
|
render(<EventForm onSubmit={onSubmit} />);
|
|
await waitForOptions();
|
|
|
|
await userEvent.type(screen.getByLabelText("Name"), "Event");
|
|
await userEvent.type(screen.getByLabelText("Location"), "Here");
|
|
pickDate("Start date", "2025-06-01");
|
|
pickDate("End date", "2025-06-03");
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: /create/i }));
|
|
|
|
await waitFor(() => expect(screen.getByRole("button", { name: /create/i })).toBeDisabled());
|
|
resolveSubmit();
|
|
});
|
|
});
|