Open History to see the last commit
1import { RegistryContext } from "@effect/atom-react";
2import { EnvironmentId, type VcsListRefsResult } from "@t3tools/contracts";
3import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
4import { describe, expect, it, vi } from "vite-plus/test";
5
6const pages = vi.hoisted(() => new Map<string, unknown>());
7const pageKey = (query = "", cursor = 0) => JSON.stringify([query, cursor]);
8
9vi.mock(import("./vcs"), async (original) => {
10 const actual = await original();
11 return {
12 ...actual,
13 vcsEnvironment: {
14 ...actual.vcsEnvironment,
15 listRefs: ((target: { input: { query?: string; cursor?: number } }) =>
16 pages.get(
17 pageKey(target.input.query, target.input.cursor),
18 )) as unknown as typeof actual.vcsEnvironment.listRefs,
19 },
20 };
21});
22
23import { render } from "../../test/render";
24import { usePaginatedBranches } from "./queries";
25
26const page = (names: ReadonlyArray<string>): VcsListRefsResult => ({
27 refs: names.map((name) => ({ name, current: false, isDefault: false, worktreePath: null })),
28 isRepo: true,
29 hasPrimaryRemote: true,
30 nextCursor: null,
31 totalCount: names.length,
32});
33
34function pageAtom(query: string, cursor?: number) {
35 const atom = Atom.make<AsyncResult.AsyncResult<VcsListRefsResult, Error>>(
36 AsyncResult.initial(true),
37 ).pipe(Atom.keepAlive);
38 pages.set(pageKey(query, cursor), atom);
39 return atom;
40}
41
42function Refs({ query }: { query: string }) {
43 const { refs, data, loadNext } = usePaginatedBranches({
44 environmentId: EnvironmentId.make("primary"),
45 cwd: "/repo",
46 query,
47 });
48 return (
49 <>
50 <span>
51 {data === null
52 ? "loading"
53 : refs.map((ref) => (ref.isRemote ? `${ref.name} remote` : ref.name)).join(",")}
54 </span>
55 <button type="button" onClick={loadNext}>
56 Load more
57 </button>
58 </>
59 );
60}
61
62describe("usePaginatedBranches", () => {
63 it("keeps the local branch when a later page returns its remote namesake", async () => {
64 const registry = AtomRegistry.make();
65 const first = pageAtom("topic");
66 const next = pageAtom("topic", 1);
67 const name = "origin/topic";
68 registry.set(first, AsyncResult.success({ ...page([name]), nextCursor: 1, totalCount: 3 }));
69 const last = page([name, "second-page"]);
70 registry.set(
71 next,
72 AsyncResult.success({
73 ...last,
74 refs: last.refs.map((ref) => (ref.name === name ? { ...ref, isRemote: true } : ref)),
75 totalCount: 3,
76 }),
77 );
78 await using screen = await render(
79 <RegistryContext.Provider value={registry}>
80 <Refs query="topic" />
81 </RegistryContext.Provider>,
82 { router: false },
83 );
84 await expect.element(screen.getByText(name, { exact: true })).toBeInTheDocument();
85 await screen.getByRole("button", { name: "Load more" }).click();
86 await expect
87 .element(screen.getByText(`${name},second-page`, { exact: true }))
88 .toBeInTheDocument();
89 });
90
91 it("keeps the shown refs while a new search is pending", async () => {
92 const registry = AtomRegistry.make();
93 const all = pageAtom("");
94 const filtered = pageAtom("ma");
95 const ui = (query: string) => (
96 <RegistryContext.Provider value={registry}>
97 <Refs query={query} />
98 </RegistryContext.Provider>
99 );
100 await using screen = await render(ui(""), { router: false });
101 registry.set(all, AsyncResult.success(page(["develop", "main"])));
102 await expect.element(screen.getByText("develop,main")).toBeInTheDocument();
103
104 await screen.rerender(ui("ma"));
105 await expect.element(screen.getByText("develop,main")).toBeInTheDocument();
106
107 registry.set(filtered, AsyncResult.success(page(["main"])));
108 await expect.element(screen.getByText("main", { exact: true })).toBeInTheDocument();
109 });
110});