Add `texts` to src/test/elementTree.ts: every string of the tree, depth first, through arrays at any depth and fragments, each string as it is; a number is not a string and is left out, never rendered (Max's decision of 2026-10-24). Eight cases of elementTree.test.ts prove each point. The fifteen test files that kept a copy of `find` import the shared one, and the seven of them that kept a copy of `texts` import that too. The copies had the same traversal: same tests (137), same names, all green. The eleven calls by type become predicates (`(el) => el.type === X`), the Fragment search of TransactionTable.wiring.test.ts included. Only the imports tsc reports as unused go; `expand` keeps its own in OnboardingChecklist.test.tsx and ImportEmptyState.test.tsx. The `const texts` of CategoryDetailPanel.delete.test.ts and AccountsPage.delete.test.ts, of another meaning, stay as they are. Refs #495 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
3.9 KiB
TypeScript
97 lines
3.9 KiB
TypeScript
// CategoryCombobox — the open list follows its input (#429, review 25431).
|
|
//
|
|
// React's hooks come from the shared harness (src/test/hookHarness.ts, #409),
|
|
// which refuses `useLayoutEffect` and `useId`: this file alone maps
|
|
// `useLayoutEffect` onto the harness's `useEffect` (same dependency rule, run
|
|
// by `commit()` after the render, where React runs a layout effect before the
|
|
// paint) and `useId` onto a constant. No DOM: the input is a fake whose
|
|
// `getBoundingClientRect` the test moves, `window` and `document` are stubs,
|
|
// and `createPortal` is the identity. What stays out of reach: the paint, and
|
|
// React skipping the render on an unchanged state (here, the same object in
|
|
// the state slot stands for it).
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import type { ReactNode } from "react";
|
|
|
|
vi.mock("react", async (orig) => {
|
|
const fake = (await import("../../test/hookHarness")).fakeReact(await orig());
|
|
return { ...fake, useLayoutEffect: fake.useEffect, useId: () => "cb" };
|
|
});
|
|
vi.mock("react-dom", async (orig) => ({
|
|
...(await orig<typeof import("react-dom")>()),
|
|
createPortal: (children: ReactNode) => children,
|
|
}));
|
|
vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock());
|
|
|
|
import { hookHarness } from "../../test/hookHarness";
|
|
import { find } from "../../test/elementTree";
|
|
import CategoryCombobox from "./CategoryCombobox";
|
|
|
|
const harness = hookHarness();
|
|
|
|
const CATEGORIES = [{ id: 7, name: "Épicerie", parent_id: null, sort_order: 1 }];
|
|
|
|
/** The input's place on screen, which a render of the parent can change. */
|
|
const rect = { top: 100, bottom: 130, left: 50, width: 200 };
|
|
const fakeInput = { getBoundingClientRect: () => ({ ...rect }), blur: vi.fn() };
|
|
|
|
/** One render of the mounted combobox. */
|
|
const render = () =>
|
|
harness.render(() => CategoryCombobox({ categories: CATEGORIES, value: null, onChange: vi.fn() }) as ReactNode);
|
|
|
|
/** The open list's style, or null when no list is rendered. */
|
|
const listStyle = (tree: ReactNode) => {
|
|
const [list] = find(tree, (el) => el.type === "ul");
|
|
return list ? (list.props.style as { top?: number; bottom?: number }) : null;
|
|
};
|
|
|
|
/** Mounts the combobox, hands its input ref the fake input, then focuses it: the list opens. */
|
|
function open(): ReactNode {
|
|
const closed = render();
|
|
const [input] = find(closed, (el) => el.type === "input");
|
|
(input.props.ref as { current: unknown }).current = fakeInput;
|
|
harness.commit();
|
|
(input.props.onFocus as () => void)();
|
|
render();
|
|
harness.commit(); // places the list
|
|
return render();
|
|
}
|
|
|
|
beforeEach(() => {
|
|
harness.clear();
|
|
Object.assign(rect, { top: 100, bottom: 130 });
|
|
vi.stubGlobal("window", { innerHeight: 800, addEventListener: vi.fn(), removeEventListener: vi.fn() });
|
|
vi.stubGlobal("document", { body: {}, addEventListener: vi.fn(), removeEventListener: vi.fn() });
|
|
});
|
|
|
|
afterEach(() => {
|
|
harness.unmount();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe("CategoryCombobox — the open list follows its input (#429)", () => {
|
|
it("places the list under its input once open", () => {
|
|
expect(listStyle(render())).toBeNull();
|
|
harness.clear();
|
|
expect(listStyle(open())).toMatchObject({ top: 134 });
|
|
});
|
|
|
|
it("follows the input moved by a render of the parent, with no scroll nor resize", () => {
|
|
open();
|
|
// A window centered on screen grows when its content arrives: the input moves down.
|
|
Object.assign(rect, { top: 300, bottom: 330 });
|
|
render();
|
|
harness.commit();
|
|
expect(listStyle(render())).toMatchObject({ top: 334 });
|
|
});
|
|
|
|
it("keeps the same placement object when the input did not move, so React renders nothing more", () => {
|
|
open();
|
|
const placement = listStyle(render());
|
|
const before = harness.slots.find((slot) => (slot as { top?: number } | null)?.top === 134);
|
|
expect(before).toBeDefined();
|
|
render();
|
|
harness.commit();
|
|
expect(listStyle(render())).toEqual(placement);
|
|
expect(harness.slots).toContain(before);
|
|
});
|
|
});
|