Files
community-rule/tests/components/UploadPage.test.tsx
T
adilallo 0535f6f167 Let anyone finish a Rule without an account or email.
Continue on final review and Finish without inviting no longer open a login wall. The header stays Exit until Save your progress is done, and Send invitations is the only invite-path login.
2026-11-23 14:48:01 -07:00

193 lines
6.0 KiB
TypeScript

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<typeof import("../../lib/create/uploadToServer")>();
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(<CommunityUploadScreen />);
expect(
screen.getByRole("heading", {
name: "Add a logo",
}),
).toBeInTheDocument();
});
it("renders Upload control and helper copy", () => {
render(<CommunityUploadScreen />);
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(<CommunityUploadScreen />);
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(<CommunityUploadScreen />);
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(<CommunityUploadScreen />);
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(<CommunityUploadScreen />);
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(<CommunityUploadScreen />);
await screen.findByRole("heading", { name: copy.title });
fireEvent.change(fileInput(), {
target: {
files: [
new File(
['<svg xmlns="http://www.w3.org/2000/svg"></svg>'],
"photo.png",
{ type: "image/png" },
),
],
},
});
expect(await screen.findByRole("alert")).toHaveTextContent(uploadErrors.svg);
expect(storePendingCommunityAvatarFile).not.toHaveBeenCalled();
});
});