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
338 lines
16 KiB
TypeScript
338 lines
16 KiB
TypeScript
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { WarehouseFormDialog } from "./WarehouseFormDialog";
|
|
import type { AdminWarehouse } from "@/app/lib/types";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
createAdminWarehouse: vi.fn(),
|
|
updateAdminWarehouse: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminWarehouses", () => ({
|
|
createAdminWarehouse: mocks.createAdminWarehouse,
|
|
updateAdminWarehouse: mocks.updateAdminWarehouse,
|
|
}));
|
|
|
|
const PARENT_OPTIONS: AdminWarehouse[] = [
|
|
{ id: 1, code: "HQ", name: "Headquarters", type: "W", status: "Y", parentId: null },
|
|
];
|
|
|
|
beforeEach(() => {
|
|
mocks.createAdminWarehouse.mockReset();
|
|
mocks.updateAdminWarehouse.mockReset();
|
|
});
|
|
|
|
describe("WarehouseFormDialog", () => {
|
|
it("create mode: submits code/name/type/parent/address and fires onSuccess", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.createAdminWarehouse.mockResolvedValue({
|
|
id: 2,
|
|
code: "HQS2",
|
|
name: "New Store",
|
|
type: "S",
|
|
status: "Y",
|
|
parentId: 1,
|
|
});
|
|
const onSuccess = vi.fn();
|
|
|
|
render(<WarehouseFormDialog mode="create" parentOptions={PARENT_OPTIONS} onSuccess={onSuccess} />);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add warehouse" }));
|
|
await user.type(screen.getByLabelText("Code"), "HQS2");
|
|
await user.type(screen.getByLabelText("Name"), "New Store");
|
|
await user.click(screen.getByLabelText("Type"));
|
|
await user.click(await screen.findByRole("option", { name: "Store" }));
|
|
await user.click(screen.getByLabelText("Parent"));
|
|
await user.click(await screen.findByRole("option", { name: "HQ — Headquarters" }));
|
|
await user.type(screen.getByLabelText("Address"), "123 Main St");
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
|
|
await waitFor(() =>
|
|
expect(mocks.createAdminWarehouse).toHaveBeenCalledWith({
|
|
code: "HQS2",
|
|
name: "New Store",
|
|
type: "S",
|
|
status: "Y",
|
|
parentId: 1,
|
|
address: "123 Main St",
|
|
}),
|
|
);
|
|
await waitFor(() => expect(onSuccess).toHaveBeenCalled());
|
|
});
|
|
|
|
it("edit mode: the code field is read-only, and submitting calls updateAdminWarehouse with edits", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.updateAdminWarehouse.mockResolvedValue({});
|
|
const onSuccess = vi.fn();
|
|
const warehouse: AdminWarehouse = {
|
|
id: 5,
|
|
code: "HQS",
|
|
name: "Old Store",
|
|
type: "S",
|
|
status: "Y",
|
|
parentId: 1,
|
|
address: "Old address",
|
|
};
|
|
|
|
render(
|
|
<WarehouseFormDialog
|
|
mode="edit"
|
|
warehouse={warehouse}
|
|
parentOptions={PARENT_OPTIONS}
|
|
onSuccess={onSuccess}
|
|
/>,
|
|
);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Edit" }));
|
|
expect(screen.getByLabelText("Code")).toBeDisabled();
|
|
|
|
await user.clear(screen.getByLabelText("Name"));
|
|
await user.type(screen.getByLabelText("Name"), "Renamed Store");
|
|
await user.clear(screen.getByLabelText("Address"));
|
|
await user.type(screen.getByLabelText("Address"), "New address");
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(mocks.updateAdminWarehouse).toHaveBeenCalledWith(5, {
|
|
name: "Renamed Store",
|
|
type: "S",
|
|
status: "Y",
|
|
parentId: 1,
|
|
address: "New address",
|
|
}),
|
|
);
|
|
expect(onSuccess).toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows an error and does not fire onSuccess when the service rejects", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.createAdminWarehouse.mockRejectedValue(new Error("Duplicate code"));
|
|
const onSuccess = vi.fn();
|
|
|
|
render(<WarehouseFormDialog mode="create" parentOptions={PARENT_OPTIONS} onSuccess={onSuccess} />);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add warehouse" }));
|
|
await user.type(screen.getByLabelText("Code"), "DUP");
|
|
await user.type(screen.getByLabelText("Name"), "Dup Store");
|
|
await user.click(screen.getByLabelText("Type"));
|
|
await user.click(await screen.findByRole("option", { name: "Store" }));
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
|
|
expect(await screen.findByText("Duplicate code")).toBeInTheDocument();
|
|
expect(onSuccess).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("caps the address field at 255 characters (VARCHAR(255) column)", async () => {
|
|
const user = userEvent.setup();
|
|
render(<WarehouseFormDialog mode="create" parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add warehouse" }));
|
|
|
|
expect(screen.getByLabelText("Address")).toHaveAttribute("maxlength", "255");
|
|
});
|
|
|
|
it("type dropdown offers exactly Warehouse, Master store, and Store — no BC or Key-in point", async () => {
|
|
const user = userEvent.setup();
|
|
render(<WarehouseFormDialog mode="create" parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add warehouse" }));
|
|
await user.click(screen.getByLabelText("Type"));
|
|
|
|
// Required options must be present
|
|
expect(await screen.findByRole("option", { name: "Warehouse" })).toBeInTheDocument();
|
|
expect(screen.getByRole("option", { name: "Master store" })).toBeInTheDocument();
|
|
expect(screen.getByRole("option", { name: "Store" })).toBeInTheDocument();
|
|
|
|
// Removed options must be absent
|
|
expect(screen.queryByRole("option", { name: "BC" })).not.toBeInTheDocument();
|
|
expect(screen.queryByRole("option", { name: "Business centre" })).not.toBeInTheDocument();
|
|
expect(screen.queryByRole("option", { name: "Key-in point" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("create mode: offers Online store and submits type ONLINE", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.createAdminWarehouse.mockResolvedValue({
|
|
id: 3, code: "MYB-ON", name: "HQ Online store", type: "ONLINE", status: "Y", parentId: 1,
|
|
});
|
|
|
|
render(<WarehouseFormDialog mode="create" parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add warehouse" }));
|
|
await user.type(screen.getByLabelText("Code"), "MYB-ON");
|
|
await user.type(screen.getByLabelText("Name"), "HQ Online store");
|
|
await user.click(screen.getByLabelText("Type"));
|
|
await user.click(await screen.findByRole("option", { name: "Online store" }));
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
|
|
await waitFor(() =>
|
|
expect(mocks.createAdminWarehouse).toHaveBeenCalledWith(expect.objectContaining({ type: "ONLINE" })),
|
|
);
|
|
});
|
|
|
|
describe("online fulfilment fields", () => {
|
|
async function openOnline(user: ReturnType<typeof userEvent.setup>) {
|
|
render(<WarehouseFormDialog mode="create" parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
await user.click(screen.getByRole("button", { name: "Add warehouse" }));
|
|
await user.type(screen.getByLabelText("Code"), "MYB-ON");
|
|
await user.type(screen.getByLabelText("Name"), "HQ Online store");
|
|
await user.click(screen.getByLabelText("Type"));
|
|
await user.click(await screen.findByRole("option", { name: "Online store" }));
|
|
}
|
|
|
|
it("ONLINE shows Fulfilment mode and Routing priority; QianYi warehouse name only for QianYi", async () => {
|
|
const user = userEvent.setup();
|
|
await openOnline(user);
|
|
|
|
expect(screen.getByLabelText("Fulfilment mode")).toBeInTheDocument();
|
|
expect(screen.getByLabelText("Routing priority")).toBeInTheDocument();
|
|
expect(screen.queryByLabelText("QianYi warehouse name")).not.toBeInTheDocument();
|
|
|
|
await user.click(screen.getByLabelText("Fulfilment mode"));
|
|
expect(await screen.findByRole("option", { name: "Manual" })).toBeInTheDocument();
|
|
await user.click(screen.getByRole("option", { name: "QianYi (automated)" }));
|
|
|
|
expect(screen.getByLabelText("QianYi warehouse name")).toBeInTheDocument();
|
|
});
|
|
|
|
it("saves fulfilmentMode, numeric onlinePriority and qyWarehouseCode", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.createAdminWarehouse.mockResolvedValue({});
|
|
await openOnline(user);
|
|
|
|
await user.click(screen.getByLabelText("Fulfilment mode"));
|
|
await user.click(await screen.findByRole("option", { name: "QianYi (automated)" }));
|
|
await user.clear(screen.getByLabelText("Routing priority"));
|
|
await user.type(screen.getByLabelText("Routing priority"), "5");
|
|
await user.type(screen.getByLabelText("QianYi warehouse name"), "QY-HQ");
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
|
|
await waitFor(() =>
|
|
expect(mocks.createAdminWarehouse).toHaveBeenCalledWith(
|
|
expect.objectContaining({ type: "ONLINE", fulfilmentMode: "QIANYI", onlinePriority: 5, qyWarehouseCode: "QY-HQ" }),
|
|
),
|
|
);
|
|
});
|
|
|
|
it("a MANUAL ONLINE save sends mode and priority but no qyWarehouseCode", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.createAdminWarehouse.mockResolvedValue({});
|
|
await openOnline(user);
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
|
|
await waitFor(() => expect(mocks.createAdminWarehouse).toHaveBeenCalled());
|
|
const body = mocks.createAdminWarehouse.mock.calls[0][0];
|
|
expect(body).toMatchObject({ fulfilmentMode: "MANUAL", onlinePriority: 100 });
|
|
expect(body).not.toHaveProperty("qyWarehouseCode");
|
|
});
|
|
|
|
it("edit mode seeds the loaded fulfilment settings and a name-only save resends them", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.updateAdminWarehouse.mockResolvedValue({});
|
|
const wh: AdminWarehouse = {
|
|
id: 9, code: "MYB-ON", name: "HQ Online", type: "ONLINE", status: "Y", parentId: null,
|
|
fulfilmentMode: "QIANYI", onlinePriority: 10, qyWarehouseCode: "QY-MY-01",
|
|
};
|
|
render(<WarehouseFormDialog mode="edit" warehouse={wh} parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
await user.click(screen.getByRole("button", { name: "Edit" }));
|
|
expect(screen.getByLabelText("Routing priority")).toHaveValue(10);
|
|
expect(screen.getByLabelText("QianYi warehouse name")).toHaveValue("QY-MY-01");
|
|
await user.type(screen.getByLabelText("Name"), "X");
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(mocks.updateAdminWarehouse).toHaveBeenCalled());
|
|
expect(mocks.updateAdminWarehouse.mock.calls[0][1]).toMatchObject({
|
|
name: "HQ OnlineX", fulfilmentMode: "QIANYI", onlinePriority: 10, qyWarehouseCode: "QY-MY-01",
|
|
});
|
|
});
|
|
|
|
it("reseeds from the current warehouse each time it opens (rerender with another warehouse)", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.updateAdminWarehouse.mockResolvedValue({});
|
|
const a: AdminWarehouse = { id: 1, code: "HQ", name: "Headquarters", type: "W", status: "Y", parentId: null };
|
|
const b: AdminWarehouse = {
|
|
id: 9, code: "MYB-ON", name: "HQ Online", type: "ONLINE", status: "Y", parentId: null,
|
|
fulfilmentMode: "QIANYI", onlinePriority: 10, qyWarehouseCode: "QY-MY-01",
|
|
};
|
|
const { rerender } = render(<WarehouseFormDialog mode="edit" warehouse={a} parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
rerender(<WarehouseFormDialog mode="edit" warehouse={b} parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
await user.click(screen.getByRole("button", { name: "Edit" }));
|
|
expect(screen.getByLabelText("Code")).toHaveValue("MYB-ON");
|
|
expect(screen.getByLabelText("Name")).toHaveValue("HQ Online");
|
|
expect(screen.getByLabelText("Routing priority")).toHaveValue(10);
|
|
expect(screen.getByLabelText("QianYi warehouse name")).toHaveValue("QY-MY-01");
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(mocks.updateAdminWarehouse).toHaveBeenCalled());
|
|
expect(mocks.updateAdminWarehouse.mock.calls[0][0]).toBe(9);
|
|
expect(mocks.updateAdminWarehouse.mock.calls[0][1]).toMatchObject({
|
|
name: "HQ Online", type: "ONLINE", fulfilmentMode: "QIANYI", onlinePriority: 10, qyWarehouseCode: "QY-MY-01",
|
|
});
|
|
});
|
|
|
|
it("discards unsaved edits after cancel and reopen", async () => {
|
|
const user = userEvent.setup();
|
|
const wh: AdminWarehouse = { id: 1, code: "HQ", name: "Headquarters", type: "W", status: "Y", parentId: null };
|
|
render(<WarehouseFormDialog mode="edit" warehouse={wh} parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
await user.click(screen.getByRole("button", { name: "Edit" }));
|
|
await user.type(screen.getByLabelText("Name"), " changed");
|
|
await user.keyboard("{Escape}");
|
|
await waitFor(() => expect(screen.queryByLabelText("Name")).not.toBeInTheDocument());
|
|
await user.click(screen.getByRole("button", { name: "Edit" }));
|
|
expect(screen.getByLabelText("Name")).toHaveValue("Headquarters");
|
|
});
|
|
|
|
it.each([["", "empty"], ["0", "zero"], ["2.5", "fractional"]])("rejects routing priority %j (%s)", async (value) => {
|
|
const user = userEvent.setup();
|
|
await openOnline(user);
|
|
const input = screen.getByLabelText("Routing priority");
|
|
await user.clear(input);
|
|
if (value) await user.type(input, value);
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
expect(await screen.findByText("Routing priority must be a whole number of at least 1")).toBeInTheDocument();
|
|
expect(mocks.createAdminWarehouse).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("QianYi with an empty warehouse name sends qyWarehouseCode \"\" so the backend clears it", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.createAdminWarehouse.mockResolvedValue({});
|
|
await openOnline(user);
|
|
await user.click(screen.getByLabelText("Fulfilment mode"));
|
|
await user.click(await screen.findByRole("option", { name: "QianYi (automated)" }));
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
await waitFor(() => expect(mocks.createAdminWarehouse).toHaveBeenCalled());
|
|
expect(mocks.createAdminWarehouse.mock.calls[0][0]).toMatchObject({ fulfilmentMode: "QIANYI", qyWarehouseCode: "" });
|
|
});
|
|
|
|
it("type S renders none of the fulfilment fields and sends none", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.createAdminWarehouse.mockResolvedValue({});
|
|
render(<WarehouseFormDialog mode="create" parentOptions={PARENT_OPTIONS} onSuccess={vi.fn()} />);
|
|
await user.click(screen.getByRole("button", { name: "Add warehouse" }));
|
|
await user.type(screen.getByLabelText("Code"), "HQS2");
|
|
await user.type(screen.getByLabelText("Name"), "Store");
|
|
await user.click(screen.getByLabelText("Type"));
|
|
await user.click(await screen.findByRole("option", { name: "Store" }));
|
|
|
|
expect(screen.queryByLabelText("Fulfilment mode")).not.toBeInTheDocument();
|
|
expect(screen.queryByLabelText("Routing priority")).not.toBeInTheDocument();
|
|
expect(screen.queryByLabelText("QianYi warehouse name")).not.toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
await waitFor(() => expect(mocks.createAdminWarehouse).toHaveBeenCalled());
|
|
const body = mocks.createAdminWarehouse.mock.calls[0][0];
|
|
for (const k of ["fulfilmentMode", "onlinePriority", "qyWarehouseCode"]) expect(body).not.toHaveProperty(k);
|
|
});
|
|
|
|
it("shows a 409 message and keeps the dialog open", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.createAdminWarehouse.mockRejectedValue(new Error("An active QianYi warehouse already exists: MYB-ON"));
|
|
await openOnline(user);
|
|
await user.click(screen.getByLabelText("Fulfilment mode"));
|
|
await user.click(await screen.findByRole("option", { name: "QianYi (automated)" }));
|
|
await user.type(screen.getByLabelText("QianYi warehouse name"), "QY-2");
|
|
await user.click(screen.getByRole("button", { name: "Create" }));
|
|
|
|
expect(await screen.findByText("An active QianYi warehouse already exists: MYB-ON")).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: "Create" })).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|