1import type { VcsListRefsResult } from "@t3tools/contracts";
2import { AsyncResult } from "effect/reactivity";
3import { describe, expect, it } from "vite-plus/test";
4
5import {
6 isPaginatedBranchesNextPagePending,
7 shouldLoadNextBranchPageAfterScroll,
8} from "./paginatedBranches";
9
10const FIRST_PAGE: VcsListRefsResult = {
11 refs: [],
12 isRepo: true,
13 hasPrimaryRemote: true,
14 nextCursor: 100,
15 totalCount: 150,
16};
17
18const LAST_PAGE: VcsListRefsResult = {
19 ...FIRST_PAGE,
20 nextCursor: null,
21};
22
23describe("paginated branch loading state", () => {
24 it("does not label a first-page background refresh as loading more refs", () => {
25 expect(
26 isPaginatedBranchesNextPagePending([
27 AsyncResult.success(FIRST_PAGE, {
28 waiting: true,
29 }),
30 ]),
31 ).toBe(false);
32 });
33
34 it("only reports loading more while a new cursor has no value", () => {
35 expect(
36 isPaginatedBranchesNextPagePending([
37 AsyncResult.success(FIRST_PAGE),
38 AsyncResult.initial<VcsListRefsResult>(true),
39 ]),
40 ).toBe(true);
41
42 expect(
43 isPaginatedBranchesNextPagePending([
44 AsyncResult.success(FIRST_PAGE),
45 AsyncResult.success(LAST_PAGE),
46 ]),
47 ).toBe(false);
48 });
49});
50
51describe("paginated branch scroll trigger", () => {
52 it("does not load from mount, layout, or an unchanged scroll position", () => {
53 expect(
54 shouldLoadNextBranchPageAfterScroll({
55 previousScrollTop: null,
56 scrollTop: 0,
57 scrollHeight: 224,
58 clientHeight: 224,
59 }),
60 ).toBe(false);
61
62 expect(
63 shouldLoadNextBranchPageAfterScroll({
64 previousScrollTop: 120,
65 scrollTop: 120,
66 scrollHeight: 344,
67 clientHeight: 224,
68 }),
69 ).toBe(false);
70 });
71
72 it("does not load when scrolling upward near the bottom", () => {
73 expect(
74 shouldLoadNextBranchPageAfterScroll({
75 previousScrollTop: 160,
76 scrollTop: 150,
77 scrollHeight: 450,
78 clientHeight: 224,
79 }),
80 ).toBe(false);
81 });
82
83 it("loads only after a downward scroll reaches the end threshold", () => {
84 expect(
85 shouldLoadNextBranchPageAfterScroll({
86 previousScrollTop: 0,
87 scrollTop: 100,
88 scrollHeight: 800,
89 clientHeight: 224,
90 }),
91 ).toBe(false);
92
93 expect(
94 shouldLoadNextBranchPageAfterScroll({
95 previousScrollTop: 100,
96 scrollTop: 500,
97 scrollHeight: 800,
98 clientHeight: 224,
99 }),
100 ).toBe(true);
101
102 expect(
103 shouldLoadNextBranchPageAfterScroll({
104 previousScrollTop: 499.5,
105 scrollTop: 500,
106 scrollHeight: 800,
107 clientHeight: 224,
108 }),
109 ).toBe(true);
110 });
111});