@hyper-fetch/core 9.5.1 → 9.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,388 @@
1
+ /* eslint-disable max-params */
2
+ import type { PaginationConfig, RequestInstance } from "@hyper-fetch/core";
3
+ import {
4
+ Client,
5
+ getCachedPages,
6
+ getHasNextPage,
7
+ getHasPreviousPage,
8
+ getInitialPageParam,
9
+ getNextPageParam,
10
+ getPageItems,
11
+ getPageRequest,
12
+ getPageTotal,
13
+ getPaginationMode,
14
+ getPreviousPageParam,
15
+ } from "@hyper-fetch/core";
16
+
17
+ describe("Infinite [ Utils ]", () => {
18
+ const client = new Client({ url: "http://localhost:3000" });
19
+ const request = client.createRequest<{ response: any; queryParams: any }>()({
20
+ endpoint: "/posts",
21
+ }) as RequestInstance;
22
+
23
+ // Setting a custom key flags the request it was called on, so those tests cannot share one
24
+ const createRequest = () =>
25
+ client.createRequest<{ response: any; queryParams: any }>()({ endpoint: "/posts" }) as RequestInstance;
26
+
27
+ const context = (pagination: PaginationConfig, data: any, param: unknown, target: RequestInstance = request) => ({
28
+ request: target,
29
+ pagination,
30
+ data,
31
+ param,
32
+ pages: [data],
33
+ });
34
+
35
+ describe("getPaginationMode", () => {
36
+ it("should recognize every mode", () => {
37
+ expect(getPaginationMode({ page: "page" })).toBe("page");
38
+ expect(getPaginationMode({ offset: "offset", limit: "limit" })).toBe("offset");
39
+ expect(getPaginationMode({ cursor: "cursor", next: "next" })).toBe("cursor");
40
+ });
41
+
42
+ it("should explain a missing or incomplete pagination", () => {
43
+ expect(() => getPaginationMode(undefined as any)).toThrow('"pagination" option');
44
+ expect(() => getPaginationMode({})).toThrow('"pagination" option');
45
+ expect(() => getPaginationMode({ cursor: "cursor" })).toThrow('"next"');
46
+ });
47
+ });
48
+
49
+ describe("getInitialPageParam", () => {
50
+ it("should default to the start of the list", () => {
51
+ expect(getInitialPageParam(request, { page: "page" })).toBe(1);
52
+ expect(getInitialPageParam(request, { offset: "offset" })).toBe(0);
53
+ expect(getInitialPageParam(request, { cursor: "cursor", next: "next" })).toBeUndefined();
54
+ });
55
+
56
+ it("should start from the value of the request", () => {
57
+ expect(getInitialPageParam(request.setQueryParams({ page: 5 }), { page: "page" })).toBe(5);
58
+ expect(getInitialPageParam(request.setQueryParams({ page: 0 }), { page: "page" })).toBe(0);
59
+ expect(getInitialPageParam(request.setQueryParams({ page: "7" }), { page: "page" })).toBe(7);
60
+ expect(getInitialPageParam(request.setQueryParams({ offset: 40 }), { offset: "offset" })).toBe(40);
61
+ expect(getInitialPageParam(request.setQueryParams({ c: "abc" }), { cursor: "c", next: "next" })).toBe("abc");
62
+ });
63
+
64
+ it("should treat an empty cursor as the start", () => {
65
+ expect(getInitialPageParam(request.setQueryParams({ c: "" }), { cursor: "c", next: "next" })).toBeUndefined();
66
+ });
67
+ });
68
+
69
+ describe("getPageRequest", () => {
70
+ it("should set the paging field and keep the other query params", () => {
71
+ const page = getPageRequest(request.setQueryParams({ search: "a" }), { page: "page" }, 2);
72
+
73
+ expect(page.queryParams).toEqual({ search: "a", page: 2 });
74
+ });
75
+
76
+ it("should give every page its own generated keys", () => {
77
+ const first = getPageRequest(request, { page: "page" }, 1);
78
+ const second = getPageRequest(request, { page: "page" }, 2);
79
+
80
+ expect(first.cacheKey).not.toBe(second.cacheKey);
81
+ expect(first.cacheKey).toBe(request.setQueryParams({ page: 1 }).cacheKey);
82
+ });
83
+
84
+ it("should return the request itself for the first cursor page", () => {
85
+ // oxlint-disable-next-line unicorn/no-useless-undefined
86
+ expect(getPageRequest(request, { cursor: "cursor", next: "next" }, undefined)).toBe(request);
87
+ });
88
+
89
+ it("should page through URL params", () => {
90
+ const target = client.createRequest<{ response: any }>()({ endpoint: "/posts/:page" }) as RequestInstance;
91
+
92
+ expect(getPageRequest(target, { page: "page", in: "params" }, 3).endpoint).toBe("/posts/3");
93
+ });
94
+
95
+ it("should separate the keys of pages sharing a custom cache key", () => {
96
+ const target = createRequest().setCacheKey("posts");
97
+
98
+ const first = getPageRequest(target, { page: "page" }, 1);
99
+ const second = getPageRequest(target, { page: "page" }, 2);
100
+
101
+ expect(first.cacheKey).not.toBe(second.cacheKey);
102
+ expect(first.cacheKey.startsWith("posts")).toBe(true);
103
+ });
104
+
105
+ it("should separate the keys of pages inside the payload", () => {
106
+ const target = client.createRequest<{ response: any; payload: any }>()({
107
+ endpoint: "/search",
108
+ method: "POST",
109
+ }) as RequestInstance;
110
+
111
+ const first = getPageRequest(target, { page: "page", in: "payload" }, 1);
112
+ const second = getPageRequest(target, { page: "page", in: "payload" }, 2);
113
+
114
+ expect(first.payload).toEqual({ page: 1 });
115
+ expect(first.cacheKey).not.toBe(second.cacheKey);
116
+ expect(first.queryKey).not.toBe(second.queryKey);
117
+ });
118
+ });
119
+
120
+ describe("getPageItems and getPageTotal", () => {
121
+ it("should read an array response", () => {
122
+ expect(getPageItems(context({ page: "page" }, [1, 2], 1))).toEqual([1, 2]);
123
+ });
124
+
125
+ it("should read a field and a function", () => {
126
+ expect(getPageItems(context({ page: "page", items: "list" }, { list: [1] }, 1))).toEqual([1]);
127
+ expect(getPageItems(context({ page: "page", items: ({ data }) => data.list }, { list: [2] }, 1))).toEqual([2]);
128
+ });
129
+
130
+ it("should not fail on data that is not a list", () => {
131
+ expect(getPageItems(context({ page: "page" }, { nope: true }, 1))).toEqual([]);
132
+ expect(getPageItems(context({ page: "page", items: "list" }, null, 1))).toEqual([]);
133
+ });
134
+
135
+ it("should read the total only when declared", () => {
136
+ expect(getPageTotal(context({ page: "page" }, { total: 3 }, 1))).toBeNull();
137
+ expect(getPageTotal(context({ page: "page", total: "total" }, { total: 3 }, 1))).toBe(3);
138
+ expect(getPageTotal(context({ page: "page", total: () => 8 }, {}, 1))).toBe(8);
139
+ expect(getPageTotal(context({ page: "page", total: "total" }, { total: "many" }, 1))).toBeNull();
140
+ });
141
+ });
142
+
143
+ describe("next page", () => {
144
+ const limited = request.setQueryParams({ limit: 2 });
145
+
146
+ it("should increase page numbers", () => {
147
+ expect(getNextPageParam(context({ page: "page" }, [1], 4))).toBe(5);
148
+ });
149
+
150
+ it("should move offsets by the limit, or by the items without one", () => {
151
+ expect(getNextPageParam(context({ offset: "o", limit: "limit" }, [1], 4, limited))).toBe(6);
152
+ expect(getNextPageParam(context({ offset: "o" }, [1, 2, 3], 4))).toBe(7);
153
+ });
154
+
155
+ it.each([null, undefined, ""])("should end a cursor list on %s", (cursor) => {
156
+ const pagination = { cursor: "c", next: "next" };
157
+
158
+ expect(getNextPageParam(context(pagination, { next: cursor }, "a"))).toBeUndefined();
159
+ expect(getHasNextPage(context(pagination, { next: cursor }, "a"))).toBe(false);
160
+ });
161
+
162
+ it("should keep a cursor of zero", () => {
163
+ expect(getHasNextPage(context({ cursor: "c", next: "next" }, { next: 0 }, "a"))).toBe(true);
164
+ });
165
+
166
+ it.each<[string, PaginationConfig, any, number, RequestInstance, boolean]>([
167
+ ["an empty page", { page: "page" }, [], 1, request, false],
168
+ ["a page without a known size", { page: "page" }, [1], 1, request, true],
169
+ ["a full page", { page: "page", limit: "limit" }, [1, 2], 1, limited, true],
170
+ ["a short page", { page: "page", limit: "limit" }, [1], 1, limited, false],
171
+ ["pages below the total", { page: "page", limit: "limit", total: () => 5 }, [1, 2], 2, limited, true],
172
+ ["the total reached", { page: "page", limit: "limit", total: () => 4 }, [1, 2], 2, limited, false],
173
+ ["an offset below the total", { offset: "o", limit: "limit", total: () => 5 }, [1, 2], 2, limited, true],
174
+ ["an offset at the total", { offset: "o", limit: "limit", total: () => 4 }, [1, 2], 2, limited, false],
175
+ ])("should detect the end for %s", (_name, pagination, data, param, target, expected) => {
176
+ expect(getHasNextPage(context(pagination, data, param, target))).toBe(expected);
177
+ });
178
+
179
+ it("should count the total from zero for lists numbered from zero", () => {
180
+ const zero = request.setQueryParams({ page: 0, limit: 2 });
181
+ const pagination = { page: "page", limit: "limit", total: () => 4 };
182
+
183
+ expect(getHasNextPage(context(pagination, [1, 2], 0, zero))).toBe(true);
184
+ expect(getHasNextPage(context(pagination, [1, 2], 1, zero))).toBe(false);
185
+ });
186
+
187
+ it("should prefer the hasNext override", () => {
188
+ expect(getHasNextPage(context({ page: "page", hasNext: () => true }, [], 1))).toBe(true);
189
+ expect(getHasNextPage(context({ page: "page", hasNext: () => false }, [1], 1))).toBe(false);
190
+ });
191
+
192
+ it("should not let hasNext continue a list without a next cursor", () => {
193
+ const pagination = { cursor: "c", next: "next", hasNext: () => true };
194
+
195
+ expect(getHasNextPage(context(pagination, { next: null }, "a"))).toBe(false);
196
+ });
197
+ });
198
+
199
+ describe("previous page", () => {
200
+ it("should stop at the first page", () => {
201
+ expect(getPreviousPageParam(context({ page: "page" }, [], 3))).toBe(2);
202
+ expect(getPreviousPageParam(context({ page: "page" }, [], 1))).toBeUndefined();
203
+ expect(getHasPreviousPage(context({ page: "page" }, [], 1))).toBe(false);
204
+ });
205
+
206
+ it("should reach page zero only in lists numbered from zero", () => {
207
+ const zero = request.setQueryParams({ page: 0 });
208
+
209
+ expect(getPreviousPageParam(context({ page: "page" }, [], 1, zero))).toBe(0);
210
+ expect(getPreviousPageParam(context({ page: "page" }, [], 0, zero))).toBeUndefined();
211
+ });
212
+
213
+ it("should not move offsets below zero", () => {
214
+ const limited = request.setQueryParams({ limit: 10 });
215
+
216
+ expect(getPreviousPageParam(context({ offset: "o", limit: "limit" }, [], 4, limited))).toBe(0);
217
+ expect(getPreviousPageParam(context({ offset: "o", limit: "limit" }, [], 0, limited))).toBeUndefined();
218
+ });
219
+
220
+ it("should read the previous cursor", () => {
221
+ const pagination = { cursor: "c", next: "next", previous: "before" };
222
+
223
+ expect(getPreviousPageParam(context(pagination, { before: "a" }, "b"))).toBe("a");
224
+ expect(getHasPreviousPage(context(pagination, { before: null }, "b"))).toBe(false);
225
+ expect(getHasPreviousPage(context({ cursor: "c", next: "next" }, {}, "b"))).toBe(false);
226
+ });
227
+
228
+ it("should prefer the hasPrevious override", () => {
229
+ expect(getHasPreviousPage(context({ page: "page", hasPrevious: () => false }, [], 3))).toBe(false);
230
+ });
231
+ });
232
+
233
+ describe("page size", () => {
234
+ it.each([0, -2, "many", null])("should ignore a limit of %s", (limit) => {
235
+ const target = request.setQueryParams({ limit });
236
+
237
+ expect(getNextPageParam(context({ offset: "o", limit: "limit" }, [1, 2, 3], 4, target))).toBe(7);
238
+ expect(getHasNextPage(context({ page: "page", limit: "limit" }, [1], 1, target))).toBe(true);
239
+ });
240
+
241
+ it("should go back by the size of the page without a limit", () => {
242
+ expect(getPreviousPageParam(context({ offset: "o" }, [1, 2], 6))).toBe(4);
243
+ });
244
+
245
+ it("should compare the total with the size of the page without a limit", () => {
246
+ const pagination = { page: "page", total: () => 4 };
247
+
248
+ expect(getHasNextPage(context(pagination, [1, 2], 1))).toBe(true);
249
+ expect(getHasNextPage(context(pagination, [1, 2], 2))).toBe(false);
250
+ });
251
+
252
+ it("should read the limit from the place of the paging field", () => {
253
+ const target = client.createRequest<{ response: any }>()({ endpoint: "/m/:page/:limit" }) as RequestInstance;
254
+ const page = target.setParams({ page: 1, limit: 2 } as any) as RequestInstance;
255
+
256
+ expect(getHasNextPage(context({ page: "page", limit: "limit", in: "params" }, [1], 1, page))).toBe(false);
257
+ });
258
+ });
259
+
260
+ describe("paging values", () => {
261
+ it.each([
262
+ [{ page: "page" }, { page: "x" }, 1],
263
+ [{ offset: "offset" }, { offset: "x" }, 0],
264
+ ])("should fall back to the start for an invalid value", (pagination, queryParams, expected) => {
265
+ expect(getInitialPageParam(request.setQueryParams(queryParams), pagination)).toBe(expected);
266
+ });
267
+
268
+ it("should start from the URL params and the payload", () => {
269
+ const inParams = client.createRequest<{ response: any }>()({ endpoint: "/m/:page" }) as RequestInstance;
270
+ const inPayload = client.createRequest<{ response: any; payload: any }>()({
271
+ endpoint: "/m",
272
+ method: "POST",
273
+ }) as RequestInstance;
274
+
275
+ expect(
276
+ getInitialPageParam(inParams.setParams({ page: 3 } as any) as RequestInstance, { page: "page", in: "params" }),
277
+ ).toBe(3);
278
+ expect(
279
+ getInitialPageParam(inPayload.setPayload({ page: 8 }) as RequestInstance, { page: "page", in: "payload" }),
280
+ ).toBe(8);
281
+ });
282
+
283
+ it("should prefer the cursor when modes are mixed at runtime", () => {
284
+ expect(getPaginationMode({ cursor: "c", next: "n", offset: "o", page: "p" })).toBe("cursor");
285
+ expect(getPaginationMode({ offset: "o", page: "p" })).toBe("offset");
286
+ });
287
+
288
+ it("should read cursors from functions", () => {
289
+ const pagination = { cursor: "c", next: () => "after", previous: () => "before" };
290
+
291
+ expect(getNextPageParam(context(pagination, {}, "x"))).toBe("after");
292
+ expect(getPreviousPageParam(context(pagination, {}, "x"))).toBe("before");
293
+ });
294
+
295
+ it("should pass the page details to callbacks", () => {
296
+ const next = vi.fn(() => "after");
297
+
298
+ getNextPageParam({ ...context({ cursor: "c", next }, { a: 1 }, "x"), pages: [{ b: 2 }] });
299
+
300
+ expect(next).toHaveBeenCalledWith({ data: { a: 1 }, param: "x", pages: [{ b: 2 }] });
301
+ });
302
+
303
+ it("should keep the query params of the request untouched", () => {
304
+ const target = request.setQueryParams({ search: "a" });
305
+
306
+ getPageRequest(target, { page: "page" }, 2);
307
+
308
+ expect(target.queryParams).toEqual({ search: "a" });
309
+ });
310
+
311
+ it("should give custom query keys of the pages their own value", () => {
312
+ const target = createRequest().setQueryKey("posts");
313
+
314
+ const first = getPageRequest(target, { page: "page" }, 1);
315
+ const second = getPageRequest(target, { page: "page" }, 2);
316
+
317
+ expect(first.queryKey).not.toBe(second.queryKey);
318
+ });
319
+
320
+ it("should not touch the generated keys of a custom query key sibling", () => {
321
+ const target = createRequest().setCacheKey("posts");
322
+
323
+ expect(getPageRequest(target, { page: "page" }, 1).queryKey).toBe(target.setQueryParams({ page: 1 }).queryKey);
324
+ });
325
+ });
326
+
327
+ describe("getCachedPages", () => {
328
+ const stored = (pages: Record<string, any>) => (pageRequest: RequestInstance) => {
329
+ const value = pages[String((pageRequest.queryParams as any)?.page ?? (pageRequest.queryParams as any)?.c)];
330
+ return value ? { data: value } : undefined;
331
+ };
332
+
333
+ it("should return nothing without the first page", () => {
334
+ expect(getCachedPages({ request, pagination: { page: "page" }, read: () => null })).toEqual([]);
335
+ });
336
+
337
+ it("should walk forwards until a page is missing", () => {
338
+ const pages = getCachedPages({
339
+ request,
340
+ pagination: { page: "page" },
341
+ read: stored({ 1: [1], 2: [2], 4: [4] }),
342
+ });
343
+
344
+ expect(pages.map((page) => page.param)).toEqual([1, 2]);
345
+ });
346
+
347
+ it("should walk in both directions from the starting page", () => {
348
+ const pages = getCachedPages({
349
+ request: request.setQueryParams({ page: 3 }),
350
+ pagination: { page: "page" },
351
+ read: stored({ 2: [2], 3: [3], 4: [4] }),
352
+ });
353
+
354
+ expect(pages.map((page) => page.param)).toEqual([2, 3, 4]);
355
+ });
356
+
357
+ it("should stop at the end of the list", () => {
358
+ const pages = getCachedPages({
359
+ request,
360
+ pagination: { page: "page" },
361
+ read: stored({ 1: [1], 2: [], 3: [3] }),
362
+ });
363
+
364
+ expect(pages.map((page) => page.param)).toEqual([1, 2]);
365
+ });
366
+
367
+ it("should respect maxPages", () => {
368
+ const pages = getCachedPages({
369
+ request,
370
+ pagination: { page: "page" },
371
+ maxPages: 2,
372
+ read: stored({ 1: [1], 2: [2], 3: [3] }),
373
+ });
374
+
375
+ expect(pages).toHaveLength(2);
376
+ });
377
+
378
+ it("should not loop over cursors pointing back at each other", () => {
379
+ const pages = getCachedPages({
380
+ request: request.setQueryParams({ c: "a" }),
381
+ pagination: { cursor: "c", next: "next", previous: "before" },
382
+ read: stored({ a: { next: "b", before: "b" }, b: { next: "a", before: "a" } }),
383
+ });
384
+
385
+ expect(pages.map((page) => page.param)).toEqual(["a", "b"]);
386
+ });
387
+ });
388
+ });