Simpl-Resultat/src/test/elementTree.ts
le king fu 41f3b15f4d test: pin four cases the reviews of the stack left unpinned
Non-blocking suggestions of the independent reviews of PRs #490 to #493,
applied in a follow-up link above #493 (Max's decision of 2026-10-24):

- locales.keys.test.ts: a variable name that differs by its case only is a
  gap (`{{version}}` against `{{Version}}`, review 27037, H8); the header says
  that `{{- name}}` and `{{name}}` render alike with `escapeValue: false`.
- db.test.ts: a `)` glued to a word ends no path
  (`C:\Users\jean\Projet(2024)final\x.db`, review 27073, R10).
- TemplateActions.delete.test.ts: three templates, the middle one deleted, so
  a handler bound to the first or the last fails (review 27088, R11).
- ProfileSwitcher.wiring.test.ts: a locked profile that has a PIN opens the
  upsell, never the PIN entry; in Base it asks for the PIN (review 27084, R5).
- elementTree.ts: an absence is proved with `find`, never with `holding`
  (review 27088, S2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 09:09:36 -04:00

56 lines
3.2 KiB
TypeScript

// elementTree — searching the tree a component returns, for tests without jsdom (#467).
//
// A test that calls a component as a plain function, under the shared hook
// harness of this folder or without it, gets back a tree of React elements,
// not a DOM. These two helpers search that tree, shared since #467 (several
// test files still keep a copy of their own). Each semantic below is proved by
// elementTree.test.ts.
//
// - `find(node, pred)`: every element of the tree for which `pred` is true,
// depth first: an element before what it holds, siblings in order, and an
// element kept with what it holds when both match. Arrays (several children,
// the result of a `.map`) are read at any depth, and a fragment is an element
// like any other: `pred` sees it, then its children. The elements of one
// type: `find(tree, (el) => el.type === CategoryForm)`.
// - `holding(node, type)`: every element one of whose direct children is an
// element of `type`, in the same order and once each: the button that holds
// a `Trash2`, neither the icon nor the row around the button. A direct child
// is an item of `props.children`, an array there being read one level deep:
// in `<td>{rows}</td>` the items of `rows` are direct children of the cell,
// in `<td>{label}{rows}</td>` they are not. A fragment holds its own
// children, not its parent. An absence is proved with `find`, never with
// `holding`: its one-level read would miss an element nested deeper.
//
// Both read `props.children` only: an element passed in another prop
// (`icon={<Plus />}`) is not reached. A component in the tree is an element,
// never called, as under the harness: what a child component renders is not
// in the tree, and the test reaches it through the props of the child's
// element (`find(tree, (el) => el.type === CategoryForm)[0].props.onDelete()`).
import { isValidElement, type ReactElement, type ReactNode } from "react";
/** The props of an element of the tree: any props, and its children. */
export type TreeProps = Record<string, unknown> & { children?: ReactNode };
/** An element of the tree; `P` narrows its props for a test that knows them. */
export type TreeElement<P extends TreeProps = TreeProps> = ReactElement<P>;
/** Every element of `node` for which `pred` is true, depth first, through arrays and fragments. */
export function find<P extends TreeProps = TreeProps>(
node: ReactNode,
pred: (el: TreeElement<P>) => boolean,
): TreeElement<P>[] {
if (Array.isArray(node)) return node.flatMap((n) => find(n, pred));
if (!isValidElement(node)) return [];
const el = node as TreeElement<P>;
return [...(pred(el) ? [el] : []), ...find(el.props.children, pred)];
}
/** Every element of `node` one of whose direct children is an element of `type`, depth first. */
export function holding<P extends TreeProps = TreeProps>(node: ReactNode, type: unknown): TreeElement<P>[] {
if (Array.isArray(node)) return node.flatMap((n) => holding<P>(n, type));
if (!isValidElement(node)) return [];
const el = node as TreeElement<P>;
const children = ([] as ReactNode[]).concat(el.props.children ?? []);
const holds = children.some((c) => isValidElement(c) && c.type === type);
return [...(holds ? [el] : []), ...holding<P>(el.props.children, type)];
}