All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 2m11s
Self-review: the docblock says that neither helper calls a component of the tree, but only find had a test for it. holding gets its own (a holder that a child component renders is not found, the component is never called). The header no longer reads as if no test file kept a copy of its own: several still do. Resolves #467 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
6.3 KiB
TypeScript
136 lines
6.3 KiB
TypeScript
// elementTree — each semantic of the two helpers proved by its own test (#467).
|
|
//
|
|
// The tests that search a rendered tree can only be as true as these helpers.
|
|
// The trees are built here with the real React (`createElement`), in the shapes
|
|
// JSX gives them: several children in an array, a `.map` as one child, nested
|
|
// arrays, fragments, texts and holes.
|
|
import { describe, it, expect, vi } from "vitest";
|
|
import { createElement as h, Fragment, type ReactNode } from "react";
|
|
import { Pencil, Trash2 } from "lucide-react";
|
|
import { find, holding, type TreeElement } from "./elementTree";
|
|
|
|
/** The `id` prop of each element, in the order the helper returns them. */
|
|
const ids = (elements: TreeElement[]) => elements.map((el) => el.props.id);
|
|
|
|
const all = () => true;
|
|
|
|
describe("elementTree — find", () => {
|
|
const tree = h(
|
|
"section",
|
|
{ id: "s" },
|
|
h("div", { id: "a" }, h("span", { id: "a1" }), h("span", { id: "a2" })),
|
|
h("div", { id: "b" }, h("div", { id: "b1" })),
|
|
);
|
|
|
|
it("returns every element the predicate keeps, depth first: an element before what it holds", () => {
|
|
expect(ids(find(tree, all))).toEqual(["s", "a", "a1", "a2", "b", "b1"]);
|
|
expect(ids(find(tree, (el) => el.type === "span"))).toEqual(["a1", "a2"]);
|
|
});
|
|
|
|
it("keeps an element and what it holds when both match", () => {
|
|
expect(ids(find(tree, (el) => el.type === "div"))).toEqual(["a", "b", "b1"]);
|
|
});
|
|
|
|
it("reads arrays at any depth: a .map as one child, a list beside a sibling, a nested array, a top-level array", () => {
|
|
const rows = [1, 2].map((n) => h("li", { key: n, id: `li${n}` }));
|
|
const list = h("ul", { key: "ul", id: "ul" }, h("li", { id: "first" }), rows);
|
|
const node: ReactNode = [list, [[h("p", { key: "p", id: "p" })]]];
|
|
expect(ids(find(node, (el) => el.type === "li"))).toEqual(["first", "li1", "li2"]);
|
|
expect(ids(find(node, (el) => el.type === "p"))).toEqual(["p"]);
|
|
expect(ids(find(h("ol", { id: "ol" }, rows), (el) => el.type === "li"))).toEqual(["li1", "li2"]);
|
|
});
|
|
|
|
it("treats a fragment as an element: the predicate sees it, then its children", () => {
|
|
const node = h("div", { id: "d" }, h(Fragment, null, h("i", { id: "i1" }), h("i", { id: "i2" })));
|
|
expect(ids(find(node, (el) => el.type === "i"))).toEqual(["i1", "i2"]);
|
|
const fragments = find(node, (el) => el.type === Fragment);
|
|
expect(fragments).toHaveLength(1);
|
|
expect(fragments[0].type).toBe(Fragment);
|
|
});
|
|
|
|
it("skips texts, numbers and holes", () => {
|
|
const node = h("p", { id: "p" }, "text", 0, null, undefined, false, true, h("b", { id: "b" }));
|
|
expect(ids(find(node, all))).toEqual(["p", "b"]);
|
|
expect(find("text", all)).toEqual([]);
|
|
expect(find(null, all)).toEqual([]);
|
|
});
|
|
|
|
it("reads props.children only: an element passed in another prop is not reached", () => {
|
|
const node = h("button", { id: "button", icon: h(Trash2) }, "label");
|
|
expect(find(node, (el) => el.type === Trash2)).toEqual([]);
|
|
expect(ids(find(node, all))).toEqual(["button"]);
|
|
});
|
|
|
|
it("never calls a component of the tree: what it renders is not there", () => {
|
|
const Child = vi.fn(() => h("b", { id: "inside" }));
|
|
const node = h("div", { id: "d" }, h(Child, { id: "child" }));
|
|
expect(find(node, (el) => el.type === "b")).toEqual([]);
|
|
expect(ids(find(node, (el) => el.type === Child))).toEqual(["child"]);
|
|
expect(Child).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("elementTree — holding", () => {
|
|
it("returns the element whose child is of the type: the button, neither the icon nor the row around it", () => {
|
|
const row = h(
|
|
"div",
|
|
{ id: "row" },
|
|
h("span", { id: "label" }, "Loyer"),
|
|
h("button", { id: "edit" }, h(Pencil)),
|
|
h("button", { id: "delete" }, h(Trash2), "Supprimer"),
|
|
);
|
|
expect(ids(holding(row, Trash2))).toEqual(["delete"]);
|
|
expect(ids(holding(row, Pencil))).toEqual(["edit"]);
|
|
});
|
|
|
|
it("returns every holder, depth first, a holder before a holder it holds", () => {
|
|
const rows = [1, 2, 3].map((n) =>
|
|
h("tr", { key: n, id: `row${n}` }, h("td", { id: `cell${n}` }, h("button", { id: `delete${n}` }, h(Trash2)))),
|
|
);
|
|
expect(ids(holding(h("tbody", { id: "body" }, rows), Trash2))).toEqual(["delete1", "delete2", "delete3"]);
|
|
const nested = h("div", { id: "outer" }, h(Trash2), h("div", { id: "inner" }, h(Trash2)));
|
|
expect(ids(holding(nested, Trash2))).toEqual(["outer", "inner"]);
|
|
});
|
|
|
|
it("returns a holder once, however many children of the type it holds", () => {
|
|
const button = h("button", { id: "b" }, h(Trash2, { key: 1 }), h(Trash2, { key: 2 }));
|
|
expect(ids(holding(button, Trash2))).toEqual(["b"]);
|
|
});
|
|
|
|
it("compares the type by identity: a tag name, a component", () => {
|
|
const Child = () => null;
|
|
const Other = () => null;
|
|
const node = h("td", { id: "cell" }, h("button", { id: "button" }), h("div", { id: "div" }, h(Child)));
|
|
expect(ids(holding(node, "button"))).toEqual(["cell"]);
|
|
expect(ids(holding(node, Child))).toEqual(["div"]);
|
|
expect(holding(node, Other)).toEqual([]);
|
|
});
|
|
|
|
it("reads props.children one level deep: a list alone is direct, a list beside a sibling is not", () => {
|
|
const alone = h("td", { id: "alone" }, [h(Trash2, { key: 1 })]);
|
|
const beside = h("td", { id: "beside" }, "label", [h(Trash2, { key: 1 })]);
|
|
expect(ids(holding(alone, Trash2))).toEqual(["alone"]);
|
|
expect(holding(beside, Trash2)).toEqual([]);
|
|
});
|
|
|
|
it("never calls a component of the tree: a holder its child renders is not there", () => {
|
|
const Child = vi.fn(() => h("button", { id: "inside" }, h(Trash2)));
|
|
const node = h("div", { id: "d" }, h(Child));
|
|
expect(holding(node, Trash2)).toEqual([]);
|
|
expect(ids(holding(node, Child))).toEqual(["d"]);
|
|
expect(Child).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("lets a fragment hold its own children, not its parent", () => {
|
|
const node = h("div", { id: "d" }, h(Fragment, null, h(Trash2)));
|
|
const holders = holding(node, Trash2);
|
|
expect(holders).toHaveLength(1);
|
|
expect(holders[0].type).toBe(Fragment);
|
|
});
|
|
|
|
it("reads props.children only, and skips texts and holes", () => {
|
|
expect(holding(h("button", { id: "b", icon: h(Trash2) }, "label"), Trash2)).toEqual([]);
|
|
expect(holding(h("p", { id: "p" }, "text", null, false), Trash2)).toEqual([]);
|
|
expect(holding(null, Trash2)).toEqual([]);
|
|
});
|
|
});
|