Files
co-lite/solvway-admin-portal/app/components/events/RegistrationQrCard.checkin.test.tsx
T

77 lines
3.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { forwardRef } from "react";
const mocks = vi.hoisted(() => ({ getEventLinks: vi.fn(), setLinkEnabled: vi.fn(), getCheckin: vi.fn() }));
vi.mock("@/app/lib/services/adminRegistration", () => ({ getEventLinks: mocks.getEventLinks, setLinkEnabled: mocks.setLinkEnabled }));
vi.mock("@/app/lib/services/adminGuests", () => ({ getCheckin: mocks.getCheckin }));
vi.mock("qrcode.react", () => ({
QRCodeSVG: ({ value }: { value: string }) => <svg data-testid="qr-svg" data-value={value} />,
QRCodeCanvas: forwardRef<HTMLCanvasElement>(function QRCodeCanvas(_, ref) {
return <canvas ref={ref} />;
}),
}));
import { RegistrationQrCard } from "./RegistrationQrCard";
const links = {
event: { token: "ev1", enabled: true },
members: [
{ userId: 5, name: "Aini", username: "aini@cv", token: "m1", enabled: true },
{ userId: 6, name: "Bob", username: "", token: "m2", enabled: true },
],
};
const o = window.location.origin;
beforeEach(() => {
mocks.getEventLinks.mockReset().mockResolvedValue(links);
mocks.setLinkEnabled.mockReset();
mocks.getCheckin.mockReset().mockResolvedValue({ token: "chk1" });
});
describe("RegistrationQrCard check-in and print", () => {
it("shows the check-in code with its /c/ link and no on/off switch", async () => {
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite />);
await screen.findByText("Check-in code");
expect(screen.getAllByTestId("qr-svg").map((e) => e.getAttribute("data-value"))).toContain(`${o}/c/chk1`);
expect(screen.getAllByRole("switch")).toHaveLength(3);
expect(mocks.getCheckin).toHaveBeenCalledWith("u1");
});
it("still lists the other codes when the check-in code does not load", async () => {
mocks.getCheckin.mockRejectedValue(new Error("nope"));
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite />);
await screen.findByText("Aini (aini@cv)");
expect(screen.queryByText("Check-in code")).toBeNull();
});
it("links to the print cards page", async () => {
render(<RegistrationQrCard eventUuid="u 1" eventName="Expo" canWrite />);
expect(await screen.findByRole("link", { name: "Print cards" })).toHaveAttribute("href", "/events/u%201/print");
});
it("copies every code as a name: link line", async () => {
const user = userEvent.setup();
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite />);
await screen.findByText("Check-in code");
const write = vi.spyOn(navigator.clipboard, "writeText");
await user.click(screen.getByRole("button", { name: "Copy all links" }));
expect(write).toHaveBeenCalledWith(
[`Event code: ${o}/r/ev1`, `Aini (aini@cv): ${o}/r/m1`, `Bob: ${o}/r/m2`, `Check-in code: ${o}/c/chk1`].join("\n"),
);
});
it("confirms Copy all, and reports a clipboard failure", async () => {
const user = userEvent.setup();
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite />);
await screen.findByText("Check-in code");
const write = vi.spyOn(navigator.clipboard, "writeText");
await user.click(screen.getByRole("button", { name: "Copy all links" }));
expect(await screen.findByRole("button", { name: "Link copied" })).toBeInTheDocument();
write.mockRejectedValue(new Error("denied"));
await user.click(screen.getByRole("button", { name: "Link copied" }));
expect(await screen.findByText("Could not copy.")).toBeInTheDocument();
});
});