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
76 lines
3.5 KiB
TypeScript
76 lines
3.5 KiB
TypeScript
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi } from "vitest";
|
|
import { GeneralTab } from "./GeneralTab";
|
|
import type { AdminLocationNode } from "@/app/lib/types";
|
|
|
|
const node: AdminLocationNode = {
|
|
id: 35,
|
|
code: "MYB",
|
|
name: "Kuala Lumpur",
|
|
type: "B",
|
|
status: "Y",
|
|
parent: { id: 21 },
|
|
};
|
|
|
|
describe("GeneralTab", () => {
|
|
it("shows the current name, code and status", () => {
|
|
render(<GeneralTab node={node} onSave={vi.fn()} />);
|
|
expect(screen.getByLabelText("Name")).toHaveValue("Kuala Lumpur");
|
|
expect(screen.getByLabelText("Code")).toHaveValue("MYB");
|
|
});
|
|
|
|
// Live-verification fix (fix-currency-gate-and-type-labels): the Type field
|
|
// was rendering the raw HierarchyType code (e.g. "G") instead of its full
|
|
// name. A node with type "G" must show "Global", not "G".
|
|
it("shows the full hierarchy type name instead of the raw code", () => {
|
|
render(<GeneralTab node={{ ...node, type: "G" }} onSave={vi.fn()} />);
|
|
expect(screen.getByText("Global")).toBeInTheDocument();
|
|
expect(screen.queryByText("G")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("falls back to the raw code for an unknown type", () => {
|
|
// Cast bypasses the nominal "G" | "C" | "R" | "B" union to exercise the
|
|
// lookup's fallback for any future/unmapped hierarchy code.
|
|
const unknownTypeNode = { ...node, type: "ZZ" as AdminLocationNode["type"] };
|
|
render(<GeneralTab node={unknownTypeNode} onSave={vi.fn()} />);
|
|
expect(screen.getByText("ZZ")).toBeInTheDocument();
|
|
});
|
|
|
|
it("rejects an empty name before calling onSave", async () => {
|
|
const onSave = vi.fn();
|
|
render(<GeneralTab node={node} onSave={onSave} />);
|
|
fireEvent.change(screen.getByLabelText("Name"), { target: { value: "" } });
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
expect(await screen.findByText("Name is required.")).toBeInTheDocument();
|
|
expect(onSave).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("calls onSave with the changed fields and shows a success message", async () => {
|
|
const onSave = vi.fn().mockResolvedValue(undefined);
|
|
render(<GeneralTab node={node} onSave={onSave} />);
|
|
fireEvent.change(screen.getByLabelText("Name"), { target: { value: "Kuala Lumpur City" } });
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(onSave).toHaveBeenCalledWith({ name: "Kuala Lumpur City" }));
|
|
expect(await screen.findByText("Saved.")).toHaveClass("text-positive");
|
|
});
|
|
|
|
it("shows an error message when onSave rejects", async () => {
|
|
const onSave = vi.fn().mockRejectedValue(new Error("Code already in use"));
|
|
render(<GeneralTab node={node} onSave={onSave} />);
|
|
fireEvent.change(screen.getByLabelText("Name"), { target: { value: "New Name" } });
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
expect(await screen.findByText("Code already in use")).toBeInTheDocument();
|
|
});
|
|
|
|
it("disables Save while the request is pending", async () => {
|
|
let resolve!: () => void;
|
|
const onSave = vi.fn(() => new Promise<void>((r) => (resolve = r)));
|
|
render(<GeneralTab node={node} onSave={onSave} />);
|
|
fireEvent.change(screen.getByLabelText("Name"), { target: { value: "New Name" } });
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
expect(screen.getByRole("button", { name: "Save" })).toBeDisabled();
|
|
resolve();
|
|
await waitFor(() => expect(screen.getByRole("button", { name: "Save" })).not.toBeDisabled());
|
|
});
|
|
});
|