Files
co-lite/solvway-customer-portal/app/components/DialCodeSelect.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

84 lines
3.9 KiB
TypeScript

import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import DialCodeSelect from "./DialCodeSelect";
import { flagEmoji } from "@/app/lib/dialCodes";
describe("DialCodeSelect", () => {
it("renders the current dial code on the trigger", () => {
render(<DialCodeSelect value="+60" onChange={() => {}} />);
expect(screen.getByRole("button", { name: /\+60/ })).toBeInTheDocument();
});
it("is closed initially (no search box, no options)", () => {
render(<DialCodeSelect value="+60" onChange={() => {}} />);
expect(screen.queryByPlaceholderText(/search country or code/i)).toBeNull();
expect(screen.queryAllByRole("option")).toHaveLength(0);
});
it("opens, filters by typed text, and emits the chosen dial code", () => {
const onChange = vi.fn();
render(<DialCodeSelect value="+60" onChange={onChange} />);
fireEvent.click(screen.getByRole("button", { name: /\+60/ }));
fireEvent.change(screen.getByPlaceholderText(/search country or code/i), {
target: { value: "sing" },
});
const options = screen.getAllByRole("option");
expect(options).toHaveLength(1);
expect(options[0]).toHaveTextContent("Singapore");
fireEvent.click(options[0]);
expect(onChange).toHaveBeenCalledWith("+65");
});
it("pins Malaysia and Singapore to the top of the list", () => {
render(<DialCodeSelect value="+60" onChange={() => {}} />);
fireEvent.click(screen.getByRole("button", { name: /\+60/ }));
const options = screen.getAllByRole("option");
expect(options[0]).toHaveTextContent("Malaysia");
expect(options[1]).toHaveTextContent("Singapore");
});
it("closes on Escape", () => {
render(<DialCodeSelect value="+60" onChange={() => {}} />);
fireEvent.click(screen.getByRole("button", { name: /\+60/ }));
expect(screen.getByPlaceholderText(/search country or code/i)).toBeInTheDocument();
fireEvent.keyDown(document, { key: "Escape" });
expect(screen.queryByPlaceholderText(/search country or code/i)).toBeNull();
expect(screen.queryAllByRole("option")).toHaveLength(0);
});
it("closes on outside click", () => {
render(<DialCodeSelect value="+60" onChange={() => {}} />);
fireEvent.click(screen.getByRole("button", { name: /\+60/ }));
expect(screen.getByPlaceholderText(/search country or code/i)).toBeInTheDocument();
fireEvent.mouseDown(document.body);
expect(screen.queryByPlaceholderText(/search country or code/i)).toBeNull();
});
it("tracks the chosen country for a shared dial code, then reconciles the flag when value changes externally", () => {
// +1 is shared (Canada is the first match); pick United States explicitly.
const { rerender } = render(<DialCodeSelect value="+1" onChange={() => {}} />);
fireEvent.click(screen.getByRole("button", { name: /\+1/ }));
fireEvent.change(screen.getByPlaceholderText(/search country or code/i), {
target: { value: "United States" },
});
fireEvent.click(screen.getByRole("option", { name: /United States/ }));
// Trigger shows the explicitly chosen US flag, not the first +1 match (Canada).
expect(screen.getByRole("button", { name: /\+1/ })).toHaveTextContent(flagEmoji("US"));
// Parent changes the dial code externally → the stale US selection must be
// dropped so the flag reflects the new value (Malaysia for +60).
rerender(<DialCodeSelect value="+60" onChange={() => {}} />);
expect(screen.getByRole("button", { name: /\+60/ })).toHaveTextContent(flagEmoji("MY"));
});
it("shows a no-matches message when nothing matches", () => {
render(<DialCodeSelect value="+60" onChange={() => {}} />);
fireEvent.click(screen.getByRole("button", { name: /\+60/ }));
fireEvent.change(screen.getByPlaceholderText(/search country or code/i), {
target: { value: "zzzzz" },
});
expect(screen.queryAllByRole("option")).toHaveLength(0);
expect(screen.getByText(/no matches/i)).toBeInTheDocument();
});
});