Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
158 lines
8.6 KiB
TypeScript
158 lines
8.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent } from "@testing-library/react";
|
|
|
|
const mocks = vi.hoisted(() => ({ getPublicTicket: vi.fn(), getStaffTicket: vi.fn(), consentTicket: vi.fn(), checkInTicket: vi.fn() }));
|
|
vi.mock("@/app/lib/services/publicTickets", async (orig) => ({
|
|
...(await orig<object>()),
|
|
getPublicTicket: mocks.getPublicTicket,
|
|
getStaffTicket: mocks.getStaffTicket,
|
|
consentTicket: mocks.consentTicket,
|
|
checkInTicket: mocks.checkInTicket,
|
|
}));
|
|
vi.mock("next/navigation", () => ({ useParams: () => ({ code: "abcdefghijklmnopqrstu_" }) }));
|
|
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
import TicketPage from "./page";
|
|
|
|
const CODE = "abcdefghijklmnopqrstu_";
|
|
const EVENT = { eventName: "KL Hotel Tech Day", startDate: "2027-01-15", endDate: "2027-01-16", location: "KLCC" };
|
|
const NOTICE = { id: 9, textEn: "English notice", textMs: "Notis" };
|
|
const VALID = {
|
|
...EVENT, companyName: "INS", typeName: "General", attendeeFirstName: "Tan", named: true, state: "VALID",
|
|
checkedInAt: null, consented: false, notice: NOTICE,
|
|
};
|
|
const STAFF = {
|
|
...EVENT, companyName: "INS", typeName: "General", attendeeName: "Tan Mei Ling", named: true, consented: false, state: "VALID",
|
|
checkedInAt: null, checkedInBy: null, notice: NOTICE,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
mocks.getPublicTicket.mockReset().mockResolvedValue(VALID);
|
|
mocks.getStaffTicket.mockReset().mockRejectedValue(new ApiError("x", 401, { error: "Not authenticated" }));
|
|
mocks.consentTicket.mockReset().mockResolvedValue({ consented: true });
|
|
mocks.checkInTicket.mockReset().mockResolvedValue({ ...STAFF, state: "CHECKED_IN", checkedInAt: "2027-01-15T09:30:00", checkedInBy: "door1" });
|
|
});
|
|
|
|
// add-event-tickets: the ticket page /t/<code>, public view and staff view.
|
|
describe("/t/[code] public view", () => {
|
|
it("shows the event, the ticket, its QR and the consent offer", async () => {
|
|
render(<TicketPage />);
|
|
expect(await screen.findByRole("heading", { level: 1, name: "KL Hotel Tech Day" })).toBeInTheDocument();
|
|
expect(mocks.getPublicTicket).toHaveBeenCalledWith(CODE);
|
|
expect(screen.getByText("General")).toBeInTheDocument();
|
|
expect(screen.getByText("Ticket for Tan")).toBeInTheDocument();
|
|
expect(screen.getByRole("img", { name: "Ticket QR code" })).toHaveAttribute("src", `/api/public/tickets/${CODE}/qr.png`);
|
|
expect(screen.getByText("Keep me updated about INS events")).toBeInTheDocument();
|
|
expect(screen.getByText("English notice")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: "Check in" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows a checked-in ticket's time without a QR", async () => {
|
|
mocks.getPublicTicket.mockResolvedValue({ ...VALID, state: "CHECKED_IN", checkedInAt: "2027-01-15T09:30:00" });
|
|
render(<TicketPage />);
|
|
expect(await screen.findByText("Checked in at 15 Jan 2027, 09:30")).toBeInTheDocument();
|
|
expect(screen.queryByRole("img", { name: "Ticket QR code" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it.each(["AWAITING", "ORDER_CANCELLED", "ORDER_EXPIRED", "TICKET_CANCELLED"])("says a %s ticket is not valid, with no QR and no consent", async (state) => {
|
|
mocks.getPublicTicket.mockResolvedValue({ ...VALID, state, notice: undefined });
|
|
render(<TicketPage />);
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("This ticket is not valid");
|
|
expect(screen.queryByRole("img", { name: "Ticket QR code" })).not.toBeInTheDocument();
|
|
expect(screen.queryByText("Keep me updated about INS events")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("says an unknown link is not valid", async () => {
|
|
mocks.getPublicTicket.mockRejectedValue(new ApiError("x", 404, { code: "TICKET_INVALID" }));
|
|
render(<TicketPage />);
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("This ticket link is not valid.");
|
|
});
|
|
|
|
it("records the consent once ticked", async () => {
|
|
render(<TicketPage />);
|
|
const save = await screen.findByRole("button", { name: "Save" });
|
|
expect(save).toBeDisabled();
|
|
fireEvent.click(screen.getByRole("checkbox", { name: "Keep me updated about INS events" }));
|
|
fireEvent.click(save);
|
|
expect(await screen.findByText("Thank you. We will keep you updated.")).toBeInTheDocument();
|
|
expect(mocks.consentTicket).toHaveBeenCalledWith(CODE);
|
|
});
|
|
|
|
it("offers no consent without a notice", async () => {
|
|
mocks.getPublicTicket.mockResolvedValue({ ...VALID, named: false, attendeeFirstName: null, notice: undefined });
|
|
render(<TicketPage />);
|
|
await screen.findByRole("img", { name: "Ticket QR code" });
|
|
expect(screen.queryByText("Keep me updated about INS events")).not.toBeInTheDocument();
|
|
expect(screen.queryByText(/Ticket for/)).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("/t/[code] staff view", () => {
|
|
it.each([403, 404])("keeps the public view on a %s", async (status) => {
|
|
mocks.getStaffTicket.mockRejectedValue(new ApiError("x", status, {}));
|
|
render(<TicketPage />);
|
|
await screen.findByRole("img", { name: "Ticket QR code" });
|
|
expect(screen.queryByRole("button", { name: "Check in" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("needs only the staff call, so the public limit never blocks door staff", async () => {
|
|
mocks.getStaffTicket.mockResolvedValue(STAFF);
|
|
mocks.getPublicTicket.mockRejectedValue(new ApiError("x", 429, { code: "TOO_MANY" }));
|
|
render(<TicketPage />);
|
|
expect(await screen.findByRole("button", { name: "Check in" })).toBeInTheDocument();
|
|
expect(screen.getByRole("heading", { level: 1, name: "KL Hotel Tech Day" })).toBeInTheDocument();
|
|
expect(mocks.getPublicTicket).not.toHaveBeenCalled();
|
|
expect(screen.queryByRole("img", { name: "Ticket QR code" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the state banner and checks a named ticket in", async () => {
|
|
mocks.getStaffTicket.mockResolvedValue(STAFF);
|
|
render(<TicketPage />);
|
|
expect(await screen.findByText("Valid")).toBeInTheDocument();
|
|
expect(screen.getByText("Tan Mei Ling")).toBeInTheDocument();
|
|
fireEvent.click(screen.getByRole("button", { name: "Check in" }));
|
|
expect(await screen.findByText("Checked in.")).toBeInTheDocument();
|
|
expect(mocks.checkInTicket).toHaveBeenCalledWith(CODE, { consent: false });
|
|
expect(screen.queryByRole("button", { name: "Check in" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("asks for the attendee of an unnamed ticket and sends the consent", async () => {
|
|
mocks.getStaffTicket.mockResolvedValue({ ...STAFF, named: false, attendeeName: null });
|
|
render(<TicketPage />);
|
|
expect(await screen.findByText("No attendee named yet")).toBeInTheDocument();
|
|
fireEvent.change(screen.getByLabelText("Attendee name"), { target: { value: "Lim Wei" } });
|
|
fireEvent.change(screen.getByLabelText("Mobile number"), { target: { value: "012-555 0101" } });
|
|
fireEvent.click(screen.getByRole("checkbox", { name: "Keep me updated about INS events" }));
|
|
fireEvent.click(screen.getByRole("button", { name: "Check in" }));
|
|
await screen.findByText("Checked in.");
|
|
expect(mocks.checkInTicket).toHaveBeenCalledWith(CODE, { name: "Lim Wei", mobile: "012-555 0101", email: "", consent: true });
|
|
});
|
|
|
|
it("shows a second scan as already checked in", async () => {
|
|
mocks.getStaffTicket.mockResolvedValue({ ...STAFF, state: "CHECKED_IN", checkedInAt: "2027-01-15T09:30:00", checkedInBy: "door1" });
|
|
render(<TicketPage />);
|
|
expect(await screen.findByText("Already checked in at 15 Jan 2027, 09:30")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: "Check in" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows an unpaid ticket as not valid with no Check in", async () => {
|
|
mocks.getStaffTicket.mockResolvedValue({ ...STAFF, state: "AWAITING" });
|
|
render(<TicketPage />);
|
|
expect(await screen.findByText("Not valid: awaiting payment")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: "Check in" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it.each([
|
|
[{ code: "ALREADY_CHECKED_IN", details: { value: "2027-01-15T09:30" } }, "Already checked in at 15 Jan 2027, 09:30"],
|
|
[{ code: "NOT_VALID", details: { value: "ORDER_CANCELLED" } }, "Not valid: order cancelled"],
|
|
[{ code: "NOT_EVENT_DAY" }, "Check-in is only open on the event days."],
|
|
[{ code: "MOBILE_OR_EMAIL_REQUIRED", error: "MOBILE_OR_EMAIL_REQUIRED", details: { field: "mobile" } }, "Enter a mobile number or an email."],
|
|
])("shows the refusal %o", async (data, text) => {
|
|
mocks.getStaffTicket.mockResolvedValue(STAFF);
|
|
mocks.checkInTicket.mockRejectedValue(new ApiError("x", 409, data));
|
|
render(<TicketPage />);
|
|
fireEvent.click(await screen.findByRole("button", { name: "Check in" }));
|
|
expect(await screen.findByRole("alert")).toHaveTextContent(text);
|
|
});
|
|
});
|