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
188 lines
7.3 KiB
TypeScript
188 lines
7.3 KiB
TypeScript
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
|
|
vi.mock("@/app/lib/services/adminLocations", () => ({
|
|
uploadInvoiceHeaderImage: vi.fn(),
|
|
}));
|
|
|
|
import { InvoicingTab } from "./InvoicingTab";
|
|
import { uploadInvoiceHeaderImage } from "@/app/lib/services/adminLocations";
|
|
import type { AdminLocationNode } from "@/app/lib/types";
|
|
|
|
const node: AdminLocationNode = {
|
|
id: 35, code: "MYB", name: "Kuala Lumpur", type: "B", status: "Y", parent: { id: 21 },
|
|
invoicePrefix: "IN", deliveryOrderPrefix: "",
|
|
};
|
|
|
|
// jsdom has no FileReader.readAsDataURL; fake one that resolves synchronously
|
|
// (mirrors app/bulletins/page.test.tsx).
|
|
function stubFileReader(dataUrl: string) {
|
|
vi.stubGlobal(
|
|
"FileReader",
|
|
class {
|
|
result: string | ArrayBuffer | null = null;
|
|
onload: (() => void) | null = null;
|
|
onerror: (() => void) | null = null;
|
|
readAsDataURL() {
|
|
this.result = dataUrl;
|
|
this.onload?.();
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
function stubObjectUrl() {
|
|
URL.createObjectURL = vi.fn(() => "blob:mock-preview");
|
|
URL.revokeObjectURL = vi.fn();
|
|
}
|
|
|
|
function pickHeaderImage(name: string, type: string, size: number) {
|
|
const file = new File([new ArrayBuffer(size)], name, { type });
|
|
fireEvent.change(screen.getByLabelText(/invoice header image/i), { target: { files: [file] } });
|
|
}
|
|
|
|
describe("InvoicingTab", () => {
|
|
beforeEach(() => {
|
|
vi.mocked(uploadInvoiceHeaderImage).mockReset();
|
|
});
|
|
|
|
afterEach(() => {
|
|
// vi.stubGlobal'ed FileReader must not leak into tests that pick no file.
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("shows the current invoice and delivery-order prefixes", () => {
|
|
render(<InvoicingTab node={node} onSave={vi.fn()} />);
|
|
expect(screen.getByLabelText("Invoice prefix")).toHaveValue("IN");
|
|
expect(screen.getByLabelText("Delivery order prefix")).toHaveValue("");
|
|
});
|
|
|
|
it("saves the changed fields and confirms success", async () => {
|
|
const onSave = vi.fn().mockResolvedValue(undefined);
|
|
render(<InvoicingTab node={node} onSave={onSave} />);
|
|
fireEvent.change(screen.getByLabelText("Delivery order prefix"), { target: { value: "DO" } });
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() =>
|
|
expect(onSave).toHaveBeenCalledWith({ invoicePrefix: "IN", deliveryOrderPrefix: "DO" }),
|
|
);
|
|
expect(await screen.findByText("Saved.")).toBeInTheDocument();
|
|
});
|
|
|
|
it("shows an error message when onSave rejects", async () => {
|
|
const onSave = vi.fn().mockRejectedValue(new Error("Prefix already in use"));
|
|
render(<InvoicingTab node={node} onSave={onSave} />);
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
expect(await screen.findByText("Prefix already in use")).toBeInTheDocument();
|
|
});
|
|
|
|
// --- invoice-header image upload (openspec/changes/add-location-invoice-header-upload) ---
|
|
|
|
it("renders the node's documentHeaderPath as the current preview image", () => {
|
|
const withHeader = { ...node, documentHeaderPath: "https://cdn.example/header.png" };
|
|
render(<InvoicingTab node={withHeader} onSave={vi.fn()} />);
|
|
expect(screen.getByAltText(/preview/i)).toHaveAttribute("src", "https://cdn.example/header.png");
|
|
});
|
|
|
|
it("rejects a non-image pick with an inline error and no upload", async () => {
|
|
render(<InvoicingTab node={node} onSave={vi.fn()} />);
|
|
pickHeaderImage("doc.txt", "text/plain", 10);
|
|
|
|
expect(await screen.findByText(/only png, jpeg, webp or gif/i)).toBeInTheDocument();
|
|
expect(uploadInvoiceHeaderImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects an oversize pick with an inline error and no upload", async () => {
|
|
render(<InvoicingTab node={node} onSave={vi.fn()} />);
|
|
pickHeaderImage("big.png", "image/png", 2 * 1024 * 1024 + 1);
|
|
|
|
expect(await screen.findByText(/2 mb or smaller/i)).toBeInTheDocument();
|
|
expect(uploadInvoiceHeaderImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("picking a valid file shows a local blob preview and does not upload", async () => {
|
|
stubObjectUrl();
|
|
render(<InvoicingTab node={node} onSave={vi.fn()} />);
|
|
pickHeaderImage("photo.png", "image/png", 100);
|
|
|
|
expect(await screen.findByAltText(/preview/i)).toHaveAttribute("src", "blob:mock-preview");
|
|
expect(uploadInvoiceHeaderImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("submitting with a staged file uploads it once and saves documentHeaderPath with the returned url", async () => {
|
|
const onSave = vi.fn().mockResolvedValue(undefined);
|
|
vi.mocked(uploadInvoiceHeaderImage).mockResolvedValue({ url: "https://cdn/pub/bucket/photo.png" });
|
|
stubObjectUrl();
|
|
stubFileReader("data:image/png;base64,QUJD");
|
|
|
|
render(<InvoicingTab node={node} onSave={onSave} />);
|
|
pickHeaderImage("photo.png", "image/png", 100);
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(uploadInvoiceHeaderImage).toHaveBeenCalledWith({
|
|
fileName: "photo.png",
|
|
fileType: "image/png",
|
|
base: "QUJD",
|
|
}),
|
|
);
|
|
expect(uploadInvoiceHeaderImage).toHaveBeenCalledTimes(1);
|
|
await waitFor(() =>
|
|
expect(onSave).toHaveBeenCalledWith({
|
|
invoicePrefix: "IN",
|
|
deliveryOrderPrefix: "",
|
|
documentHeaderPath: "https://cdn/pub/bucket/photo.png",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("removing the header image then submitting saves documentHeaderPath as empty", async () => {
|
|
const withHeader = { ...node, documentHeaderPath: "https://cdn.example/header.png" };
|
|
const onSave = vi.fn().mockResolvedValue(undefined);
|
|
render(<InvoicingTab node={withHeader} onSave={onSave} />);
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: /remove image/i }));
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(onSave).toHaveBeenCalledWith({
|
|
invoicePrefix: "IN",
|
|
deliveryOrderPrefix: "",
|
|
documentHeaderPath: "",
|
|
}),
|
|
);
|
|
expect(uploadInvoiceHeaderImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("submitting untouched omits documentHeaderPath so the stored value is preserved", async () => {
|
|
const withHeader = { ...node, documentHeaderPath: "https://cdn.example/header.png" };
|
|
const onSave = vi.fn().mockResolvedValue(undefined);
|
|
render(<InvoicingTab node={withHeader} onSave={onSave} />);
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(onSave).toHaveBeenCalledWith({ invoicePrefix: "IN", deliveryOrderPrefix: "" }),
|
|
);
|
|
const [savedFields] = onSave.mock.calls[0];
|
|
expect(savedFields).not.toHaveProperty("documentHeaderPath");
|
|
expect(uploadInvoiceHeaderImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("an upload failure on submit blocks the save and shows a distinct upload error", async () => {
|
|
const onSave = vi.fn().mockResolvedValue(undefined);
|
|
vi.mocked(uploadInvoiceHeaderImage).mockRejectedValue(new ApiError("Upstream said no", 400));
|
|
stubObjectUrl();
|
|
stubFileReader("data:image/png;base64,QUJD");
|
|
|
|
render(<InvoicingTab node={node} onSave={onSave} />);
|
|
pickHeaderImage("photo.png", "image/png", 100);
|
|
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(screen.getByText(/image upload failed: upstream said no/i)).toBeInTheDocument(),
|
|
);
|
|
expect(onSave).not.toHaveBeenCalled();
|
|
});
|
|
});
|