|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { generateCsv, type CsvRow } from "@/lib/csvExport"; |
| 3 | + |
| 4 | +describe("generateCsv", () => { |
| 5 | + let clickSpy: ReturnType<typeof vi.fn>; |
| 6 | + let appendChildSpy: ReturnType<typeof vi.fn>; |
| 7 | + let removeChildSpy: ReturnType<typeof vi.fn>; |
| 8 | + let revokeObjectURLSpy: ReturnType<typeof vi.fn>; |
| 9 | + let createObjectURLSpy: ReturnType<typeof vi.fn>; |
| 10 | + let linkAttrs: Record<string, string>; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + clickSpy = vi.fn(); |
| 14 | + appendChildSpy = vi.fn(); |
| 15 | + removeChildSpy = vi.fn(); |
| 16 | + revokeObjectURLSpy = vi.fn(); |
| 17 | + createObjectURLSpy = vi.fn(() => "blob:mock-url"); |
| 18 | + linkAttrs = {}; |
| 19 | + |
| 20 | + const mockLink = { |
| 21 | + click: clickSpy, |
| 22 | + setAttribute: (name: string, value: string) => { |
| 23 | + linkAttrs[name] = value; |
| 24 | + }, |
| 25 | + style: { display: "" }, |
| 26 | + }; |
| 27 | + |
| 28 | + vi.spyOn(document, "createElement").mockReturnValue(mockLink as unknown as HTMLElement); |
| 29 | + vi.spyOn(document.body, "appendChild").mockImplementation(appendChildSpy); |
| 30 | + vi.spyOn(document.body, "removeChild").mockImplementation(removeChildSpy); |
| 31 | + vi.spyOn(URL, "createObjectURL").mockImplementation(createObjectURLSpy); |
| 32 | + vi.spyOn(URL, "revokeObjectURL").mockImplementation(revokeObjectURLSpy); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + vi.restoreAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("does nothing when rows are empty", () => { |
| 40 | + generateCsv([], "test"); |
| 41 | + expect(appendChildSpy).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("creates and clicks a download link", () => { |
| 45 | + const rows: CsvRow[] = [ |
| 46 | + { id: "1", status: "Released", amount: 100 }, |
| 47 | + { id: "2", status: "Pending", amount: 50 }, |
| 48 | + ]; |
| 49 | + generateCsv(rows, "my-data"); |
| 50 | + |
| 51 | + expect(createObjectURLSpy).toHaveBeenCalled(); |
| 52 | + expect(appendChildSpy).toHaveBeenCalled(); |
| 53 | + expect(clickSpy).toHaveBeenCalled(); |
| 54 | + expect(removeChildSpy).toHaveBeenCalled(); |
| 55 | + expect(revokeObjectURLSpy).toHaveBeenCalled(); |
| 56 | + expect(linkAttrs["download"]).toBe("my-data.csv"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("generates a Blob with CSV content", () => { |
| 60 | + const rows: CsvRow[] = [{ id: "1", name: "Test" }]; |
| 61 | + generateCsv(rows, "test"); |
| 62 | + |
| 63 | + const blobArg = createObjectURLSpy.mock.calls[0][0] as Blob; |
| 64 | + expect(blobArg).toBeInstanceOf(Blob); |
| 65 | + }); |
| 66 | + |
| 67 | + it("handles values with commas by quoting them", () => { |
| 68 | + const rows: CsvRow[] = [{ id: "1", note: "hello, world" }]; |
| 69 | + generateCsv(rows, "test"); |
| 70 | + expect(createObjectURLSpy).toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("handles values with double quotes by escaping them", () => { |
| 74 | + const rows: CsvRow[] = [{ id: "1", note: 'say "hi"' }]; |
| 75 | + generateCsv(rows, "test"); |
| 76 | + expect(createObjectURLSpy).toHaveBeenCalled(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments