hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { createRoot, createSignal } from "solid-js"; |
| 2 | import { describe, expect, it, vi } from "vitest"; |
| 3 | import type { Page, PageParams } from "@/api/client"; |
| 4 | import { createInfiniteList } from "./infinite-list"; |
| 5 | |
| 6 | const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); |
| 7 | |
| 8 | function server(total: number) { |
| 9 | const rows = Array.from({ length: total }, (_, i) => i); |
| 10 | return vi.fn(async (query: string, params: PageParams): Promise<Page<string>> => { |
| 11 | const offset = params.offset ?? 0; |
| 12 | const limit = params.limit ?? total; |
| 13 | return { items: rows.slice(offset, offset + limit).map((n) => `${query}${n}`), total, limit, offset }; |
| 14 | }); |
| 15 | } |
| 16 | |
| 17 | describe("createInfiniteList", () => { |
| 18 | it("fetches only the first page, then one page per loadMore", async () => { |
| 19 | const fetch = server(120); |
| 20 | await createRoot(async (dispose) => { |
| 21 | const list = createInfiniteList(() => "r", fetch, { pageSize: 50 }); |
| 22 | await flush(); |
| 23 | expect(fetch).toHaveBeenCalledTimes(1); |
| 24 | expect(fetch).toHaveBeenLastCalledWith("r", { limit: 50, offset: 0 }); |
| 25 | expect(list.items()).toHaveLength(50); |
| 26 | expect(list.total()).toBe(120); |
| 27 | expect(list.hasMore()).toBe(true); |
| 28 | |
| 29 | list.loadMore(); |
| 30 | list.loadMore(); // ignored while the first is in flight |
| 31 | await flush(); |
| 32 | expect(fetch).toHaveBeenCalledTimes(2); |
| 33 | expect(fetch).toHaveBeenLastCalledWith("r", { limit: 50, offset: 50 }); |
| 34 | |
| 35 | list.loadMore(); |
| 36 | await flush(); |
| 37 | expect(list.items()).toHaveLength(120); |
| 38 | expect(list.hasMore()).toBe(false); |
| 39 | list.loadMore(); |
| 40 | await flush(); |
| 41 | expect(fetch).toHaveBeenCalledTimes(3); |
| 42 | dispose(); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | it("starts over from offset 0 when the query changes and drops a stale reply", async () => { |
| 47 | let release: (() => void) | undefined; |
| 48 | const fetch = vi.fn(async (query: string, params: PageParams): Promise<Page<string>> => { |
| 49 | if (query === "slow") await new Promise<void>((resolve) => (release = resolve)); |
| 50 | return { items: [`${query}-${params.offset}`], total: 1, limit: params.limit ?? 50, offset: params.offset ?? 0 }; |
| 51 | }); |
| 52 | await createRoot(async (dispose) => { |
| 53 | const [query, setQuery] = createSignal("slow"); |
| 54 | const list = createInfiniteList(query, fetch); |
| 55 | await flush(); |
| 56 | setQuery("fast"); |
| 57 | await flush(); |
| 58 | release?.(); |
| 59 | await flush(); |
| 60 | expect(list.items()).toEqual(["fast-0"]); |
| 61 | dispose(); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | it("fetches nothing until the source is ready", async () => { |
| 66 | const fetch = server(5); |
| 67 | await createRoot(async (dispose) => { |
| 68 | const [ready, setReady] = createSignal<string | null>(null); |
| 69 | const list = createInfiniteList(ready, fetch); |
| 70 | await flush(); |
| 71 | expect(fetch).not.toHaveBeenCalled(); |
| 72 | expect(list.initialLoading()).toBe(false); |
| 73 | setReady("x"); |
| 74 | await flush(); |
| 75 | expect(list.items()).toHaveLength(5); |
| 76 | dispose(); |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | it("ends the list on a short page even if the total disagrees", async () => { |
| 81 | const fetch = vi.fn(async (_: string, params: PageParams): Promise<Page<number>> => ({ |
| 82 | items: [1, 2], |
| 83 | total: 999, |
| 84 | limit: params.limit ?? 50, |
| 85 | offset: params.offset ?? 0, |
| 86 | })); |
| 87 | await createRoot(async (dispose) => { |
| 88 | const list = createInfiniteList(() => "q", fetch, { pageSize: 50 }); |
| 89 | await flush(); |
| 90 | expect(list.total()).toBe(2); |
| 91 | expect(list.hasMore()).toBe(false); |
| 92 | dispose(); |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | it("refresh refetches the loaded rows in one request and keeps them", async () => { |
| 97 | const fetch = server(120); |
| 98 | await createRoot(async (dispose) => { |
| 99 | const list = createInfiniteList(() => "r", fetch, { pageSize: 50 }); |
| 100 | await flush(); |
| 101 | list.loadMore(); |
| 102 | await flush(); |
| 103 | expect(list.items()).toHaveLength(100); |
| 104 | await list.refresh(); |
| 105 | expect(fetch).toHaveBeenLastCalledWith("r", { limit: 100, offset: 0 }); |
| 106 | expect(list.items()).toHaveLength(100); |
| 107 | expect(list.hasMore()).toBe(true); |
| 108 | dispose(); |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | it("restores the loaded row count for a query from session storage", async () => { |
| 113 | sessionStorage.clear(); |
| 114 | const fetch = server(120); |
| 115 | await createRoot(async (dispose) => { |
| 116 | const list = createInfiniteList(() => "r", fetch, { pageSize: 50, restoreKey: "t" }); |
| 117 | await flush(); |
| 118 | list.loadMore(); |
| 119 | await flush(); |
| 120 | dispose(); |
| 121 | }); |
| 122 | fetch.mockClear(); |
| 123 | await createRoot(async (dispose) => { |
| 124 | const list = createInfiniteList(() => "r", fetch, { pageSize: 50, restoreKey: "t" }); |
| 125 | await flush(); |
| 126 | expect(fetch).toHaveBeenCalledTimes(1); |
| 127 | expect(fetch).toHaveBeenLastCalledWith("r", { limit: 100, offset: 0 }); |
| 128 | expect(list.items()).toHaveLength(100); |
| 129 | dispose(); |
| 130 | }); |
| 131 | }); |
| 132 | }); |