|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import { expect, test, type Page } from "@playwright/test"; |
| 3 | + |
| 4 | +async function openPreview(page: Page) { |
| 5 | + await page.goto("/docs/grid/export", { waitUntil: "domcontentloaded" }); |
| 6 | + const example = page.locator("figure").filter({ |
| 7 | + has: page.locator('a[href="/examples/export-preview.md"]'), |
| 8 | + }); |
| 9 | + await example.scrollIntoViewIfNeeded(); |
| 10 | + await expect( |
| 11 | + example.locator("[data-pretable-hydrated]").first(), |
| 12 | + ).toHaveAttribute("data-pretable-hydrated", "true", { timeout: 20_000 }); |
| 13 | + return example; |
| 14 | +} |
| 15 | + |
| 16 | +test("CSV preview quotes formatted data and downloads the exact captured file", async ({ |
| 17 | + page, |
| 18 | +}) => { |
| 19 | + const example = await openPreview(page); |
| 20 | + const downloadButton = example.getByRole("button", { name: "Download CSV" }); |
| 21 | + await expect(downloadButton).toBeDisabled(); |
| 22 | + await example.getByRole("button", { name: "Preview CSV" }).click(); |
| 23 | + const preview = example.getByLabel("CSV preview", { exact: true }); |
| 24 | + const text = await preview.textContent(); |
| 25 | + expect(text?.split(/\r?\n/).filter(Boolean)).toEqual([ |
| 26 | + "Order,Customer,Total", |
| 27 | + 'ORD-101,"Acme, Inc.","$1,234.50"', |
| 28 | + 'ORD-102,"Beacon ""Labs""",$99.00', |
| 29 | + "ORD-103,Cedar,$0.00", |
| 30 | + ]); |
| 31 | + const pending = page.waitForEvent("download"); |
| 32 | + await downloadButton.click(); |
| 33 | + const download = await pending; |
| 34 | + expect(download.suggestedFilename()).toMatch( |
| 35 | + /^orders-preview-\d{8}T\d{6}Z\.csv$/, |
| 36 | + ); |
| 37 | + const path = await download.path(); |
| 38 | + expect(path).not.toBeNull(); |
| 39 | + expect(await readFile(path!, "utf8")).toBe(text); |
| 40 | +}); |
| 41 | + |
| 42 | +test("CSV settings invalidate old previews and control columns, headers, and delimiters", async ({ |
| 43 | + page, |
| 44 | +}) => { |
| 45 | + const example = await openPreview(page); |
| 46 | + const generate = example.getByRole("button", { name: "Preview CSV" }); |
| 47 | + const download = example.getByRole("button", { name: "Download CSV" }); |
| 48 | + const preview = example.getByLabel("CSV preview", { exact: true }); |
| 49 | + await generate.click(); |
| 50 | + await example |
| 51 | + .getByRole("checkbox", { name: "Customer", exact: true }) |
| 52 | + .uncheck(); |
| 53 | + await expect(preview).toHaveCount(0); |
| 54 | + await expect(download).toBeDisabled(); |
| 55 | + await example.getByRole("checkbox", { name: "Include headers" }).uncheck(); |
| 56 | + for (const delimiter of [";", "\t"]) { |
| 57 | + await example |
| 58 | + .getByRole("combobox", { name: "Delimiter", exact: true }) |
| 59 | + .selectOption(delimiter); |
| 60 | + await expect(download).toBeDisabled(); |
| 61 | + await generate.click(); |
| 62 | + expect( |
| 63 | + (await preview.textContent())?.split(/\r?\n/).filter(Boolean), |
| 64 | + ).toEqual([ |
| 65 | + `ORD-101${delimiter}$1,234.50`, |
| 66 | + `ORD-102${delimiter}$99.00`, |
| 67 | + `ORD-103${delimiter}$0.00`, |
| 68 | + ]); |
| 69 | + } |
| 70 | + await example.getByRole("checkbox", { name: "Order", exact: true }).uncheck(); |
| 71 | + await example.getByRole("checkbox", { name: "Total", exact: true }).uncheck(); |
| 72 | + await expect(generate).toBeDisabled(); |
| 73 | + await expect(download).toBeDisabled(); |
| 74 | + await expect(example).toContainText("Select at least one column to preview."); |
| 75 | + await example |
| 76 | + .getByRole("checkbox", { name: "Customer", exact: true }) |
| 77 | + .check(); |
| 78 | + await generate.click(); |
| 79 | + await expect(preview).toContainText('"Beacon ""Labs"""'); |
| 80 | +}); |
| 81 | + |
| 82 | +test("CSV example source is complete and preview stays within a mobile page", async ({ |
| 83 | + page, |
| 84 | + request, |
| 85 | +}) => { |
| 86 | + await page.setViewportSize({ width: 390, height: 844 }); |
| 87 | + const example = await openPreview(page); |
| 88 | + await example.getByRole("button", { name: "Preview CSV" }).click(); |
| 89 | + await expect( |
| 90 | + example.getByLabel("CSV preview", { exact: true }), |
| 91 | + ).toBeVisible(); |
| 92 | + expect( |
| 93 | + await page.evaluate( |
| 94 | + () => document.documentElement.scrollWidth <= innerWidth, |
| 95 | + ), |
| 96 | + ).toBe(true); |
| 97 | + const response = await request.get("/examples/export-preview.md"); |
| 98 | + expect(response.ok()).toBe(true); |
| 99 | + const source = await response.text(); |
| 100 | + expect(source).toContain("saveFile={setPreview}"); |
| 101 | + expect(source).toContain("defaultSaveFile(preview"); |
| 102 | +}); |
0 commit comments