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( {}} />); expect(screen.getByRole("button", { name: /\+60/ })).toBeInTheDocument(); }); it("is closed initially (no search box, no options)", () => { render( {}} />); 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(); 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( {}} />); 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( {}} />); 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( {}} />); 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( {}} />); 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( {}} />); expect(screen.getByRole("button", { name: /\+60/ })).toHaveTextContent(flagEmoji("MY")); }); it("shows a no-matches message when nothing matches", () => { render( {}} />); 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(); }); });