|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + dbSelect: vi.fn(), |
| 5 | +})); |
| 6 | + |
| 7 | +vi.mock("~/server/db", () => ({ |
| 8 | + db: { select: mocks.dbSelect }, |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("~/server/db/schema", () => ({ |
| 12 | + referents: { |
| 13 | + region: "region", |
| 14 | + county: "county", |
| 15 | + name: "name", |
| 16 | + type: "type", |
| 17 | + value: "value", |
| 18 | + principal: "principal", |
| 19 | + substituteName: "substituteName", |
| 20 | + substituteEmail: "substituteEmail", |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("drizzle-orm", () => ({ |
| 25 | + asc: (col: unknown) => ({ asc: col }), |
| 26 | +})); |
| 27 | + |
| 28 | +function setRows(rows: unknown[]) { |
| 29 | + mocks.dbSelect.mockReturnValue({ |
| 30 | + from: () => ({ |
| 31 | + orderBy: () => Promise.resolve(rows), |
| 32 | + }), |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +describe("/api/public/referents-egalite-professionnelle", () => { |
| 37 | + beforeEach(() => { |
| 38 | + vi.resetAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("returns JSON by default", async () => { |
| 42 | + setRows([ |
| 43 | + { |
| 44 | + region: "11", |
| 45 | + county: "75", |
| 46 | + name: "Jean", |
| 47 | + type: "email", |
| 48 | + value: "j@gouv.fr", |
| 49 | + principal: true, |
| 50 | + substituteName: null, |
| 51 | + substituteEmail: null, |
| 52 | + }, |
| 53 | + ]); |
| 54 | + |
| 55 | + const { GET } = await import("../route"); |
| 56 | + const response = await GET( |
| 57 | + new Request( |
| 58 | + "http://localhost/api/public/referents-egalite-professionnelle", |
| 59 | + ), |
| 60 | + ); |
| 61 | + |
| 62 | + expect(response.headers.get("Content-Type")).toMatch(/application\/json/); |
| 63 | + const body = await response.json(); |
| 64 | + expect(body).toHaveLength(1); |
| 65 | + expect(body[0]).toMatchObject({ name: "Jean", region: "11" }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("returns CSV with headers and region/county labels when format=csv", async () => { |
| 69 | + setRows([ |
| 70 | + { |
| 71 | + region: "11", |
| 72 | + county: "75", |
| 73 | + name: "Jean", |
| 74 | + type: "email", |
| 75 | + value: 'j"@gouv.fr', |
| 76 | + principal: true, |
| 77 | + substituteName: "Marie", |
| 78 | + substituteEmail: "m@gouv.fr", |
| 79 | + }, |
| 80 | + { |
| 81 | + region: "11", |
| 82 | + county: null, |
| 83 | + name: "Sans département", |
| 84 | + type: "url", |
| 85 | + value: "https://gouv.fr", |
| 86 | + principal: false, |
| 87 | + substituteName: null, |
| 88 | + substituteEmail: null, |
| 89 | + }, |
| 90 | + ]); |
| 91 | + |
| 92 | + const { GET } = await import("../route"); |
| 93 | + const response = await GET( |
| 94 | + new Request( |
| 95 | + "http://localhost/api/public/referents-egalite-professionnelle?format=csv", |
| 96 | + ), |
| 97 | + ); |
| 98 | + |
| 99 | + expect(response.headers.get("Content-Type")).toContain("text/csv"); |
| 100 | + expect(response.headers.get("Content-Disposition")).toContain( |
| 101 | + "referents_egalite_professionnelle.csv", |
| 102 | + ); |
| 103 | + |
| 104 | + const csv = await response.text(); |
| 105 | + const lines = csv.split("\n"); |
| 106 | + expect(lines[0]).toBe( |
| 107 | + "Région;Département;Nom;Type;Valeur;Principal;Nom suppléant;Email suppléant", |
| 108 | + ); |
| 109 | + expect(lines[1]).toContain('"Jean"'); |
| 110 | + expect(lines[1]).toContain('"j""@gouv.fr"'); |
| 111 | + expect(lines[1]).toContain('"Oui"'); |
| 112 | + expect(lines[2]).toContain('"Sans département"'); |
| 113 | + expect(lines[2]).toContain('"Non"'); |
| 114 | + expect(lines[2]).toContain('""'); |
| 115 | + }); |
| 116 | +}); |
0 commit comments