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
182 lines
7.2 KiB
TypeScript
182 lines
7.2 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";
|
|
|
|
const svc = vi.hoisted(() => ({ createMessage: vi.fn(), updateMessage: vi.fn() }));
|
|
vi.mock("@/app/lib/services/messages", async () => {
|
|
const actual = await vi.importActual<typeof import("@/app/lib/services/messages")>(
|
|
"@/app/lib/services/messages",
|
|
);
|
|
return {
|
|
...actual,
|
|
createMessage: (p: unknown) => svc.createMessage(p),
|
|
updateMessage: (id: number, p: unknown) => svc.updateMessage(id, p),
|
|
};
|
|
});
|
|
|
|
import { MessageEditSheet } from "./MessageEditSheet";
|
|
|
|
const initial = {
|
|
id: 7,
|
|
code: "OrderCount",
|
|
description: "",
|
|
area: "A" as const,
|
|
perLocale: { en_US: "{count} items", zh_CN: "{count} 件" },
|
|
};
|
|
|
|
function renderEdit(init = initial) {
|
|
const onClose = vi.fn();
|
|
const onSaved = vi.fn();
|
|
render(<MessageEditSheet mode="edit" initial={init} onClose={onClose} onSaved={onSaved} />);
|
|
return { onClose, onSaved };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
svc.createMessage.mockReset();
|
|
svc.updateMessage.mockReset();
|
|
});
|
|
|
|
describe("MessageEditSheet", () => {
|
|
it("edit mode shows code and area as text and saves the original code/area", async () => {
|
|
const user = userEvent.setup();
|
|
svc.updateMessage.mockResolvedValue({});
|
|
const { onSaved, onClose } = renderEdit();
|
|
expect(screen.getByText("OrderCount")).toBeInTheDocument();
|
|
expect(screen.queryByDisplayValue("OrderCount")).not.toBeInTheDocument();
|
|
expect(screen.queryByRole("combobox", { name: "Area" })).not.toBeInTheDocument();
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(svc.updateMessage).toHaveBeenCalledTimes(1));
|
|
expect(svc.updateMessage.mock.calls[0][0]).toBe(7);
|
|
expect(svc.updateMessage.mock.calls[0][1]).toEqual(
|
|
expect.objectContaining({ code: "OrderCount", area: "A" }),
|
|
);
|
|
await waitFor(() => expect(onSaved).toHaveBeenCalled());
|
|
expect(onClose).toHaveBeenCalled();
|
|
});
|
|
|
|
it("add mode has an editable code and an area select", () => {
|
|
render(
|
|
<MessageEditSheet
|
|
mode="add"
|
|
initial={{ code: "", description: "", area: "A", perLocale: {} }}
|
|
onClose={vi.fn()}
|
|
onSaved={vi.fn()}
|
|
/>,
|
|
);
|
|
expect(screen.getByLabelText("Code")).toBeEnabled();
|
|
expect(screen.getByRole("combobox", { name: "Area" })).toBeInTheDocument();
|
|
});
|
|
|
|
it("renders one textarea per enabled locale, English first", () => {
|
|
renderEdit();
|
|
const areas = screen.getAllByRole("textbox").filter((e) => e.tagName === "TEXTAREA");
|
|
expect(areas.length).toBeGreaterThanOrEqual(2);
|
|
expect(screen.getByLabelText("English").tagName).toBe("TEXTAREA");
|
|
const all = Array.from(document.querySelectorAll("textarea"));
|
|
expect(all[0]).toBe(screen.getByLabelText("English"));
|
|
});
|
|
|
|
it("blocks Save when an edit introduces a placeholder mismatch", async () => {
|
|
const user = userEvent.setup();
|
|
renderEdit();
|
|
const zh = screen.getByLabelText("简体中文");
|
|
await user.clear(zh);
|
|
await user.type(zh, "件");
|
|
expect(screen.getByText(/Placeholders differ from English\. Missing: \{count\}/)).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: "Save" })).toBeDisabled();
|
|
});
|
|
|
|
it("shows a warning but keeps Save enabled for an initially mismatched row", () => {
|
|
renderEdit({ ...initial, perLocale: { en_US: "{count} items", zh_CN: "件" } });
|
|
expect(screen.getByText(/Already different from English before your edit/)).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: "Save" })).toBeEnabled();
|
|
});
|
|
|
|
it("closesWithoutConfirmWhenUnchanged", async () => {
|
|
const user = userEvent.setup();
|
|
const { onClose } = renderEdit();
|
|
await user.keyboard("{Escape}");
|
|
expect(onClose).toHaveBeenCalled();
|
|
expect(screen.queryByText("Discard changes?")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("asks before discarding edits: Cancel keeps the panel, Discard closes", async () => {
|
|
const user = userEvent.setup();
|
|
const { onClose } = renderEdit();
|
|
await user.type(screen.getByLabelText("English"), "x");
|
|
await user.keyboard("{Escape}");
|
|
expect(await screen.findByText("Discard changes?")).toBeInTheDocument();
|
|
await user.click(screen.getByRole("button", { name: "Cancel" }));
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
expect(screen.getByLabelText("English")).toBeInTheDocument();
|
|
await user.keyboard("{Escape}");
|
|
await user.click(await screen.findByRole("button", { name: "Discard" }));
|
|
expect(onClose).toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows a 409 duplicate-code reason inline", async () => {
|
|
const user = userEvent.setup();
|
|
svc.createMessage.mockRejectedValue(new Error("MsgsDuplicateCode"));
|
|
render(
|
|
<MessageEditSheet
|
|
mode="add"
|
|
initial={{ code: "Dup", description: "", area: "A", perLocale: {} }}
|
|
onClose={vi.fn()}
|
|
onSaved={vi.fn()}
|
|
/>,
|
|
);
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
expect(await screen.findByText("A message with this code already exists in this area.")).toBeInTheDocument();
|
|
});
|
|
|
|
it("copy-code button writes the code to the clipboard", async () => {
|
|
const user = userEvent.setup();
|
|
renderEdit();
|
|
const spy = vi.spyOn(navigator.clipboard, "writeText").mockResolvedValue();
|
|
await user.click(screen.getByRole("button", { name: "Copy code" }));
|
|
expect(spy).toHaveBeenCalledWith("OrderCount");
|
|
});
|
|
|
|
it("edit mode sends the stored code and area untouched, even with stray whitespace", async () => {
|
|
const user = userEvent.setup();
|
|
svc.updateMessage.mockResolvedValue({});
|
|
renderEdit({ ...initial, code: "X\t" });
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(svc.updateMessage).toHaveBeenCalledTimes(1));
|
|
expect(svc.updateMessage.mock.calls[0][1]).toEqual(expect.objectContaining({ code: "X\t", area: "A" }));
|
|
});
|
|
|
|
it("add mode disables Save while the code is empty or whitespace", async () => {
|
|
const user = userEvent.setup();
|
|
render(
|
|
<MessageEditSheet
|
|
mode="add"
|
|
initial={{ code: "", description: "", area: "A", perLocale: {} }}
|
|
onClose={vi.fn()}
|
|
onSaved={vi.fn()}
|
|
/>,
|
|
);
|
|
expect(screen.getByRole("button", { name: "Save" })).toBeDisabled();
|
|
await user.type(screen.getByLabelText("Code"), " ");
|
|
expect(screen.getByRole("button", { name: "Save" })).toBeDisabled();
|
|
await user.type(screen.getByLabelText("Code"), "Ok");
|
|
expect(screen.getByRole("button", { name: "Save" })).toBeEnabled();
|
|
});
|
|
|
|
it("Cancel button closes when unchanged and asks to discard when edited", async () => {
|
|
const user = userEvent.setup();
|
|
const first = renderEdit();
|
|
await user.click(screen.getByRole("button", { name: "Cancel" }));
|
|
expect(first.onClose).toHaveBeenCalled();
|
|
});
|
|
|
|
it("Cancel button opens the discard confirm when there are edits", async () => {
|
|
const user = userEvent.setup();
|
|
const { onClose } = renderEdit();
|
|
await user.type(screen.getByLabelText("English"), "x");
|
|
await user.click(screen.getByRole("button", { name: "Cancel" }));
|
|
expect(await screen.findByText("Discard changes?")).toBeInTheDocument();
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
});
|
|
});
|