forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

capped-list.test.ts64 lines, 2.7 KiBHistoryRawDownload
1import type { Page, PageParams } from "@/api/client";
2import { isTruncated, loadAllPages, loadCappedList, loadWindowedList } from "./capped-list";
3
4const page = (items: number[], total: number): Page<number> => ({ items, total, limit: null, offset: 0 });
5
6it("asks for the cap and reports the server total", async () => {
7 const fetch = vi.fn(async (_params?: PageParams) => page([1, 2], 5));
8 const list = await loadCappedList(fetch, 2, false);
9 expect(fetch).toHaveBeenCalledWith({ limit: 2 });
10 expect(list).toEqual({ items: [1, 2], total: 5 });
11 expect(isTruncated(list)).toBe(true);
12});
13
14it("fetches every row without a limit when asked for all", async () => {
15 const fetch = vi.fn(async (_params?: PageParams) => page([1, 2, 3, 4, 5], 5));
16 const list = await loadCappedList(fetch, 2, true);
17 expect(fetch).toHaveBeenCalledWith(undefined);
18 expect(isTruncated(list)).toBe(false);
19});
20
21it("never reports a total below the rows it holds", async () => {
22 const list = await loadCappedList(async () => page([1, 2, 3], 1), 10, false);
23 expect(list.total).toBe(3);
24 expect(isTruncated(undefined)).toBe(false);
25});
26
27it("reads every page of a paged list, in order", async () => {
28 const all = Array.from({ length: 7 }, (_, index) => index);
29 const fetch = vi.fn(async (params?: PageParams) => {
30 const offset = params?.offset ?? 0;
31 return page(all.slice(offset, offset + (params?.limit ?? all.length)), all.length);
32 });
33 expect(await loadAllPages(fetch, 3)).toEqual(all);
34 expect(fetch).toHaveBeenCalledTimes(3);
35 expect(fetch).toHaveBeenCalledWith({ limit: 3, offset: 6 });
36});
37
38it("stops after one request when the first page holds everything", async () => {
39 const fetch = vi.fn(async () => page([1, 2], 2));
40 expect(await loadAllPages(fetch, 100)).toEqual([1, 2]);
41 expect(fetch).toHaveBeenCalledTimes(1);
42});
43
44it("follows offsets until the windowed total", async () => {
45 const fetch = vi.fn(async (params?: PageParams) => {
46 if (!params?.offset) return page([1, 2], 5);
47 if (params.offset === 2) return page([3, 4], 5);
48 return page([5], 5);
49 });
50 const list = await loadWindowedList(fetch, 2);
51 expect(fetch).toHaveBeenNthCalledWith(1, { limit: 2, offset: 0 });
52 expect(fetch).toHaveBeenNthCalledWith(2, { limit: 2, offset: 2 });
53 expect(fetch).toHaveBeenNthCalledWith(3, { limit: 2, offset: 4 });
54 expect(list).toEqual({ items: [1, 2, 3, 4, 5], total: 5 });
55 expect(isTruncated(list)).toBe(false);
56});
57
58it("stops on an empty page instead of looping a shrinking window", async () => {
59 const fetch = vi.fn(async (params?: PageParams) => (params?.offset ? page([], 5) : page([1, 2], 5)));
60 const list = await loadWindowedList(fetch, 2);
61 expect(fetch).toHaveBeenCalledTimes(2);
62 expect(list.items).toEqual([1, 2]);
63 expect(list.total).toBe(5);
64});