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
579 lines
23 KiB
TypeScript
579 lines
23 KiB
TypeScript
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
adjustStock: vi.fn(),
|
|
getProductVariations: vi.fn(),
|
|
listAdminWarehouses: vi.fn(),
|
|
listProducts: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@/app/lib/services/adminStock", () => ({
|
|
adjustStock: mocks.adjustStock,
|
|
getProductVariations: mocks.getProductVariations,
|
|
}));
|
|
vi.mock("@/app/lib/services/adminWarehouses", () => ({
|
|
listAdminWarehouses: mocks.listAdminWarehouses,
|
|
}));
|
|
vi.mock("@/app/lib/services/adminProduct", () => ({
|
|
listProducts: mocks.listProducts,
|
|
}));
|
|
|
|
// The History link shows only to users holding BO_MOVEMENT_HISTORY (merge-inventory-page).
|
|
const auth = vi.hoisted(() => ({ hasModule: vi.fn() }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ hasModule: auth.hasModule }),
|
|
}));
|
|
|
|
import { AdjustmentForm } from "./AdjustmentForm";
|
|
|
|
// ─── fixture data ────────────────────────────────────────────────────────────
|
|
|
|
const WAREHOUSE_A = {
|
|
id: 11,
|
|
code: "MYB-M",
|
|
name: "MYB-Master Store",
|
|
type: "M",
|
|
parentId: null,
|
|
status: "Y",
|
|
};
|
|
const WAREHOUSE_B = {
|
|
id: 10,
|
|
code: "MYB-W",
|
|
name: "MYB-HQ",
|
|
type: "W",
|
|
parentId: null,
|
|
status: "Y",
|
|
};
|
|
const PRODUCTS = [
|
|
{ id: "1001", productCode: "P001", productName: "Baby Milk", productStatus: "Y" },
|
|
{ id: "1002", productCode: "P002", productName: "Baby Lotion", productStatus: "Y" },
|
|
];
|
|
const VARIATIONS = [
|
|
{ skuVariationId: "SV-001", variationId: "V-001", label: "100g" },
|
|
{ skuVariationId: "SV-002", variationId: "V-002", label: "250g" },
|
|
];
|
|
|
|
// ─── setup ───────────────────────────────────────────────────────────────────
|
|
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReset();
|
|
auth.hasModule.mockReturnValue(false);
|
|
mocks.adjustStock.mockReset();
|
|
mocks.getProductVariations.mockReset();
|
|
mocks.listAdminWarehouses.mockReset();
|
|
mocks.listProducts.mockReset();
|
|
|
|
mocks.listAdminWarehouses.mockResolvedValue([WAREHOUSE_A, WAREHOUSE_B]);
|
|
mocks.listProducts.mockResolvedValue({ items: PRODUCTS, total: 2 });
|
|
mocks.getProductVariations.mockResolvedValue(VARIATIONS);
|
|
});
|
|
|
|
// ─── helpers ─────────────────────────────────────────────────────────────────
|
|
|
|
async function waitForLoad() {
|
|
await screen.findByRole("combobox", { name: /^warehouse$/i });
|
|
}
|
|
|
|
async function pickProduct(user: ReturnType<typeof userEvent.setup>, productName: string) {
|
|
await user.click(screen.getByRole("combobox", { name: /^product$/i }));
|
|
await user.click(await screen.findByText(productName));
|
|
}
|
|
|
|
async function pickVariation(user: ReturnType<typeof userEvent.setup>, label: string) {
|
|
await user.click(await screen.findByRole("combobox", { name: /^variation$/i }));
|
|
await user.click(await screen.findByRole("option", { name: label }));
|
|
}
|
|
|
|
async function selectWarehouse(user: ReturnType<typeof userEvent.setup>, name: RegExp | string) {
|
|
await user.click(screen.getByRole("combobox", { name: /^warehouse$/i }));
|
|
await user.click(await screen.findByRole("option", { name }));
|
|
}
|
|
|
|
async function selectReason(user: ReturnType<typeof userEvent.setup>, label: string) {
|
|
await user.click(screen.getByRole("combobox", { name: /^reason$/i }));
|
|
await user.click(await screen.findByRole("option", { name: label }));
|
|
}
|
|
|
|
// ─── submit disabled states ───────────────────────────────────────────────────
|
|
|
|
describe("AdjustmentForm — submit gating", () => {
|
|
it("disables Submit when no warehouse is selected", async () => {
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
expect(screen.getByRole("button", { name: /^submit$/i })).toBeDisabled();
|
|
});
|
|
|
|
it("disables Submit when warehouse is chosen but reason is not", async () => {
|
|
const user = userEvent.setup();
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
|
|
// Reason not selected — Submit still disabled
|
|
expect(screen.getByRole("button", { name: /^submit$/i })).toBeDisabled();
|
|
});
|
|
|
|
it("disables Submit when warehouse and reason are chosen but no lines are complete", async () => {
|
|
const user = userEvent.setup();
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await selectReason(user, "Damage");
|
|
|
|
// No product/variation chosen — Submit still disabled
|
|
expect(screen.getByRole("button", { name: /^submit$/i })).toBeDisabled();
|
|
});
|
|
|
|
it("reason is REQUIRED — Submit remains disabled when only warehouse + lines are complete", async () => {
|
|
const user = userEvent.setup();
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
|
|
// Reason still blank — Submit must remain disabled
|
|
expect(screen.getByRole("button", { name: /^submit$/i })).toBeDisabled();
|
|
});
|
|
});
|
|
|
|
// ─── direction toggle ─────────────────────────────────────────────────────────
|
|
|
|
describe("AdjustmentForm — direction toggle", () => {
|
|
it("defaults to Increase (direction IN)", async () => {
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
// The Increase button should be pressed by default
|
|
expect(screen.getByRole("button", { name: /^increase$/i })).toHaveAttribute("aria-pressed", "true");
|
|
expect(screen.getByRole("button", { name: /^decrease$/i })).toHaveAttribute("aria-pressed", "false");
|
|
});
|
|
|
|
it("switches to Decrease (direction OUT) when the Decrease button is clicked", async () => {
|
|
const user = userEvent.setup();
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await user.click(screen.getByRole("button", { name: /^decrease$/i }));
|
|
|
|
expect(screen.getByRole("button", { name: /^decrease$/i })).toHaveAttribute("aria-pressed", "true");
|
|
expect(screen.getByRole("button", { name: /^increase$/i })).toHaveAttribute("aria-pressed", "false");
|
|
});
|
|
|
|
it("sends direction:IN when Increase is selected and form is submitted", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJI20260906001" });
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
// Increase is default — no extra click needed
|
|
await selectReason(user, "Count correction");
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(mocks.adjustStock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ direction: "IN" }),
|
|
),
|
|
);
|
|
});
|
|
|
|
it("sends direction:OUT when Decrease is selected and form is submitted", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJO20260906001" });
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await user.click(screen.getByRole("button", { name: /^decrease$/i }));
|
|
await selectReason(user, "Damage");
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(mocks.adjustStock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ direction: "OUT" }),
|
|
),
|
|
);
|
|
});
|
|
});
|
|
|
|
// ─── reason contract ──────────────────────────────────────────────────────────
|
|
|
|
describe("AdjustmentForm — reason exact-string contract", () => {
|
|
it.each([
|
|
["Damage", "Damage"],
|
|
["Expiry", "Expiry"],
|
|
["Return to shelf", "Return-to-shelf"],
|
|
["Cancelled DO", "Cancelled-DO"],
|
|
["Transfer gap", "Transfer-gap"],
|
|
["Count correction", "Count-correction"],
|
|
] as [string, string][])(
|
|
"selecting reason label '%s' submits the exact backend reason value '%s'",
|
|
async (label, expectedValue) => {
|
|
const user = userEvent.setup();
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJO20260906001" });
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await user.click(screen.getByRole("button", { name: /^decrease$/i }));
|
|
await selectReason(user, label);
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(mocks.adjustStock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ reason: expectedValue }),
|
|
),
|
|
);
|
|
|
|
mocks.adjustStock.mockReset();
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJO20260906001" });
|
|
},
|
|
);
|
|
});
|
|
|
|
// ─── success ─────────────────────────────────────────────────────────────────
|
|
|
|
describe("AdjustmentForm — success", () => {
|
|
it("shows the AJO documentNo on success and resets to one empty line", async () => {
|
|
const user = userEvent.setup();
|
|
// Backend returns the saved Inventory entity; Jackson key is "documentNo" (getDocumentNo()).
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJO20260906001" });
|
|
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await user.click(screen.getByRole("button", { name: /^decrease$/i }));
|
|
await selectReason(user, "Expiry");
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
|
|
// Success message shows the exact documentNo returned by the backend (not the "Submitted" fallback).
|
|
await screen.findByText(/AJO20260906001/);
|
|
|
|
// Form is reset: Submit is disabled again (empty line, no variation chosen)
|
|
expect(screen.getByRole("button", { name: /^submit$/i })).toBeDisabled();
|
|
});
|
|
|
|
it("shows the AJI documentNo on success for an Increase adjustment", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJI20260906001" });
|
|
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
// Increase is the default direction
|
|
await selectReason(user, "Count correction");
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
|
|
await screen.findByText(/AJI20260906001/);
|
|
});
|
|
|
|
it("calls adjustStock with correct warehouseId, direction, reason, skuVariationId, quantity", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJO20260906002" });
|
|
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await user.click(screen.getByRole("button", { name: /^decrease$/i }));
|
|
await selectReason(user, "Damage");
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
|
|
const qtyInput = screen.getByRole("spinbutton", { name: /quantity/i });
|
|
fireEvent.change(qtyInput, { target: { value: "5" } });
|
|
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
|
|
await waitFor(() =>
|
|
expect(mocks.adjustStock).toHaveBeenCalledWith({
|
|
warehouseId: "11",
|
|
direction: "OUT",
|
|
reason: "Damage",
|
|
lines: [
|
|
expect.objectContaining({
|
|
skuVariationId: "SV-001",
|
|
quantity: 5,
|
|
}),
|
|
],
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
// ─── 4xx rejection ───────────────────────────────────────────────────────────
|
|
|
|
describe("AdjustmentForm — 4xx rejection", () => {
|
|
it("renders the error message when adjustStock rejects AND keeps the entered lines", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.adjustStock.mockRejectedValue(new Error("Stock would go below zero"));
|
|
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await user.click(screen.getByRole("button", { name: /^decrease$/i }));
|
|
await selectReason(user, "Expiry");
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
|
|
// Error message is shown
|
|
await screen.findByText(/Stock would go below zero/i);
|
|
|
|
// Lines are retained: the product label "Baby Milk (P001)" is still visible
|
|
expect(screen.getByText("Baby Milk (P001)")).toBeInTheDocument();
|
|
|
|
// Variation combobox still present (line not reset to empty)
|
|
expect(screen.getByRole("combobox", { name: /^variation$/i })).toBeInTheDocument();
|
|
|
|
// Submit is still enabled (complete line remains)
|
|
expect(screen.getByRole("button", { name: /^submit$/i })).not.toBeDisabled();
|
|
|
|
// No success status message (role="status" is only present on submit success)
|
|
expect(screen.queryByRole("status")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("AdjustmentForm — product picker shows the product code", () => {
|
|
// Live dev has BA-001 and EVENT-BA-001 with the same display name; name-only options were
|
|
// indistinguishable, and cmdk (value = name) treated the pair as one item.
|
|
it("lists same-named products as two distinct options and picks the right one", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.listProducts.mockResolvedValue({
|
|
items: [
|
|
{ id: "1001", productCode: "BA-001", productName: "Pump Bra", productStatus: "Y" },
|
|
{ id: "1009", productCode: "EVENT-BA-001", productName: "Pump Bra", productStatus: "Y" },
|
|
],
|
|
total: 2,
|
|
});
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
await user.click(screen.getByRole("combobox", { name: /^product$/i }));
|
|
const options = await screen.findAllByRole("option", { name: /Pump Bra/ });
|
|
expect(options).toHaveLength(2);
|
|
expect(screen.getByRole("option", { name: /EVENT-BA-001/ })).toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("option", { name: /EVENT-BA-001/ }));
|
|
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1009"));
|
|
expect(screen.getByRole("combobox", { name: /^product$/i })).toHaveTextContent("Pump Bra (EVENT-BA-001)");
|
|
});
|
|
});
|
|
|
|
// ─── Inventory page panel props (merge-inventory-page) ────────────────────────
|
|
|
|
describe("AdjustmentForm — panel props", () => {
|
|
it("pre-selects an active defaultWarehouseId as the warehouse", async () => {
|
|
render(<AdjustmentForm defaultWarehouseId="10" />);
|
|
await waitForLoad();
|
|
expect(screen.getByRole("combobox", { name: /^warehouse$/i })).toHaveTextContent(/MYB-HQ/);
|
|
});
|
|
|
|
it("leaves the warehouse empty when defaultWarehouseId is not an active warehouse", async () => {
|
|
render(<AdjustmentForm defaultWarehouseId="999999" />);
|
|
await waitForLoad();
|
|
expect(screen.getByRole("combobox", { name: /^warehouse$/i })).not.toHaveTextContent(/MYB-/);
|
|
});
|
|
|
|
it("reports dirty after a product is picked, clean after a successful submit, and calls onSubmitted", async () => {
|
|
const user = userEvent.setup();
|
|
const onDirty = vi.fn();
|
|
const onSubmitted = vi.fn();
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJI20260924001" });
|
|
|
|
render(<AdjustmentForm defaultWarehouseId="11" onDirtyChange={onDirty} onSubmitted={onSubmitted} />);
|
|
await waitForLoad();
|
|
expect(onDirty).toHaveBeenLastCalledWith(false);
|
|
|
|
await selectReason(user, "Expiry");
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(onDirty).toHaveBeenLastCalledWith(true));
|
|
await pickVariation(user, "100g");
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
|
|
await screen.findByText(/AJI20260924001/);
|
|
expect(onSubmitted).toHaveBeenCalledTimes(1);
|
|
await waitFor(() => expect(onDirty).toHaveBeenLastCalledWith(false));
|
|
});
|
|
|
|
it("counts a picked reason as unsaved work even before a product is picked", async () => {
|
|
const user = userEvent.setup();
|
|
const onDirty = vi.fn();
|
|
render(<AdjustmentForm onDirtyChange={onDirty} />);
|
|
await waitForLoad();
|
|
|
|
await selectReason(user, "Expiry");
|
|
await waitFor(() => expect(onDirty).toHaveBeenLastCalledWith(true));
|
|
});
|
|
|
|
it("links to the History tab, keeping the panel open, when the user holds Movement History", async () => {
|
|
auth.hasModule.mockImplementation((m: string) => m === "BO_MOVEMENT_HISTORY");
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
expect(screen.getByRole("link", { name: "History" })).toHaveAttribute(
|
|
"href",
|
|
"/inventory?tab=history&action=adjust",
|
|
);
|
|
});
|
|
|
|
it("hides the History link from a user without Movement History", async () => {
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
|
|
expect(screen.queryByRole("link", { name: "History" })).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("AdjustmentForm — clearable quantity", () => {
|
|
async function readyLine(user: ReturnType<typeof userEvent.setup>) {
|
|
mocks.adjustStock.mockResolvedValue({ documentNo: "AJI20260906003" });
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await selectReason(user, "Damage");
|
|
await pickProduct(user, "Baby Milk");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("1001"));
|
|
await pickVariation(user, "100g");
|
|
return screen.getByRole("spinbutton", { name: /quantity/i }) as HTMLInputElement;
|
|
}
|
|
|
|
it("lets the quantity be cleared and retyped (no stray 1)", async () => {
|
|
const user = userEvent.setup();
|
|
const qty = await readyLine(user);
|
|
await user.clear(qty);
|
|
expect(qty.value).toBe("");
|
|
await user.type(qty, "5");
|
|
expect(qty.value).toBe("5");
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
await waitFor(() =>
|
|
expect(mocks.adjustStock).toHaveBeenCalledWith({
|
|
warehouseId: "11",
|
|
direction: "IN",
|
|
reason: "Damage",
|
|
lines: [expect.objectContaining({ skuVariationId: "SV-001", quantity: 5 })],
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("blocks Submit with a visible error when the quantity is cleared", async () => {
|
|
const user = userEvent.setup();
|
|
const qty = await readyLine(user);
|
|
await user.clear(qty);
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
expect(await screen.findByText("Enter a quantity of at least 1.")).toBeInTheDocument();
|
|
expect(qty).toHaveAttribute("aria-invalid", "true");
|
|
expect(mocks.adjustStock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("blocks Submit the same way when the quantity is 0", async () => {
|
|
const user = userEvent.setup();
|
|
const qty = await readyLine(user);
|
|
await user.clear(qty);
|
|
await user.type(qty, "0");
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
expect(await screen.findByText("Enter a quantity of at least 1.")).toBeInTheDocument();
|
|
expect(qty).toHaveAttribute("aria-invalid", "true");
|
|
expect(mocks.adjustStock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps 1.5 as typed (never rewritten to 15) and blocks Submit", async () => {
|
|
const user = userEvent.setup();
|
|
const qty = await readyLine(user);
|
|
await user.clear(qty);
|
|
await user.type(qty, "1.5");
|
|
expect(qty.value).toBe("1.5");
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
expect(await screen.findByText("Enter a quantity of at least 1.")).toBeInTheDocument();
|
|
expect(qty).toHaveAttribute("aria-invalid", "true");
|
|
expect(qty.value).toBe("1.5");
|
|
expect(mocks.adjustStock).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
|
|
// ─── BABYBOO-39: structured insufficient-stock rejection ─────────────────────
|
|
|
|
describe("AdjustmentForm — insufficient stock names the product", () => {
|
|
const MOMCOZY = { id: "2001", productCode: "", productName: "Momcozy Washing Block", productStatus: "Y" };
|
|
|
|
async function submitMomcozyLine(user: ReturnType<typeof userEvent.setup>) {
|
|
mocks.listProducts.mockResolvedValue({ items: [...PRODUCTS, MOMCOZY], total: 3 });
|
|
mocks.getProductVariations.mockResolvedValue([{ skuVariationId: "50", variationId: "V-50", label: "DEFAULT" }]);
|
|
render(<AdjustmentForm />);
|
|
await waitForLoad();
|
|
await selectWarehouse(user, /MYB-Master Store/i);
|
|
await user.click(screen.getByRole("button", { name: /^decrease$/i }));
|
|
await selectReason(user, "Expiry");
|
|
await pickProduct(user, "Momcozy Washing Block");
|
|
await waitFor(() => expect(mocks.getProductVariations).toHaveBeenCalledWith("2001"));
|
|
await pickVariation(user, "DEFAULT");
|
|
await user.click(screen.getByRole("button", { name: /^submit$/i }));
|
|
}
|
|
|
|
function insufficient(skuVariationId: number) {
|
|
const raw = `Insufficient stock for sku_variation ${skuVariationId}: available 0, requested 10, shortfall 10`;
|
|
return new ApiError(raw, 400, {
|
|
error: raw,
|
|
code: "INSUFFICIENT_STOCK",
|
|
details: { skuVariationId, available: 0, requested: 10 },
|
|
});
|
|
}
|
|
|
|
it("shows the translated line message, not the raw backend text, and keeps the lines", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.adjustStock.mockRejectedValue(insufficient(50));
|
|
|
|
await submitMomcozyLine(user);
|
|
|
|
await screen.findByText("Not enough stock for Momcozy Washing Block — DEFAULT (0 available, 10 requested).");
|
|
expect(screen.queryByText(/sku_variation/)).not.toBeInTheDocument();
|
|
expect(screen.getByText("Momcozy Washing Block")).toBeInTheDocument();
|
|
expect(screen.getByRole("combobox", { name: /^variation$/i })).toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the generic message when no submitted line matches", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.adjustStock.mockRejectedValue(insufficient(999));
|
|
|
|
await submitMomcozyLine(user);
|
|
|
|
await screen.findByText("Not enough stock for one of the lines.");
|
|
expect(screen.queryByText(/sku_variation/)).not.toBeInTheDocument();
|
|
});
|
|
});
|