fix(qr): tell same-named salespeople apart; bilingual consent tick box
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
This commit is contained in:
1 parent
2d1513aa80
commit
9163557945
8 files changed
+72
-20
No files matched your search
@@ -1,19 +1,25 @@
|
||||
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: () => <canvas />,
|
||||
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", token: "m1", enabled: false }],
|
||||
members: [
|
||||
{ userId: 5, name: "Aini", username: "aini@cv", token: "m1", enabled: false },
|
||||
{ userId: 6, name: "Bob", username: "", token: "m2", enabled: true },
|
||||
],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -24,24 +30,43 @@ beforeEach(() => {
|
||||
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");
|
||||
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`]);
|
||||
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");
|
||||
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");
|
||||
await screen.findByText("Aini (aini@cv)");
|
||||
expect(screen.queryAllByRole("switch")).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -50,9 +50,12 @@ export function RegistrationQrCard({ eventUuid, eventName, canWrite }: { eventUu
|
||||
{links && (
|
||||
<>
|
||||
<QrCodeItem label={t("QrEventCode")} eventName={eventName} token={links.event.token} enabled={links.event.enabled} onToggle={onToggle(links.event.token)} />
|
||||
{links.members.map((m) => (
|
||||
<QrCodeItem key={m.token} label={m.name} eventName={eventName} fileLabel={m.name} token={m.token} enabled={m.enabled} onToggle={onToggle(m.token)} />
|
||||
))}
|
||||
{links.members.map((m) => {
|
||||
const label = m.username ? `${m.name} (${m.username})` : m.name;
|
||||
return (
|
||||
<QrCodeItem key={m.token} label={label} eventName={eventName} fileLabel={label} token={m.token} enabled={m.enabled} onToggle={onToggle(m.token)} />
|
||||
);
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
</CardContent>
|
||||
|
||||
@@ -474,7 +474,8 @@ export const DEFAULT_MESSAGES: Record<string, string> = {
|
||||
RegInterests: "Interests (optional)",
|
||||
RegNoticeEn: "English",
|
||||
RegNoticeMs: "Bahasa Malaysia",
|
||||
RegConsent: "I have read the notice and agree",
|
||||
RegConsentEn: "I agree to {company} using my details as described above.",
|
||||
RegConsentMs: "Saya bersetuju {company} menggunakan butiran saya seperti yang diterangkan di atas.",
|
||||
RegSubmit: "Register",
|
||||
RegAnother: "Register another person",
|
||||
RegThanks: "Thank you, you're registered for {event}",
|
||||
|
||||
@@ -6,7 +6,7 @@ export interface RegistrationLink {
|
||||
}
|
||||
export interface EventLinks {
|
||||
event: RegistrationLink;
|
||||
members: (RegistrationLink & { userId: number; name: string })[];
|
||||
members: (RegistrationLink & { userId: number; name: string; username: string })[];
|
||||
}
|
||||
export interface MyLink extends RegistrationLink {
|
||||
eventUuid: string;
|
||||
|
||||
@@ -40,6 +40,13 @@ describe("/r/[token] public page", () => {
|
||||
expect(mocks.getRegistration).toHaveBeenCalledWith("tok1");
|
||||
});
|
||||
|
||||
it("shows the consent tick box text in English and Bahasa Malaysia with the company name", async () => {
|
||||
await ready();
|
||||
const box = screen.getByRole("checkbox", { name: /I agree to INS using my details as described above\./ });
|
||||
expect(box).toBeTruthy();
|
||||
expect(screen.getByText("Saya bersetuju INS menggunakan butiran saya seperti yang diterangkan di atas.")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("shows only the inactive message, no event details, when the code is off", async () => {
|
||||
mocks.getRegistration.mockRejectedValue(new ApiError("x", 404, { code: "CODE_INACTIVE" }));
|
||||
render(<RegisterPage />);
|
||||
@@ -65,7 +72,7 @@ describe("/r/[token] public page", () => {
|
||||
await user.type(screen.getByLabelText("Company"), "Acme");
|
||||
await user.type(screen.getByLabelText("Job title"), "Buyer");
|
||||
await user.click(screen.getByLabelText("Wellness"));
|
||||
await user.click(screen.getByLabelText("I have read the notice and agree"));
|
||||
await user.click(screen.getByLabelText(/I agree to INS using my details as described above\./));
|
||||
await user.click(screen.getByRole("button", { name: "Register" }));
|
||||
await screen.findByText("Thank you, you're registered for Expo 2026");
|
||||
expect(mocks.submitRegistration).toHaveBeenCalledWith("tok1", {
|
||||
@@ -75,7 +82,7 @@ describe("/r/[token] public page", () => {
|
||||
await user.click(screen.getByRole("button", { name: "Register another person" }));
|
||||
const name = (await screen.findByLabelText("Your name")) as HTMLInputElement;
|
||||
expect(name.value).toBe("");
|
||||
expect((screen.getByLabelText("I have read the notice and agree") as HTMLElement).getAttribute("aria-checked")).toBe("false");
|
||||
expect((screen.getByLabelText(/I agree to INS using my details as described above\./) as HTMLElement).getAttribute("aria-checked")).toBe("false");
|
||||
expect((screen.getByLabelText("Wellness") as HTMLElement).getAttribute("aria-checked")).toBe("false");
|
||||
expect((document.querySelector('input[name="website"]') as HTMLInputElement).value).toBe("");
|
||||
});
|
||||
@@ -111,7 +118,7 @@ describe("/r/[token] public page", () => {
|
||||
await user.type(screen.getByLabelText("Email"), "a@b.co");
|
||||
await user.click(screen.getByRole("button", { name: "Register" }));
|
||||
const msg = await screen.findByText("Consent is required.");
|
||||
const box = screen.getByLabelText("I have read the notice and agree");
|
||||
const box = screen.getByLabelText(/I agree to INS using my details as described above\./);
|
||||
expect(box.getAttribute("aria-describedby")).toBe(msg.id);
|
||||
expect(box.getAttribute("aria-invalid")).toBe("true");
|
||||
});
|
||||
@@ -142,7 +149,7 @@ describe("/r/[token] public page", () => {
|
||||
await ready();
|
||||
expect(screen.getByLabelText("Your name").className).toContain("h-11");
|
||||
expect(screen.getByRole("button", { name: "Register" }).className).toContain("h-11");
|
||||
for (const l of ["Wellness", "I have read the notice and agree"]) {
|
||||
for (const l of ["Wellness", /I agree to INS using my details/]) {
|
||||
const label = document.querySelector(`label[for="${screen.getByLabelText(l).id}"]`)!;
|
||||
expect(label.className).toContain("w-full");
|
||||
expect(label.className).toContain("min-h-11");
|
||||
@@ -155,7 +162,7 @@ describe("/r/[token] public page", () => {
|
||||
await ready();
|
||||
await user.type(screen.getByLabelText("Your name"), "Aini");
|
||||
await user.type(screen.getByLabelText("Email"), "a@b.co");
|
||||
await user.click(screen.getByLabelText("I have read the notice and agree"));
|
||||
await user.click(screen.getByLabelText(/I agree to INS using my details as described above\./));
|
||||
await user.click(screen.getByRole("button", { name: "Register" }));
|
||||
await screen.findByText("Too many registrations from this device. Try again later.");
|
||||
mocks.submitRegistration.mockRejectedValueOnce(new ApiError("x", 404, { code: "CODE_INACTIVE" }));
|
||||
|
||||
@@ -151,7 +151,10 @@ export default function RegisterPage() {
|
||||
</div>
|
||||
<Label htmlFor="reg-consent" className={ROW}>
|
||||
<Checkbox id="reg-consent" className={BOX} checked={form.consent} onCheckedChange={(on) => set({ consent: on === true })} aria-invalid={!!errors.consent} aria-describedby={errors.consent ? "reg-consent-error" : undefined} />
|
||||
{t("RegConsent")}
|
||||
<span>
|
||||
<span className="block">{t("RegConsentEn", { company: info.companyName })}</span>
|
||||
<span className="block">{t("RegConsentMs", { company: info.companyName })}</span>
|
||||
</span>
|
||||
</Label>
|
||||
{errors.consent && <p id="reg-consent-error" className="text-sm text-destructive">{errors.consent}</p>}
|
||||
{/* Honeypot: people never see or reach it; bots fill it and the server drops the submission. */}
|
||||
|
||||
+1
@@ -59,6 +59,7 @@ public class RegistrationLinkService {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("userId", id.longValue());
|
||||
m.put("name", u == null ? String.valueOf(id) : String.valueOf(u.get("name")));
|
||||
m.put("username", u == null || u.get("username") == null ? "" : String.valueOf(u.get("username")));
|
||||
m.put("token", row.get("token"));
|
||||
m.put("enabled", row.get("enabled"));
|
||||
members.add(m);
|
||||
|
||||
+15
-3
@@ -78,7 +78,7 @@ class RegistrationLinkServiceTest {
|
||||
void close() { http.close(); }
|
||||
|
||||
private void stubUser(long id, String name) {
|
||||
Map<String, Object> u = new HashMap<>(Map.of("id", id, "name", name, "type", "A", "companyId", COMPANY, "enabled", true));
|
||||
Map<String, Object> u = new HashMap<>(Map.of("id", id, "name", name, "username", "sales" + id, "type", "A", "companyId", COMPANY, "enabled", true));
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(id))).thenReturn(ResponseEntity.ok(u));
|
||||
}
|
||||
|
||||
@@ -100,11 +100,23 @@ class RegistrationLinkServiceTest {
|
||||
|
||||
assertThat((Map<String, Object>) out.get("event")).containsEntry("token", "ev1").containsEntry("enabled", true);
|
||||
assertThat((List<Map<String, Object>>) out.get("members")).containsExactly(
|
||||
Map.of("userId", 21L, "name", "Sales One", "token", "t21", "enabled", true),
|
||||
Map.of("userId", 22L, "name", "Sales Two", "token", "t22", "enabled", false));
|
||||
Map.of("userId", 21L, "name", "Sales One", "username", "sales21", "token", "t21", "enabled", true),
|
||||
Map.of("userId", 22L, "name", "Sales Two", "username", "sales22", "token", "t22", "enabled", false));
|
||||
assertThat(posted(ENSURE)).containsEntry("companyId", COMPANY).containsEntry("eventUuid", EV).containsEntry("salespersonUserIds", List.of(21L, 22L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void links_memberWithoutUsername_getsEmptyUsername() {
|
||||
Map<String, Object> u = new HashMap<>(Map.of("id", 21L, "name", "Sales One", "type", "A", "companyId", COMPANY, "enabled", true));
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(21L))).thenReturn(ResponseEntity.ok(u));
|
||||
when(rest.postForEntity(eq(ENSURE), any(ParameterizedTypeReference.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(List.of(Map.of("token", "t21", "enabled", true, "salespersonUserId", 21L))));
|
||||
|
||||
List<Map<String, Object>> members = (List<Map<String, Object>>) service.links(String.valueOf(EV)).get("members");
|
||||
|
||||
assertThat(members.get(0)).containsEntry("username", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
void setEnabled_passesEventCompanyAndCallerUsername() {
|
||||
when(rest.postForEntity(eq(SET), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(Map.of("token", "t21", "enabled", false)));
|
||||
|
||||
Reference in new issue
Block a user