Files
co-lite/solvway-admin-portal/app/components/pages/TicketsBlock.test.tsx
T

149 lines
8.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { ApiError } from "@/app/lib/clientApi";
const mocks = vi.hoisted(() => ({ publicCall: vi.fn(), listTicketTypes: vi.fn() }));
vi.mock("@/app/lib/services/publicRegistration", async (orig) => ({ ...(await orig<object>()), publicCall: mocks.publicCall }));
vi.mock("@/app/lib/services/adminTickets", async (orig) => ({ ...(await orig<object>()), listTicketTypes: mocks.listTicketTypes }));
import { TicketsBlock } from "./TicketsBlock";
import type { TicketsInfo } from "@/app/lib/services/publicTickets";
const INFO: TicketsInfo = {
eventName: "KL Hotel Tech Day",
startDate: "2027-01-15",
endDate: "2027-01-16",
location: "KLCC",
companyName: "INS",
notice: { id: 9, textEn: "English notice", textMs: "Notis" },
types: [
{ id: 1, name: "Early bird", priceSen: 15000, left: 12, state: "ON_SALE" },
{ id: 2, name: "General", priceSen: 25000, left: 3, state: "ON_SALE" },
{ id: 3, name: "VIP", priceSen: 50000, left: 0, state: "SOLD_OUT" },
{ id: 4, name: "Walk-up", priceSen: 20000, left: 5, state: "ENDED" },
{ id: 5, name: "Community pass", priceSen: 0, left: 50, state: "ON_SALE" },
],
};
const RECEIVED = {
reference: "T-7K3M9Q", token: "abcdefghijklmnopqrstuv", status: "AWAITING", totalSen: 30000, holdUntil: "2027-01-13T12:00:00",
lines: [{ name: "Early bird", unitPriceSen: 15000, quantity: 2 }], paymentInstructions: "Maybank 5140\nRef: your order number", eventName: "KL Hotel Tech Day",
};
beforeEach(() => {
mocks.publicCall.mockReset().mockResolvedValue(RECEIVED);
mocks.listTicketTypes.mockReset();
});
const row = (name: string) => screen.getByRole("group", { name });
const add = (user: ReturnType<typeof userEvent.setup>, name: string, times = 1) =>
(async () => { for (let i = 0; i < times; i++) await user.click(within(row(name)).getByRole("button", { name: `Add one ${name}` })); })();
async function fillBuyer(user: ReturnType<typeof userEvent.setup>) {
await user.type(screen.getByLabelText("Your name"), "Aisyah");
await user.type(screen.getByLabelText("Email"), "aisyah@example.test");
await user.click(screen.getByRole("checkbox"));
}
describe("TicketsBlock", () => {
it("lists the types with prices, Free, sold out and sales ended", () => {
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} info={INFO} />);
expect(within(row("Early bird")).getByText("RM 150.00")).toBeInTheDocument();
expect(within(row("Early bird")).getByText("12 left")).toBeInTheDocument();
expect(within(row("Community pass")).getByText("Free")).toBeInTheDocument();
expect(within(row("VIP")).getByText("Sold out")).toBeInTheDocument();
expect(within(row("VIP")).queryByRole("button", { name: "Add one VIP" })).toBeNull();
expect(within(row("Walk-up")).getByText("Sales ended")).toBeInTheDocument();
expect(within(row("Walk-up")).queryByRole("button", { name: "Add one Walk-up" })).toBeNull();
});
it("keeps a running total and caps the order at ten and each type at its seats", async () => {
const user = userEvent.setup();
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} info={INFO} />);
await add(user, "Early bird", 2);
await add(user, "General", 4);
expect(within(row("General")).getByText("3")).toBeInTheDocument();
expect(within(row("General")).getByRole("button", { name: "Add one General" })).toBeDisabled();
expect(screen.getByTestId("tickets-total")).toHaveTextContent("RM 1,050.00");
await add(user, "Community pass", 5);
expect(within(row("Community pass")).getByText("5")).toBeInTheDocument();
expect(within(row("Early bird")).getByRole("button", { name: "Add one Early bird" })).toBeDisabled();
await user.click(within(row("Early bird")).getByRole("button", { name: "Remove one Early bird" }));
expect(screen.getByTestId("tickets-total")).toHaveTextContent("RM 900.00");
});
it("asks for tickets, name, email and consent before sending", async () => {
const user = userEvent.setup();
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} info={INFO} />);
await user.click(screen.getByRole("button", { name: "Request tickets" }));
expect(screen.getByText("Choose at least one ticket.")).toBeInTheDocument();
expect(screen.getByText("Enter your name.")).toBeInTheDocument();
expect(screen.getByText("Enter your email.")).toBeInTheDocument();
expect(screen.getByText("Tick the box to agree to the privacy notice.")).toBeInTheDocument();
expect(mocks.publicCall).not.toHaveBeenCalled();
});
it("sends the request and thanks with the instructions, hold and order link", async () => {
const user = userEvent.setup();
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} info={INFO} />);
await add(user, "Early bird", 2);
await fillBuyer(user);
await user.click(screen.getByRole("button", { name: "Request tickets" }));
await waitFor(() => expect(mocks.publicCall).toHaveBeenCalled());
const [url, init] = mocks.publicCall.mock.calls[0];
expect(url).toBe("/api/public/pages/INS/tech-day/tickets/1");
expect(JSON.parse(init.body)).toMatchObject({
lines: [{ ticketTypeId: 1, quantity: 2 }], name: "Aisyah", email: "aisyah@example.test", consent: true, website: "",
});
expect(await screen.findByText(/T-7K3M9Q/)).toBeInTheDocument();
expect(screen.getByText(/Maybank 5140/)).toBeInTheDocument();
expect(screen.getByText(/13 Jan 2027/)).toBeInTheDocument();
expect(screen.getByRole("link", { name: "View your order" })).toHaveAttribute("href", "/o/abcdefghijklmnopqrstuv");
});
it("confirms a free order without instructions", async () => {
const user = userEvent.setup();
mocks.publicCall.mockResolvedValue({ ...RECEIVED, status: "PAID", totalSen: 0, holdUntil: null, paymentInstructions: undefined });
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} info={INFO} />);
await add(user, "Community pass", 2);
await fillBuyer(user);
await user.click(screen.getByRole("button", { name: "Request tickets" }));
expect(await screen.findByText("Your order T-7K3M9Q is confirmed.")).toBeInTheDocument();
expect(screen.queryByText(/How to pay/)).toBeNull();
});
it("shows a refusal from the server", async () => {
const user = userEvent.setup();
mocks.publicCall.mockRejectedValue(new ApiError("x", 409, { code: "TYPE_UNAVAILABLE", error: "General has only 1 ticket left." }));
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} info={INFO} />);
await add(user, "General", 2);
await fillBuyer(user);
await user.click(screen.getByRole("button", { name: "Request tickets" }));
expect(await screen.findByRole("alert")).toHaveTextContent("General has only 1 ticket left.");
});
it("shows a field error under its field", async () => {
const user = userEvent.setup();
mocks.publicCall.mockRejectedValue(new ApiError("x", 400, { code: "INVALID_EMAIL", error: "Enter a valid email.", details: { field: "email" } }));
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} info={INFO} />);
await add(user, "General", 1);
await fillBuyer(user);
await user.click(screen.getByRole("button", { name: "Request tickets" }));
expect(await screen.findByText("Enter a valid email.")).toBeInTheDocument();
});
it("says ticket sales are closed", () => {
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} />);
expect(screen.getByRole("status")).toHaveTextContent("Ticket sales are closed.");
});
it("shows the event's real types on the editor canvas", async () => {
mocks.listTicketTypes.mockResolvedValue([{ id: 2, name: "General", priceSen: 25000, quantity: 100, salesEndDate: null, sort: 0, enabled: true, held: 20, left: 80 }]);
render(<TicketsBlock companyCode="INS" slug="tech-day" index={1} preview eventUuid="900" />);
expect(await screen.findByText("General")).toBeInTheDocument();
expect(screen.getByText("RM 250.00")).toBeInTheDocument();
expect(mocks.listTicketTypes).toHaveBeenCalledWith("900");
expect(screen.queryByRole("button", { name: "Request tickets" })).toBeNull();
});
});