dbx/packages/app-tests/gridSelection.test.ts

87 lines
2.7 KiB
TypeScript

import { strict as assert } from "node:assert";
import test from "node:test";
import {
allCellsSelectionRange,
columnSelectionRange,
extractSelection,
formatSelectionAsCsv,
formatSelectionAsJson,
formatSelectionAsSqlInList,
formatSelectionAsTsv,
isCellInSelection,
normalizeSelectionRange,
rowSelectionRange,
} from "../../apps/desktop/src/lib/gridSelection.ts";
test("normalizes a dragged cell range in either direction", () => {
const range = normalizeSelectionRange({ rowIndex: 4, colIndex: 3 }, { rowIndex: 1, colIndex: 0 });
assert.deepEqual(range, {
startRow: 1,
endRow: 4,
startCol: 0,
endCol: 3,
});
assert.equal(isCellInSelection(2, 1, range), true);
assert.equal(isCellInSelection(5, 1, range), false);
});
test("extracts selection rows and columns from a rectangular range", () => {
const selection = extractSelection(
["id", "name", "active"],
[
[1, "Ada", true],
[2, "Linus", false],
[3, null, true],
],
{ startRow: 0, endRow: 1, startCol: 1, endCol: 2 },
);
assert.deepEqual(selection.columns, ["name", "active"]);
assert.deepEqual(selection.rows, [
["Ada", true],
["Linus", false],
]);
});
test("builds whole row, column, and table selection ranges", () => {
assert.deepEqual(rowSelectionRange(2, 4), { startRow: 2, endRow: 2, startCol: 0, endCol: 3 });
assert.deepEqual(rowSelectionRange(4, 4, 2), { startRow: 2, endRow: 4, startCol: 0, endCol: 3 });
assert.deepEqual(columnSelectionRange(5, 1), { startRow: 0, endRow: 4, startCol: 1, endCol: 1 });
assert.deepEqual(columnSelectionRange(5, 3, 1), { startRow: 0, endRow: 4, startCol: 1, endCol: 3 });
assert.deepEqual(allCellsSelectionRange(3, 2), { startRow: 0, endRow: 2, startCol: 0, endCol: 1 });
assert.equal(rowSelectionRange(0, 0), null);
assert.equal(columnSelectionRange(0, 0), null);
assert.equal(allCellsSelectionRange(0, 2), null);
});
test("formats selected cells as TSV, CSV, JSON, and SQL values", () => {
const selection = {
columns: ["name", "note"],
rows: [
["Ada", "math"],
["Bob", 'quote "here"'],
["O'Hara", null],
],
};
assert.equal(formatSelectionAsTsv(selection), 'Ada\tmath\nBob\tquote "here"\nO\'Hara\tNULL');
assert.equal(
formatSelectionAsCsv(selection),
'"name","note"\n"Ada","math"\n"Bob","quote ""here"""\n"O\'Hara","NULL"',
);
assert.equal(
formatSelectionAsJson(selection),
JSON.stringify(
[
{ name: "Ada", note: "math" },
{ name: "Bob", note: 'quote "here"' },
{ name: "O'Hara", note: null },
],
null,
2,
),
);
assert.equal(formatSelectionAsSqlInList(selection), "('Ada', 'math', 'Bob', 'quote \"here\"', 'O''Hara', NULL)");
});