test(web): add 210 tests for lib utils, hooks, and UI components

Lib utilities: format (38), sanitize (12), project-colors (18),
csv-export (14).

Hooks: useDebounce (8), useTableSort (22), useLocalStorage (18),
useColumnConfig (19).

Components: BatchActionBar (17), SortableColumnHeader (14),
FilterChips (14), ErrorBoundary (16).

Web test suite: 63 → 75 files, 343 → 553 tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-10 17:11:00 +02:00
parent c0ba062460
commit 98dca6126f
12 changed files with 2280 additions and 0 deletions
+129
View File
@@ -0,0 +1,129 @@
import { describe, expect, it } from "vitest";
import { generateCsv } from "./csv-export.js";
// downloadCsv is a browser-only side-effect function (creates DOM elements and
// triggers a download). It is intentionally excluded here — testing it would
// require mocking the entire DOM download flow and adds no value for a pure
// utility test suite.
type Row = { name: string; value: number | null; note: string };
const columns = [
{ header: "Name", accessor: (r: Row) => r.name },
{ header: "Value", accessor: (r: Row) => r.value },
{ header: "Note", accessor: (r: Row) => r.note },
];
describe("generateCsv", () => {
it("produces a header row followed by a data row", () => {
const rows: Row[] = [{ name: "Alice", value: 42, note: "ok" }];
const csv = generateCsv(rows, columns);
const lines = csv.split("\n");
expect(lines[0]).toBe("Name,Value,Note");
expect(lines[1]).toBe("Alice,42,ok");
});
it("returns only the header row when there are no data rows", () => {
const csv = generateCsv([], columns);
// generateCsv always appends '\n' between header and body.
// With an empty body the result is "header\n"
expect(csv).toBe("Name,Value,Note\n");
});
it("handles multiple rows", () => {
const rows: Row[] = [
{ name: "Alice", value: 1, note: "a" },
{ name: "Bob", value: 2, note: "b" },
{ name: "Carol", value: 3, note: "c" },
];
const csv = generateCsv(rows, columns);
const lines = csv.split("\n");
expect(lines).toHaveLength(4); // header + 3 data rows
expect(lines[1]).toBe("Alice,1,a");
expect(lines[2]).toBe("Bob,2,b");
expect(lines[3]).toBe("Carol,3,c");
});
it("wraps cell values that contain a comma in double quotes", () => {
const rows: Row[] = [{ name: "Smith, John", value: 10, note: "ok" }];
const csv = generateCsv(rows, columns);
const dataLine = csv.split("\n")[1];
expect(dataLine).toBe('"Smith, John",10,ok');
});
it("escapes double quotes inside cell values by doubling them", () => {
const rows: Row[] = [{ name: 'Say "hello"', value: 0, note: "x" }];
const csv = generateCsv(rows, columns);
const dataLine = csv.split("\n")[1];
expect(dataLine).toBe('"Say ""hello""",0,x');
});
it("wraps cells that contain a newline in double quotes", () => {
const rows: Row[] = [{ name: "line1\nline2", value: 0, note: "x" }];
const csv = generateCsv(rows, columns);
// The header is the first line; the wrapped cell starts on line 2
const raw = csv.split("\n");
// Header + quoted cell (which contains an embedded newline) + remainder
expect(raw[0]).toBe("Name,Value,Note");
expect(raw[1]).toBe('"line1');
expect(raw[2]).toContain("line2");
});
it("converts null accessor results to empty strings", () => {
const rows: Row[] = [{ name: "Test", value: null, note: "" }];
const csv = generateCsv(rows, columns);
const dataLine = csv.split("\n")[1];
expect(dataLine).toBe("Test,,");
});
it("converts undefined accessor results to empty strings", () => {
type Partial = { name?: string };
const partialCols = [{ header: "Name", accessor: (r: Partial) => r.name }];
const csv = generateCsv([{}] as Partial[], partialCols);
expect(csv.split("\n")[1]).toBe("");
});
it("wraps header cells that contain a comma", () => {
const colsWithComma = [{ header: "Last, First", accessor: (r: Row) => r.name }];
const csv = generateCsv([], colsWithComma);
expect(csv.startsWith('"Last, First"')).toBe(true);
});
it("handles numeric zero values without treating them as empty", () => {
const rows: Row[] = [{ name: "Zero", value: 0, note: "" }];
const csv = generateCsv(rows, columns);
const dataLine = csv.split("\n")[1];
expect(dataLine).toBe("Zero,0,");
});
it("handles boolean accessor return values", () => {
const boolCols = [
{ header: "Active", accessor: (_r: unknown) => true },
{ header: "Deleted", accessor: (_r: unknown) => false },
];
const csv = generateCsv([{}], boolCols);
expect(csv.split("\n")[1]).toBe("true,false");
});
it("handles a value that contains both a comma and a double quote", () => {
const rows: Row[] = [{ name: 'a, "b"', value: 1, note: "" }];
const csv = generateCsv(rows, columns);
const dataLine = csv.split("\n")[1];
expect(dataLine).toBe('"a, ""b""",1,');
});
it("does not wrap plain numeric strings", () => {
const numCols = [{ header: "Num", accessor: (_r: unknown) => "12345" }];
const csv = generateCsv([{}], numCols);
expect(csv.split("\n")[1]).toBe("12345");
});
it("generates correct CSV for a single-column dataset", () => {
const singleCol = [{ header: "ID", accessor: (_r: unknown) => "abc" }];
const csv = generateCsv([{}, {}], singleCol);
const lines = csv.split("\n");
expect(lines[0]).toBe("ID");
expect(lines[1]).toBe("abc");
expect(lines[2]).toBe("abc");
});
});