Files
descrybe/apps/web/src/lib/products-selection.test.ts
T
greeneclipse 8580c996c3 Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
2026-08-09 22:47:43 +02:00

72 lines
2.2 KiB
TypeScript

/**
* Products list selection + export helpers (node:test).
*/
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
canOpenExportDialog,
normalizeStatusBadgeLabel,
selectableProductIds,
selectedRawProductIds,
selectionHas,
setProductSelected,
toExportProductIds,
toggleSelectAllIds
} from "./products-selection.ts";
describe("setProductSelected / selectionHas", () => {
it("adds and removes without duplicate string/number ids", () => {
assert.deepEqual(setProductSelected([], 1, true), [1]);
assert.deepEqual(setProductSelected([1], "1", true), [1]);
assert.deepEqual(setProductSelected([1, 2], "1", false), [2]);
assert.equal(selectionHas(["1", 2], 1), true);
assert.equal(selectionHas([2], 1), false);
});
});
describe("toggleSelectAllIds / selectableProductIds", () => {
it("selects then clears visible ids, keeping off-page selection", () => {
assert.deepEqual(toggleSelectAllIds([9], [1, 2]), [9, 1, 2]);
assert.deepEqual(toggleSelectAllIds([9, 1, 2], [1, 2]), [9]);
assert.deepEqual(toggleSelectAllIds([], []), []);
});
it("skips in-flight processing rows for select-all", () => {
assert.deepEqual(
selectableProductIds([
{ id: 1, processing_status: "completed" },
{ id: 2, processing_status: "processing" },
{ id: 3 }
]),
[1, 3]
);
});
});
describe("selectedRawProductIds", () => {
it("resolves raw ids by list kind", () => {
const rows = [
{ id: "p1", raw_product_id: "r1" },
{ id: "p2", raw_product_id: null },
{ id: "r3" }
];
assert.deepEqual(selectedRawProductIds(rows, ["p1", "p2"], "processed"), ["r1"]);
assert.deepEqual(selectedRawProductIds(rows, ["r3"], "raw"), ["r3"]);
});
});
describe("export helpers + status badge label", () => {
it("stringifies export ids and gates the dialog", () => {
assert.deepEqual(toExportProductIds([1, "2"]), ["1", "2"]);
assert.equal(canOpenExportDialog(0), false);
assert.equal(canOpenExportDialog(3), true);
});
it("normalizeStatusBadgeLabel matches badge empty handling", () => {
assert.equal(normalizeStatusBadgeLabel("completed"), "completed");
assert.equal(normalizeStatusBadgeLabel(" "), "unknown");
assert.equal(normalizeStatusBadgeLabel(null), "unknown");
});
});