import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { renderWithProviders as render, screen, fireEvent, cleanup, } from "../utils/test-utils"; import "@testing-library/jest-dom/vitest"; import { CommunityUploadScreen } from "../../app/(app)/create/screens/upload/CommunityUploadScreen"; import { clearPendingCommunityAvatarFile, readPendingCommunityAvatarFile, storePendingCommunityAvatarFile, } from "../../lib/create/pendingCommunityAvatarUpload"; import { CreateFlowUploadValidationError, uploadCreateFlowFile, } from "../../lib/create/uploadToServer"; import messages from "../../messages/en/index"; vi.mock("../../lib/create/pendingCommunityAvatarUpload", () => ({ readPendingCommunityAvatarFile: vi.fn().mockResolvedValue(null), storePendingCommunityAvatarFile: vi.fn().mockResolvedValue(undefined), clearPendingCommunityAvatarFile: vi.fn().mockResolvedValue(undefined), })); vi.mock("../../lib/create/uploadToServer", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, uploadCreateFlowFile: vi.fn(), }; }); const pngBytes = Uint8Array.from( Buffer.from( "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", "base64", ), ); const copy = messages.create.community.communityUpload; const uploadErrors = messages.create.upload.errors; function fileInput(): HTMLInputElement { const input = document.querySelector('input[type="file"]'); expect(input).toBeInstanceOf(HTMLInputElement); return input as HTMLInputElement; } describe("CommunityUploadScreen", () => { beforeEach(() => { vi.mocked(readPendingCommunityAvatarFile).mockResolvedValue(null); vi.mocked(storePendingCommunityAvatarFile).mockResolvedValue(undefined); vi.mocked(clearPendingCommunityAvatarFile).mockResolvedValue(undefined); vi.mocked(uploadCreateFlowFile).mockReset(); Object.defineProperty(URL, "createObjectURL", { value: vi.fn(() => "blob:pending-avatar"), writable: true, configurable: true, }); Object.defineProperty(URL, "revokeObjectURL", { value: vi.fn(), writable: true, configurable: true, }); }); afterEach(() => { cleanup(); Reflect.deleteProperty(URL, "createObjectURL"); Reflect.deleteProperty(URL, "revokeObjectURL"); }); it("renders HeaderLockup", () => { render(); expect( screen.getByRole("heading", { name: "Add a logo", }), ).toBeInTheDocument(); }); it("renders Upload control and helper copy", () => { render(); expect(screen.getByRole("button", { name: "Upload" })).toBeInTheDocument(); expect( screen.getByText( /Only people who can read your Rule can see this image/i, ), ).toBeInTheDocument(); }); it("restores a pending IndexedDB photo into the uploaded preview", async () => { const pending = new File([pngBytes], "avatar.png", { type: "image/png" }); vi.mocked(readPendingCommunityAvatarFile).mockResolvedValue(pending); render(); const preview = await screen.findByRole("img", { name: copy.previewAlt }); expect(preview).toHaveAttribute("src", "blob:pending-avatar"); expect( screen.getByRole("button", { name: copy.replaceButton }), ).toBeInTheDocument(); expect(screen.getByText("avatar.png")).toBeInTheDocument(); }); it("uploads a guest logo without signing in", async () => { vi.mocked(uploadCreateFlowFile).mockResolvedValue({ url: "/api/uploads/guest-logo", id: "11111111-1111-4111-8111-111111111111", mimeType: "image/png", byteLength: pngBytes.byteLength, }); render(); await screen.findByRole("heading", { name: copy.title }); fireEvent.change(fileInput(), { target: { files: [new File([pngBytes], "logo.png", { type: "image/png" })], }, }); expect(await screen.findByText("logo.png")).toBeInTheDocument(); expect(uploadCreateFlowFile).toHaveBeenCalledWith( expect.any(File), "communityAvatar", ); expect(storePendingCommunityAvatarFile).not.toHaveBeenCalled(); }); it("rejects an empty file with a clear error and does not stage it", async () => { vi.mocked(uploadCreateFlowFile).mockRejectedValue( new CreateFlowUploadValidationError("empty"), ); render(); await screen.findByRole("heading", { name: copy.title }); fireEvent.change(fileInput(), { target: { files: [new File([], "empty.png", { type: "image/png" })] }, }); expect(await screen.findByRole("alert")).toHaveTextContent( uploadErrors.empty, ); expect(storePendingCommunityAvatarFile).not.toHaveBeenCalled(); }); it("rejects a text file renamed as PNG", async () => { vi.mocked(uploadCreateFlowFile).mockRejectedValue( new CreateFlowUploadValidationError("invalidType"), ); render(); await screen.findByRole("heading", { name: copy.title }); fireEvent.change(fileInput(), { target: { files: [new File(["hello"], "photo.png", { type: "image/png" })], }, }); expect(await screen.findByRole("alert")).toHaveTextContent( uploadErrors.invalidType, ); expect(storePendingCommunityAvatarFile).not.toHaveBeenCalled(); }); it("rejects SVG uploads", async () => { vi.mocked(uploadCreateFlowFile).mockRejectedValue( new CreateFlowUploadValidationError("svg"), ); render(); await screen.findByRole("heading", { name: copy.title }); fireEvent.change(fileInput(), { target: { files: [ new File( [''], "photo.png", { type: "image/png" }, ), ], }, }); expect(await screen.findByRole("alert")).toHaveTextContent(uploadErrors.svg); expect(storePendingCommunityAvatarFile).not.toHaveBeenCalled(); }); });