1import type { OlderSettledScope } from "@t3tools/client-runtime/state/threads";
2import { EnvironmentId, ProjectId } from "@t3tools/contracts";
3import { describe, expect, it } from "vite-plus/test";
4import { userEvent } from "vite-plus/test/browser";
5
6import { render } from "../../test/render";
7import { useOlderSettledShelf } from "./olderSettledShelf";
8
9const environmentId = EnvironmentId.make("env-a");
10const first = ProjectId.make("project-a");
11const second = ProjectId.make("project-b");
12
13function Shelf(props: { readonly scopes: ReadonlyArray<OlderSettledScope> }) {
14 const shelf = useOlderSettledShelf({ scopes: props.scopes, expanded: true, visibleCount: 0 });
15 return (
16 <>
17 <output>{shelf.pages.get(environmentId) ?? 0}</output>
18 <button type="button" onClick={() => shelf.grow(new Map([[environmentId, 1]]))}>
19 Grow
20 </button>
21 </>
22 );
23}
24
25describe("useOlderSettledShelf", () => {
26 it("keeps requested pages when the scope's projects reorder", async () => {
27 await using screen = await render(
28 <Shelf scopes={[{ environmentId, projectIds: [first, second] }]} />,
29 // A bare hook host; nothing here links or navigates.
30 { router: false },
31 );
32 await userEvent.click(screen.getByRole("button", { name: "Grow" }));
33 await expect.element(screen.getByRole("status")).toHaveTextContent("1");
34
35 // Loading a page can reorder the sidebar's projects; the shelf must not drop its pages.
36 await screen.rerender(<Shelf scopes={[{ environmentId, projectIds: [second, first] }]} />);
37 await expect.element(screen.getByRole("status")).toHaveTextContent("1");
38 });
39});