import { useState } from "react"; import { fireEvent, screen } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { UpliftAttachmentPicker } from "@/app/(protected)/workorders/_components/uplifts/uplift-attachment-picker"; import { renderWithProviders } from "@/test/test-utils"; function PickerHarness({ onChange }: { onChange: (files: File[]) => void }) { const [files, setFiles] = useState([]); return ( { setFiles(next); onChange(next); }} /> ); } function selectFiles(files: File[]) { const input = document.querySelector('input[type="file"]'); fireEvent.change(input as HTMLInputElement, { target: { files } }); } describe("UpliftAttachmentPicker", () => { it("keeps every allowed file from one multi-select", () => { const onChange = vi.fn(); renderWithProviders(); const quote = new File(["%PDF"], "quote.pdf", { type: "application/pdf" }); const notes = new File(["text"], "notes.txt", { type: "text/plain" }); const photo = new File(["jpeg"], "photo.jpg", { type: "image/jpeg" }); selectFiles([quote, notes, photo]); expect(screen.getByText("quote.pdf")).toBeInTheDocument(); expect(screen.getByText("photo.jpg")).toBeInTheDocument(); expect(screen.queryByText("notes.txt")).not.toBeInTheDocument(); expect(onChange).toHaveBeenCalledWith([quote, photo]); expect(document.querySelector('input[type="file"]')?.hasAttribute("multiple")).toBe(true); }); it("appends a later selection instead of replacing the staged files", () => { const onChange = vi.fn(); renderWithProviders(); const quote = new File(["%PDF"], "quote.pdf", { type: "application/pdf" }); const photo = new File(["jpeg"], "photo.jpg", { type: "image/jpeg" }); selectFiles([quote]); selectFiles([photo]); expect(screen.getByText("quote.pdf")).toBeInTheDocument(); expect(screen.getByText("photo.jpg")).toBeInTheDocument(); expect(onChange).toHaveBeenLastCalledWith([quote, photo]); }); });