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
129 lines
5.7 KiB
TypeScript
129 lines
5.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
const svc = vi.hoisted(() => ({ exportMessages: vi.fn(), importMessages: 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,
|
|
exportMessages: (area?: unknown) => svc.exportMessages(area),
|
|
importMessages: (rows: unknown) => svc.importMessages(rows),
|
|
};
|
|
});
|
|
|
|
import { ImportMessagesDialog } from "./ImportMessagesDialog";
|
|
|
|
const en = (v: string) => JSON.stringify({ en_US: v });
|
|
|
|
async function upload(text: string) {
|
|
const user = userEvent.setup();
|
|
const onImported = vi.fn();
|
|
render(<ImportMessagesDialog onClose={vi.fn()} onImported={onImported} />);
|
|
await user.upload(
|
|
screen.getByLabelText("Catalog import file"),
|
|
new File([text], "c.json", { type: "application/json" }),
|
|
);
|
|
return { user, onImported };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
svc.exportMessages.mockReset();
|
|
svc.importMessages.mockReset();
|
|
});
|
|
|
|
describe("ImportMessagesDialog", () => {
|
|
it("previews with both areas and matches by area and case-insensitive code", async () => {
|
|
svc.exportMessages.mockResolvedValue([{ id: 1, code: "CART", area: "AP", content: en("Cart") }]);
|
|
await upload(JSON.stringify([{ id: 99, code: "cart", area: "AP", content: en("Basket") }]));
|
|
const dialog = await screen.findByRole("dialog");
|
|
await within(dialog).findByText("Update");
|
|
expect(svc.exportMessages).toHaveBeenCalledWith(undefined);
|
|
expect(within(dialog).getByText("Basket")).toBeInTheDocument();
|
|
});
|
|
|
|
it("shows a translated message for a non-JSON file and sends nothing", async () => {
|
|
await upload("not json {");
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("This file isn't valid JSON.");
|
|
expect(screen.getByRole("alert")).not.toHaveTextContent(/SyntaxError|Unexpected/);
|
|
expect(svc.exportMessages).not.toHaveBeenCalled();
|
|
expect(svc.importMessages).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects a non-array file", async () => {
|
|
await upload("{}");
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("File must contain a JSON array");
|
|
expect(svc.importMessages).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("reports a row without code or area inline instead of crashing", async () => {
|
|
svc.exportMessages.mockResolvedValue([]);
|
|
await upload(JSON.stringify([{ code: "Ok", area: "A", content: en("x") }, { code: "NoArea", content: en("y") }, null]));
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(await within(dialog).findByText("Ok")).toBeInTheDocument();
|
|
expect(within(dialog).getByText(/Row 2 \(NoArea\)/)).toBeInTheDocument();
|
|
expect(within(dialog).getByText(/Row 3/)).toBeInTheDocument();
|
|
});
|
|
|
|
it("renders the result error list outside any paragraph and translates reason codes", async () => {
|
|
svc.exportMessages.mockResolvedValue([]);
|
|
svc.importMessages.mockResolvedValue({
|
|
created: 0,
|
|
updated: 0,
|
|
errors: [{ index: 0, code: "Dup", error: "MsgsDuplicateCode" }],
|
|
});
|
|
const { user, onImported } = await upload(JSON.stringify([{ code: "Dup", area: "A", content: en("x") }]));
|
|
await user.click(await screen.findByRole("button", { name: "Confirm import" }));
|
|
const item = await screen.findByText(/Row 1 \(Dup\)/);
|
|
expect(item.closest("p")).toBeNull();
|
|
expect(item.textContent).not.toContain("MsgsDuplicateCode");
|
|
expect(onImported).toHaveBeenCalled();
|
|
});
|
|
|
|
it("gives the Import action an icon", async () => {
|
|
svc.exportMessages.mockResolvedValue([]);
|
|
await upload(JSON.stringify([{ code: "A1", area: "A", content: en("x") }]));
|
|
const btn = await screen.findByRole("button", { name: "Confirm import" });
|
|
await waitFor(() => expect(btn.querySelector("svg")).not.toBeNull());
|
|
});
|
|
|
|
it("does not call importMessages until Confirm is clicked", async () => {
|
|
svc.exportMessages.mockResolvedValue([]);
|
|
const { user } = await upload(JSON.stringify([{ code: "A1", area: "A", content: en("x") }]));
|
|
await screen.findByRole("button", { name: "Confirm import" });
|
|
expect(svc.importMessages).not.toHaveBeenCalled();
|
|
svc.importMessages.mockResolvedValue({ created: 1, updated: 0, errors: [] });
|
|
await user.click(screen.getByRole("button", { name: "Confirm import" }));
|
|
await waitFor(() => expect(svc.importMessages).toHaveBeenCalledTimes(1));
|
|
});
|
|
|
|
it("lists a row with an unknown area as invalid", async () => {
|
|
svc.exportMessages.mockResolvedValue([]);
|
|
await upload(JSON.stringify([{ code: "Ok", area: "A", content: en("x") }, { code: "Odd", area: "X", content: en("y") }]));
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(await within(dialog).findByText(/Row 2 \(Odd\)/)).toBeInTheDocument();
|
|
expect(within(dialog).queryByText("Odd")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("numbers server errors by original file position", async () => {
|
|
svc.exportMessages.mockResolvedValue([]);
|
|
svc.importMessages.mockResolvedValue({
|
|
created: 0,
|
|
updated: 0,
|
|
errors: [{ index: 1, code: "Bad", error: "MsgsDuplicateCode" }],
|
|
});
|
|
const { user } = await upload(
|
|
JSON.stringify([
|
|
{ code: "NoArea", content: en("x") },
|
|
{ code: "Good", area: "A", content: en("y") },
|
|
{ code: "Bad", area: "A", content: en("z") },
|
|
]),
|
|
);
|
|
await user.click(await screen.findByRole("button", { name: "Confirm import" }));
|
|
// sent rows: [Good, Bad]; server index 1 (0-based) = Bad = file row 3
|
|
expect(await screen.findByText(/Row 3 \(Bad\)/)).toBeInTheDocument();
|
|
});
|
|
});
|