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
79 lines
3.4 KiB
TypeScript
79 lines
3.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { ImageAttachments, useAttachments } from "./ImageAttachments";
|
|
import { uploadAdminMessageImage } from "@/app/lib/services/adminMessages";
|
|
|
|
vi.mock("@/app/lib/services/adminMessages", async (orig) => ({
|
|
...(await orig<typeof import("@/app/lib/services/adminMessages")>()),
|
|
uploadAdminMessageImage: vi.fn(),
|
|
}));
|
|
|
|
function Harness() {
|
|
const a = useAttachments();
|
|
return (
|
|
<>
|
|
<ImageAttachments attachments={a} />
|
|
<output data-testid="refs">{a.refs.join(",")}</output>
|
|
<output data-testid="busy">{String(a.busy)}</output>
|
|
</>
|
|
);
|
|
}
|
|
const pick = (...files: File[]) =>
|
|
fireEvent.change(screen.getByLabelText("Choose images"), { target: { files } });
|
|
const png = (name: string) => new File(["x"], name, { type: "image/png" });
|
|
|
|
beforeEach(() => {
|
|
vi.mocked(uploadAdminMessageImage).mockReset().mockImplementation(async (f) => ({ ref: `messages/admin/${f.name}` }));
|
|
});
|
|
|
|
describe("ImageAttachments", () => {
|
|
it("uploads a valid image and exposes its ref", async () => {
|
|
render(<Harness />);
|
|
pick(png("a.png"));
|
|
await waitFor(() => expect(screen.getByTestId("refs")).toHaveTextContent("messages/admin/a.png"));
|
|
expect(screen.getByTestId("busy")).toHaveTextContent("false");
|
|
});
|
|
|
|
it("refuses a wrong type without uploading and lets the user remove it", async () => {
|
|
render(<Harness />);
|
|
pick(new File(["x"], "a.gif", { type: "image/gif" }));
|
|
expect(await screen.findByText("Only JPEG or PNG images are allowed.")).toBeInTheDocument();
|
|
expect(uploadAdminMessageImage).not.toHaveBeenCalled();
|
|
await userEvent.click(screen.getByRole("button", { name: "Remove a.gif" }));
|
|
expect(screen.queryByText("a.gif")).toBeNull();
|
|
});
|
|
|
|
it("refuses an image that is still over 5 MB after shrinking, without uploading", async () => {
|
|
render(<Harness />);
|
|
// jsdom cannot decode images, so shrinkImage returns the file unchanged.
|
|
pick(new File([new Uint8Array(5 * 1024 * 1024 + 1)], "big.png", { type: "image/png" }));
|
|
expect(await screen.findByText("Each image must be 5 MB or smaller.")).toBeInTheDocument();
|
|
expect(uploadAdminMessageImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("accepts at most 4 images", async () => {
|
|
render(<Harness />);
|
|
pick(png("1.png"), png("2.png"), png("3.png"), png("4.png"), png("5.png"));
|
|
await waitFor(() => expect(screen.getByTestId("refs").textContent?.split(",")).toHaveLength(4));
|
|
expect(uploadAdminMessageImage).toHaveBeenCalledTimes(4);
|
|
expect(screen.getByRole("button", { name: /Attach images/ })).toBeDisabled();
|
|
});
|
|
|
|
it("shows an upload failure and keeps it out of refs", async () => {
|
|
vi.mocked(uploadAdminMessageImage).mockRejectedValue(new Error("Too big"));
|
|
render(<Harness />);
|
|
pick(png("a.png"));
|
|
expect(await screen.findByText("Too big")).toBeInTheDocument();
|
|
expect(screen.getByTestId("refs")).toBeEmptyDOMElement();
|
|
});
|
|
|
|
it("shows friendly copy, not the raw reason, for a known upload failure", async () => {
|
|
vi.mocked(uploadAdminMessageImage).mockRejectedValue(new Error("MESSAGE_UPLOAD_FAILED"));
|
|
render(<Harness />);
|
|
pick(png("a.png"));
|
|
expect(await screen.findByText("The image upload failed. Please try again.")).toBeInTheDocument();
|
|
expect(screen.queryByText("MESSAGE_UPLOAD_FAILED")).toBeNull();
|
|
});
|
|
});
|