|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { act, createElement } from "react"; |
| 3 | +import type { ComponentProps } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | + |
| 6 | +import { CSV_GRID_ROW_CAP } from "../lib/csv-grid.js"; |
| 7 | +import type { CsvSelection } from "./csv-table.js"; |
| 8 | +import { CsvTable, csvFilterSpecs } from "./csv-table.js"; |
| 9 | + |
| 10 | +function mount(props: ComponentProps<typeof CsvTable>) { |
| 11 | + const container = document.createElement("div"); |
| 12 | + document.body.appendChild(container); |
| 13 | + const root = createRoot(container); |
| 14 | + const render = (next: ComponentProps<typeof CsvTable>) => |
| 15 | + act(() => { |
| 16 | + root.render(createElement(CsvTable, next)); |
| 17 | + }); |
| 18 | + render(props); |
| 19 | + return { |
| 20 | + container, |
| 21 | + render, |
| 22 | + firstColumn: () => |
| 23 | + [...container.querySelectorAll('tbody [data-cell$=":0"]')].map( |
| 24 | + (cell) => cell.textContent, |
| 25 | + ), |
| 26 | + status: () => container.querySelector('[role="status"]')?.textContent, |
| 27 | + unmount: () => { |
| 28 | + act(() => root.unmount()); |
| 29 | + container.remove(); |
| 30 | + }, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +const click = (element: Element | null, init: MouseEventInit = {}) => |
| 35 | + act(() => { |
| 36 | + element?.dispatchEvent(new MouseEvent("click", { bubbles: true, ...init })); |
| 37 | + }); |
| 38 | + |
| 39 | +const header = (container: HTMLElement, name: string) => |
| 40 | + [...container.querySelectorAll("thead button")].find( |
| 41 | + (button) => button.textContent === name, |
| 42 | + ) ?? null; |
| 43 | + |
| 44 | +describe("CsvTable interactive", () => { |
| 45 | + test("search, sort and select-all cover rows past the render cap", () => { |
| 46 | + const reported: (CsvSelection | null)[] = []; |
| 47 | + const rows = Array.from( |
| 48 | + { length: CSV_GRID_ROW_CAP + 500 }, |
| 49 | + (_, index) => `item-${index},${index}`, |
| 50 | + ); |
| 51 | + const props = { |
| 52 | + text: ["name,amount", ...rows].join("\n"), |
| 53 | + interactive: true, |
| 54 | + onSelectionChange: (selection: CsvSelection | null) => |
| 55 | + reported.push(selection), |
| 56 | + }; |
| 57 | + const { container, render, firstColumn, status, unmount } = mount(props); |
| 58 | + expect(firstColumn()).toHaveLength(CSV_GRID_ROW_CAP); |
| 59 | + act(() => { |
| 60 | + container.querySelector("table")?.dispatchEvent( |
| 61 | + new KeyboardEvent("keydown", { |
| 62 | + key: "a", |
| 63 | + ctrlKey: true, |
| 64 | + bubbles: true, |
| 65 | + }), |
| 66 | + ); |
| 67 | + }); |
| 68 | + expect(reported.at(-1)?.rows).toBe(CSV_GRID_ROW_CAP + 500); |
| 69 | + |
| 70 | + click(header(container, "amount")); |
| 71 | + click(header(container, "amount")); |
| 72 | + expect(firstColumn()[0]).toBe(`item-${CSV_GRID_ROW_CAP + 499}`); |
| 73 | + |
| 74 | + render({ ...props, query: "ITEM-1499" }); |
| 75 | + expect(firstColumn()).toEqual(["item-1499"]); |
| 76 | + expect(status()).toContain("1 of 1,500 rows"); |
| 77 | + unmount(); |
| 78 | + }); |
| 79 | + |
| 80 | + test("numbers sort as numbers, blanks last, and a blank filter matches blanks", () => { |
| 81 | + const text = 'name,amount,kind\na,"(1,234)",x\nb,,y\nc,$-5,x\nd,20,\ne,3,y'; |
| 82 | + const { container, render, firstColumn, unmount } = mount({ |
| 83 | + text, |
| 84 | + interactive: true, |
| 85 | + }); |
| 86 | + click(header(container, "amount")); |
| 87 | + expect(firstColumn()).toEqual(["a", "c", "e", "d", "b"]); |
| 88 | + |
| 89 | + const kind = csvFilterSpecs(text).find((spec) => spec.label === "kind"); |
| 90 | + const blank = kind?.options.at(-1)?.value; |
| 91 | + expect(kind?.options.at(-1)?.label).toBe("(blank)"); |
| 92 | + render({ text, interactive: true, filters: { [kind!.id]: blank! } }); |
| 93 | + expect(firstColumn()).toEqual(["d"]); |
| 94 | + unmount(); |
| 95 | + }); |
| 96 | + |
| 97 | + test("a selected range reports the sorted, filtered cells as TSV", () => { |
| 98 | + const reported: (CsvSelection | null)[] = []; |
| 99 | + const { container, unmount } = mount({ |
| 100 | + text: "name,amount,note\nc,3,keep\na,1,keep\nb,2,drop\nd,4,keep", |
| 101 | + interactive: true, |
| 102 | + query: "keep", |
| 103 | + onSelectionChange: (selection) => reported.push(selection), |
| 104 | + }); |
| 105 | + click(header(container, "name")); |
| 106 | + click(container.querySelector('[data-cell="0:0"]')); |
| 107 | + click(container.querySelector('[data-cell="1:1"]'), { shiftKey: true }); |
| 108 | + expect(reported.at(-1)?.toTsv()).toBe("a\t1\nc\t3"); |
| 109 | + unmount(); |
| 110 | + }); |
| 111 | + |
| 112 | + test("a failed copy is reported until the selection moves", async () => { |
| 113 | + const original = Object.getOwnPropertyDescriptor(navigator, "clipboard"); |
| 114 | + Object.defineProperty(navigator, "clipboard", { |
| 115 | + configurable: true, |
| 116 | + value: { writeText: () => Promise.reject(new Error("blocked")) }, |
| 117 | + }); |
| 118 | + const { container, unmount } = mount({ |
| 119 | + text: "name,amount\na,1\nb,2", |
| 120 | + interactive: true, |
| 121 | + }); |
| 122 | + const copyStatus = () => |
| 123 | + container.querySelectorAll('[role="status"]')[1]?.textContent; |
| 124 | + try { |
| 125 | + click(container.querySelector('[data-cell="0:0"]')); |
| 126 | + await act(async () => { |
| 127 | + container.querySelector("table")?.dispatchEvent( |
| 128 | + new KeyboardEvent("keydown", { |
| 129 | + key: "c", |
| 130 | + ctrlKey: true, |
| 131 | + bubbles: true, |
| 132 | + }), |
| 133 | + ); |
| 134 | + }); |
| 135 | + expect(copyStatus()).toContain("Couldn't copy"); |
| 136 | + click(container.querySelector('[data-cell="1:0"]')); |
| 137 | + expect(copyStatus()).toBe(""); |
| 138 | + } finally { |
| 139 | + unmount(); |
| 140 | + if (original === undefined) { |
| 141 | + Reflect.deleteProperty(navigator, "clipboard"); |
| 142 | + } else { |
| 143 | + Object.defineProperty(navigator, "clipboard", original); |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + test("new text resets sort and selection", () => { |
| 149 | + const selections: (CsvSelection | null)[] = []; |
| 150 | + const props = { |
| 151 | + interactive: true, |
| 152 | + onSelectionChange: (selection: CsvSelection | null) => |
| 153 | + selections.push(selection), |
| 154 | + }; |
| 155 | + const { container, render, firstColumn, unmount } = mount({ |
| 156 | + ...props, |
| 157 | + text: "name,amount\nb,2\na,1", |
| 158 | + }); |
| 159 | + click(header(container, "name")); |
| 160 | + click(container.querySelector('[data-cell="0:0"]')); |
| 161 | + |
| 162 | + render({ ...props, text: "name,amount\nz,9\ny,8" }); |
| 163 | + expect(firstColumn()).toEqual(["z", "y"]); |
| 164 | + expect(container.querySelector("[aria-sort]")).toBeNull(); |
| 165 | + expect(selections.at(-1)).toBeNull(); |
| 166 | + unmount(); |
| 167 | + }); |
| 168 | +}); |
0 commit comments