import path from "path"; import { test, expect, seed, API } from "./fixtures"; // Excel exports (ExcelTS since 1.51.6): a styled report and the attendance register come back as // real workbooks that open and hold what was asked for. // eslint-disable-next-line @typescript-eslint/no-require-imports const { Workbook } = require(path.join(__dirname, "..", "backend", "node_modules", "@cj-tech-master", "excelts")); const XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; async function open(body: Buffer) { const wb = new Workbook(); await wb.xlsx.load(body); return wb; } test.describe("Excel exports", () => { test("a report with stats, a chart and a table", async ({ request }) => { const res = await request.post(`${API}/api/v2/reports/excel`, { headers: { Authorization: `Bearer ${seed().users.supervisor.token}` }, data: { title: "E2E Payments", kind: "table", subtitle: "1–31 October", stats: [{ label: "Total", value: "R 1 200.00" }], chart: { title: "By method", data: [{ label: "Card", value: 900 }, { label: "Cash", value: 300 }] }, table: { columns: ["Name", "Amount"], rows: [["Thandi", "R 600.00"], ["Sipho", "R 600.00"]] }, }, }); expect(res.status()).toBe(200); expect(res.headers()["content-type"]).toContain(XLSX); const ws = (await open(await res.body())).worksheets[0]; const values: string[] = []; ws.eachRow((row: { values: unknown[] }) => values.push(...row.values.filter(Boolean).map(String))); expect(values).toEqual(expect.arrayContaining(["E2E Payments", "Total", "Card", "Thandi", "Sipho"])); }); test("the attendance register", async ({ request }) => { const res = await request.get(`${API}/api/v2/events/${seed().events.course}/attendance/export?format=xlsx`, { headers: { Authorization: `Bearer ${seed().users.supervisor.token}` }, }); expect(res.status()).toBe(200); expect(res.headers()["content-type"]).toContain(XLSX); const ws = (await open(await res.body())).getWorksheet("Attendance"); expect(ws.getRow(1).getCell(1).value).toBe("Name"); expect(ws.rowCount).toBeGreaterThan(1); }); });