Member codes on the event page are labelled "name (username)", as in the team list, and the consent tick box reads in English and Bahasa Malaysia. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
73 lines
3.1 KiB
TypeScript
73 lines
3.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, waitFor } 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() }));
|
|
vi.mock("@/app/lib/services/adminRegistration", () => mocks);
|
|
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: false },
|
|
{ userId: 6, name: "Bob", username: "", token: "m2", enabled: true },
|
|
],
|
|
};
|
|
|
|
beforeEach(() => {
|
|
mocks.getEventLinks.mockReset().mockResolvedValue(links);
|
|
mocks.setLinkEnabled.mockReset().mockResolvedValue(undefined);
|
|
});
|
|
|
|
describe("RegistrationQrCard", () => {
|
|
it("lists the event code and each team member code with a QR", async () => {
|
|
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite />);
|
|
await screen.findByText("Aini (aini@cv)");
|
|
const values = screen.getAllByTestId("qr-svg").map((e) => e.getAttribute("data-value"));
|
|
expect(values).toEqual([`${window.location.origin}/r/ev1`, `${window.location.origin}/r/m1`, `${window.location.origin}/r/m2`]);
|
|
expect(screen.getByText(/dynamic-qr/)).toBeTruthy();
|
|
expect(mocks.getEventLinks).toHaveBeenCalledWith("u1");
|
|
});
|
|
|
|
it("labels a member without a username by name only", async () => {
|
|
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite />);
|
|
await screen.findByText("Bob");
|
|
expect(screen.queryByText(/\(\)/)).toBeNull();
|
|
});
|
|
|
|
it("names the PNG download after the event and the member's name and username", async () => {
|
|
const user = userEvent.setup();
|
|
const names: string[] = [];
|
|
vi.spyOn(HTMLCanvasElement.prototype, "toDataURL").mockReturnValue("data:image/png;base64,");
|
|
vi.spyOn(HTMLAnchorElement.prototype, "click").mockImplementation(function (this: HTMLAnchorElement) {
|
|
names.push(this.download);
|
|
});
|
|
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite />);
|
|
await screen.findByText("Aini (aini@cv)");
|
|
await user.click(screen.getAllByRole("button", { name: "Download PNG" })[1]);
|
|
expect(names).toEqual(["expo-aini-aini-cv-qr.png"]);
|
|
});
|
|
|
|
it("toggles a code through the API", async () => {
|
|
const user = userEvent.setup();
|
|
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite />);
|
|
await screen.findByText("Aini (aini@cv)");
|
|
await user.click(screen.getAllByRole("switch")[1]);
|
|
await waitFor(() => expect(mocks.setLinkEnabled).toHaveBeenCalledWith("u1", "m1", true));
|
|
});
|
|
|
|
it("hides the switches without write access", async () => {
|
|
render(<RegistrationQrCard eventUuid="u1" eventName="Expo" canWrite={false} />);
|
|
await screen.findByText("Aini (aini@cv)");
|
|
expect(screen.queryAllByRole("switch")).toHaveLength(0);
|
|
});
|
|
});
|