@hyper-fetch/react 9.6.0 → 9.6.2
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.
- package/__tests__/features/helpers/use-request-events/use-request-events.async-mapper.spec.ts +243 -0
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +1 -1
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.keys.spec.ts +294 -0
- package/__tests__/features/helpers/utils.spec.ts +127 -0
- package/__tests__/features/provider/provider.config.spec.tsx +172 -0
- package/__tests__/features/use-app-manager/use-app-manager.events.spec.tsx +497 -0
- package/__tests__/features/use-cache/use-cache.state.spec.tsx +326 -0
- package/__tests__/features/use-emitter/use-emitter.lifecycle.spec.tsx +417 -0
- package/__tests__/features/use-fetch/use-fetch.bounce-timers.spec.ts +199 -0
- package/__tests__/features/use-fetch/use-fetch.cache-events.spec.ts +212 -0
- package/__tests__/features/use-fetch/use-fetch.callbacks.spec.tsx +359 -0
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +6 -3
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +72 -80
- package/__tests__/features/use-fetch/use-fetch.hydration.spec.tsx +126 -0
- package/__tests__/features/use-fetch/use-fetch.infinite.spec.ts +146 -0
- package/__tests__/features/use-fetch/use-fetch.initial-state.spec.ts +340 -0
- package/__tests__/features/use-fetch/use-fetch.keys.spec.tsx +559 -0
- package/__tests__/features/use-fetch/use-fetch.lifecycle.spec.ts +348 -0
- package/__tests__/features/use-fetch/use-fetch.mounting.spec.tsx +411 -0
- package/__tests__/features/use-fetch/use-fetch.pagination.spec.ts +177 -0
- package/__tests__/features/use-fetch/use-fetch.refresh-interval.spec.ts +355 -0
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +36 -13
- package/__tests__/features/use-fetch/use-fetch.response-order.spec.ts +105 -0
- package/__tests__/features/use-fetch/use-fetch.retry.spec.ts +235 -0
- package/__tests__/features/use-fetch/use-fetch.return-types.spec.ts +186 -0
- package/__tests__/features/use-fetch/use-fetch.scope.spec.ts +333 -0
- package/__tests__/features/use-fetch/use-fetch.ssr.spec.tsx +195 -0
- package/__tests__/features/use-fetch/use-fetch.suspense-edge.spec.tsx +391 -0
- package/__tests__/features/use-fetch/use-fetch.throwing-callbacks.spec.tsx +112 -0
- package/__tests__/features/use-fetch/use-fetch.tracking.spec.tsx +364 -0
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +11 -2
- package/__tests__/features/use-fetch/use-fetch.window-events.spec.ts +300 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.parity.spec.tsx +1027 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.resubscribe.spec.tsx +267 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.typescript.spec.ts +64 -1
- package/__tests__/features/use-is-fetching/use-is-fetching.base.spec.tsx +19 -9
- package/__tests__/features/use-is-fetching/use-is-fetching.lifecycle.spec.tsx +301 -0
- package/__tests__/features/use-listener/use-listener.lifecycle.spec.tsx +507 -0
- package/__tests__/features/use-listener/use-listener.ssr.spec.tsx +89 -0
- package/__tests__/features/use-queue/use-queue.controls.spec.tsx +593 -0
- package/__tests__/features/use-queue/use-queue.list.spec.tsx +560 -0
- package/__tests__/features/use-queue/use-queue.progress.spec.tsx +650 -0
- package/__tests__/features/use-submit/use-submit.abort.spec.tsx +270 -0
- package/__tests__/features/use-submit/use-submit.actions.spec.tsx +446 -0
- package/__tests__/features/use-submit/use-submit.arguments.spec.tsx +732 -0
- package/__tests__/features/use-submit/use-submit.bounce.spec.tsx +644 -0
- package/__tests__/features/use-submit/use-submit.callbacks.spec.tsx +860 -0
- package/__tests__/features/use-submit/use-submit.chaining.spec.tsx +167 -0
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +2 -1
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +100 -124
- package/__tests__/features/use-submit/use-submit.double-submit.spec.tsx +552 -0
- package/__tests__/features/use-submit/use-submit.offline.spec.tsx +285 -0
- package/__tests__/features/use-submit/use-submit.optimistic-cache.spec.tsx +619 -0
- package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +8 -1
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +5 -1
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +51 -35
- package/__tests__/features/use-submit/use-submit.state.spec.tsx +1025 -0
- package/__tests__/features/use-submit/use-submit.submit-types.spec.ts +194 -0
- package/__tests__/features/use-submit/use-submit.submitting.spec.tsx +180 -0
- package/__tests__/features/use-submit/use-submit.unmount.spec.ts +2 -1
- package/__tests__/utils/gate.utils.ts +103 -0
- package/__tests__/utils/gated-client.utils.ts +158 -0
- package/__tests__/utils/helpers.utils.ts +19 -0
- package/__tests__/utils/use-queue.utils.ts +8 -4
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
- package/dist/helpers/use-request-events/use-request-events.types.d.ts +1 -1
- package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +8 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.hooks.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts +3 -0
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts +5 -3
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts +11 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts.map +1 -1
- package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -1
- package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
- package/dist/index.mjs +633 -347
- package/dist/index.mjs.map +1 -1
- package/dist/provider/provider.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +147 -32
- package/src/helpers/use-request-events/use-request-events.types.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +148 -101
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +26 -5
- package/src/hooks/use-cache/use-cache.hooks.ts +20 -5
- package/src/hooks/use-fetch/use-fetch.hooks.ts +194 -113
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.hooks.ts +55 -26
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.store.ts +35 -29
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.types.ts +5 -3
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts +25 -1
- package/src/hooks/use-listener/use-listener.hooks.ts +2 -1
- package/src/hooks/use-queue/use-queue.hooks.ts +85 -60
- package/src/hooks/use-submit/use-submit.hooks.ts +163 -53
- package/src/provider/provider.tsx +9 -1
- package/__tests__/features/use-fetch/use-fetch.pagination.ts +0 -36
- package/__tests__/features/use-fetch/use-fetch.retry.ts +0 -38
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { Client } from "@hyper-fetch/core";
|
|
2
|
+
import { act, render, renderHook, screen, waitFor } from "@testing-library/react";
|
|
3
|
+
import { useInfiniteFetch } from "hooks/use-infinite-fetch";
|
|
4
|
+
import React, { Activity, StrictMode } from "react";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
baseUrl,
|
|
8
|
+
createDeferred,
|
|
9
|
+
createListServer,
|
|
10
|
+
pageItems,
|
|
11
|
+
settleRequests,
|
|
12
|
+
} from "../../utils/use-infinite-fetch.utils";
|
|
13
|
+
|
|
14
|
+
const { startServer, stopServer, resetMocks, mockList, calls, resetCalls } = createListServer();
|
|
15
|
+
|
|
16
|
+
type Post = { id: number; version?: number };
|
|
17
|
+
|
|
18
|
+
const versions = (items: Post[]) => items.map((item) => item.version);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Effects of a mounted hook run again in StrictMode and when a hidden `Activity` is shown.
|
|
22
|
+
* The list must behave like it was mounted once: no repeated requests, and nothing missed while it was away.
|
|
23
|
+
*/
|
|
24
|
+
describe("useInfiniteFetch [ Resubscribe ]", () => {
|
|
25
|
+
let client: Client;
|
|
26
|
+
let version = 1;
|
|
27
|
+
|
|
28
|
+
const createPosts = () =>
|
|
29
|
+
client.createRequest<{ response: Post[]; queryParams: { page?: number } }>()({ endpoint: "/posts" });
|
|
30
|
+
|
|
31
|
+
const pagination = { page: "page" } as const;
|
|
32
|
+
|
|
33
|
+
/** Resolves once the stored page is older than its stale time of zero */
|
|
34
|
+
const waitForStalePage = async (request: ReturnType<typeof createPosts>) => {
|
|
35
|
+
const { cacheKey } = request.setQueryParams({ page: 1 });
|
|
36
|
+
await waitFor(() => expect(client.cache.get(cacheKey)?.responseTimestamp).toBeLessThan(Date.now()));
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const List = ({ request = createPosts() }: { request?: ReturnType<typeof createPosts> }) => {
|
|
40
|
+
const { items, fetchNextPage } = useInfiniteFetch(request, { pagination });
|
|
41
|
+
return (
|
|
42
|
+
<button type="button" onClick={() => fetchNextPage()}>
|
|
43
|
+
{`versions: ${versions(items).join(",")}`}
|
|
44
|
+
</button>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const App = ({ isVisible, request }: { isVisible: boolean; request?: ReturnType<typeof createPosts> }) => (
|
|
49
|
+
<Activity mode={isVisible ? "visible" : "hidden"}>
|
|
50
|
+
<List request={request} />
|
|
51
|
+
</Activity>
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const renderTwoPages = async (request?: ReturnType<typeof createPosts>) => {
|
|
55
|
+
const view = render(<App isVisible request={request} />);
|
|
56
|
+
await screen.findByText("versions: 1,1");
|
|
57
|
+
act(() => screen.getByRole("button").click());
|
|
58
|
+
await screen.findByText("versions: 1,1,1,1");
|
|
59
|
+
return view;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
beforeAll(() => startServer());
|
|
63
|
+
afterAll(() => stopServer());
|
|
64
|
+
|
|
65
|
+
beforeEach(() => {
|
|
66
|
+
client = new Client({ url: baseUrl });
|
|
67
|
+
version = 1;
|
|
68
|
+
resetCalls();
|
|
69
|
+
mockList("/posts", ({ query }) => pageItems(Number(query.page)).map((post) => ({ ...post, version })));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
afterEach(async () => {
|
|
73
|
+
await settleRequests(client);
|
|
74
|
+
resetMocks();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("hidden lists", () => {
|
|
78
|
+
it("should show a fresh list again without requests", async () => {
|
|
79
|
+
const { rerender } = await renderTwoPages();
|
|
80
|
+
const add = vi.spyOn(client.fetchDispatcher, "add");
|
|
81
|
+
|
|
82
|
+
rerender(<App isVisible={false} />);
|
|
83
|
+
rerender(<App isVisible />);
|
|
84
|
+
|
|
85
|
+
expect(screen.getByText("versions: 1,1,1,1")).toBeInTheDocument();
|
|
86
|
+
expect(add).not.toHaveBeenCalled();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should refetch the pages invalidated while the list was hidden", async () => {
|
|
90
|
+
const { rerender } = await renderTwoPages();
|
|
91
|
+
|
|
92
|
+
rerender(<App isVisible={false} />);
|
|
93
|
+
version = 2;
|
|
94
|
+
act(() => client.cache.invalidate());
|
|
95
|
+
const add = vi.spyOn(client.fetchDispatcher, "add");
|
|
96
|
+
|
|
97
|
+
rerender(<App isVisible />);
|
|
98
|
+
|
|
99
|
+
await screen.findByText("versions: 2,2,2,2");
|
|
100
|
+
expect(add).toHaveBeenCalledTimes(2);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should refetch the pages that became stale while the list was hidden", async () => {
|
|
104
|
+
const request = createPosts().setStaleTime(0);
|
|
105
|
+
const { rerender } = await renderTwoPages(request);
|
|
106
|
+
|
|
107
|
+
rerender(<App isVisible={false} request={request} />);
|
|
108
|
+
await waitForStalePage(request);
|
|
109
|
+
version = 2;
|
|
110
|
+
const add = vi.spyOn(client.fetchDispatcher, "add");
|
|
111
|
+
|
|
112
|
+
rerender(<App isVisible request={request} />);
|
|
113
|
+
|
|
114
|
+
await screen.findByText("versions: 2,2,2,2");
|
|
115
|
+
expect(add).toHaveBeenCalledTimes(2);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should show a page that arrived while the list was hidden", async () => {
|
|
119
|
+
const gate = createDeferred();
|
|
120
|
+
mockList("/posts", async ({ query }) => {
|
|
121
|
+
if (query.page === "2") {
|
|
122
|
+
await gate.promise;
|
|
123
|
+
}
|
|
124
|
+
return pageItems(Number(query.page)).map((post) => ({ ...post, version }));
|
|
125
|
+
});
|
|
126
|
+
const { rerender } = render(<App isVisible />);
|
|
127
|
+
await screen.findByText("versions: 1,1");
|
|
128
|
+
act(() => screen.getByRole("button").click());
|
|
129
|
+
await waitFor(() => expect(calls).toHaveLength(2));
|
|
130
|
+
|
|
131
|
+
rerender(<App isVisible={false} />);
|
|
132
|
+
gate.resolve();
|
|
133
|
+
await waitFor(() => expect(client.cache.keys()).toHaveLength(2));
|
|
134
|
+
await waitFor(() => expect(client.fetchDispatcher.getAllRunningRequests()).toHaveLength(0));
|
|
135
|
+
|
|
136
|
+
rerender(<App isVisible />);
|
|
137
|
+
|
|
138
|
+
await screen.findByText("versions: 1,1,1,1");
|
|
139
|
+
expect(calls).toHaveLength(2);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("should show a cache change made while the list was hidden", async () => {
|
|
143
|
+
const { rerender } = await renderTwoPages();
|
|
144
|
+
|
|
145
|
+
rerender(<App isVisible={false} />);
|
|
146
|
+
act(() => {
|
|
147
|
+
client.cache.update(createPosts().setQueryParams({ page: 2 }), (previous) => ({
|
|
148
|
+
data: previous?.data?.map((post) => ({ ...post, version: 7 })) ?? null,
|
|
149
|
+
}));
|
|
150
|
+
});
|
|
151
|
+
rerender(<App isVisible />);
|
|
152
|
+
|
|
153
|
+
await screen.findByText("versions: 1,1,7,7");
|
|
154
|
+
expect(calls).toHaveLength(2);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("StrictMode", () => {
|
|
159
|
+
const renderLoaded = async () => {
|
|
160
|
+
const first = renderHook(() => useInfiniteFetch(createPosts(), { pagination, dependencyTracking: false }));
|
|
161
|
+
await waitFor(() => expect(first.result.current.items).toHaveLength(2));
|
|
162
|
+
await act(() => first.result.current.fetchNextPage());
|
|
163
|
+
first.unmount();
|
|
164
|
+
return vi.spyOn(client.fetchDispatcher, "add");
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
it("should restore fresh pages without requests", async () => {
|
|
168
|
+
const add = await renderLoaded();
|
|
169
|
+
|
|
170
|
+
const { result } = renderHook(() => useInfiniteFetch(createPosts(), { pagination, dependencyTracking: false }), {
|
|
171
|
+
wrapper: StrictMode,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(result.current.pageParams).toEqual([1, 2]);
|
|
175
|
+
expect(result.current.isFetching).toBe(false);
|
|
176
|
+
expect(add).not.toHaveBeenCalled();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("should refetch the restored pages once with the revalidate option", async () => {
|
|
180
|
+
const add = await renderLoaded();
|
|
181
|
+
version = 2;
|
|
182
|
+
|
|
183
|
+
const { result } = renderHook(
|
|
184
|
+
() => useInfiniteFetch(createPosts(), { pagination, revalidate: true, dependencyTracking: false }),
|
|
185
|
+
{ wrapper: StrictMode },
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
await waitFor(() => expect(versions(result.current.items)).toEqual([2, 2, 2, 2]));
|
|
189
|
+
expect(result.current.isFetching).toBe(false);
|
|
190
|
+
expect(add).toHaveBeenCalledTimes(2);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("should refetch once for every change of the dependencies", async () => {
|
|
194
|
+
const add = await renderLoaded();
|
|
195
|
+
|
|
196
|
+
const { result, rerender } = renderHook(
|
|
197
|
+
({ value }) =>
|
|
198
|
+
useInfiniteFetch(createPosts(), { pagination, dependencies: [value], dependencyTracking: false }),
|
|
199
|
+
{ wrapper: StrictMode, initialProps: { value: 1 } },
|
|
200
|
+
);
|
|
201
|
+
expect(add).not.toHaveBeenCalled();
|
|
202
|
+
|
|
203
|
+
rerender({ value: 1 });
|
|
204
|
+
expect(add).not.toHaveBeenCalled();
|
|
205
|
+
|
|
206
|
+
version = 2;
|
|
207
|
+
rerender({ value: 2 });
|
|
208
|
+
|
|
209
|
+
await waitFor(() => expect(versions(result.current.items)).toEqual([2, 2, 2, 2]));
|
|
210
|
+
expect(add).toHaveBeenCalledTimes(2);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe("hooks joining a list on screen", () => {
|
|
215
|
+
it("should refetch a stale list when another hook mounts on it", async () => {
|
|
216
|
+
const stale = createPosts().setStaleTime(0);
|
|
217
|
+
const first = renderHook(() => useInfiniteFetch(stale, { pagination, dependencyTracking: false }));
|
|
218
|
+
await waitFor(() => expect(first.result.current.items).toHaveLength(2));
|
|
219
|
+
await waitForStalePage(stale);
|
|
220
|
+
version = 2;
|
|
221
|
+
const add = vi.spyOn(client.fetchDispatcher, "add");
|
|
222
|
+
|
|
223
|
+
const second = renderHook(() => useInfiniteFetch(stale, { pagination, dependencyTracking: false }));
|
|
224
|
+
|
|
225
|
+
expect(versions(second.result.current.items)).toEqual([1, 1]);
|
|
226
|
+
expect(second.result.current.loading).toBe(false);
|
|
227
|
+
await waitFor(() => expect(versions(second.result.current.items)).toEqual([2, 2]));
|
|
228
|
+
expect(versions(first.result.current.items)).toEqual([2, 2]);
|
|
229
|
+
expect(add).toHaveBeenCalledTimes(1);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("should not refetch a fresh list when another hook mounts on it", async () => {
|
|
233
|
+
const first = renderHook(() => useInfiniteFetch(createPosts(), { pagination, dependencyTracking: false }));
|
|
234
|
+
await waitFor(() => expect(first.result.current.items).toHaveLength(2));
|
|
235
|
+
const add = vi.spyOn(client.fetchDispatcher, "add");
|
|
236
|
+
|
|
237
|
+
const second = renderHook(() => useInfiniteFetch(createPosts(), { pagination, dependencyTracking: false }));
|
|
238
|
+
|
|
239
|
+
expect(second.result.current.items).toHaveLength(2);
|
|
240
|
+
expect(second.result.current.isFetching).toBe(false);
|
|
241
|
+
expect(add).not.toHaveBeenCalled();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("should not refetch a fresh list without cache when another hook mounts on it", async () => {
|
|
245
|
+
const uncached = createPosts().setCache(false);
|
|
246
|
+
const first = renderHook(() => useInfiniteFetch(uncached, { pagination, dependencyTracking: false }));
|
|
247
|
+
await waitFor(() => expect(first.result.current.items).toHaveLength(2));
|
|
248
|
+
const add = vi.spyOn(client.fetchDispatcher, "add");
|
|
249
|
+
|
|
250
|
+
const second = renderHook(() => useInfiniteFetch(uncached, { pagination, dependencyTracking: false }));
|
|
251
|
+
|
|
252
|
+
expect(second.result.current.items).toHaveLength(2);
|
|
253
|
+
expect(second.result.current.isFetching).toBe(false);
|
|
254
|
+
expect(add).not.toHaveBeenCalled();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("should join the fetch of a list that is still loading", async () => {
|
|
258
|
+
const first = renderHook(() => useInfiniteFetch(createPosts(), { pagination, dependencyTracking: false }));
|
|
259
|
+
const second = renderHook(() => useInfiniteFetch(createPosts(), { pagination, dependencyTracking: false }));
|
|
260
|
+
|
|
261
|
+
await waitFor(() => expect(second.result.current.items).toHaveLength(2));
|
|
262
|
+
|
|
263
|
+
expect(first.result.current.items).toHaveLength(2);
|
|
264
|
+
expect(calls).toHaveLength(1);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Pagination } from "@hyper-fetch/core";
|
|
1
|
+
import type { ExtractErrorType, Pagination } from "@hyper-fetch/core";
|
|
2
2
|
import { Client, fetchInfinite } from "@hyper-fetch/core";
|
|
3
3
|
import { useInfiniteFetch } from "hooks/use-infinite-fetch";
|
|
4
4
|
|
|
@@ -93,6 +93,69 @@ describe("useInfiniteFetch [ TypeScript ]", () => {
|
|
|
93
93
|
expectTypeOf(result).resolves.toHaveProperty("items").toEqualTypeOf<Post[]>();
|
|
94
94
|
});
|
|
95
95
|
|
|
96
|
+
it("should type the pages, the paging values and the result of the last fetch", () => {
|
|
97
|
+
const getTyped = client.createRequest<{
|
|
98
|
+
response: { data: Post[]; nextCursor: string | null };
|
|
99
|
+
queryParams: { cursor?: string };
|
|
100
|
+
error: { code: number };
|
|
101
|
+
}>()({ endpoint: "/typed" });
|
|
102
|
+
const list = types(() =>
|
|
103
|
+
useInfiniteFetch(getTyped, { pagination: { cursor: "cursor", next: "nextCursor", items: "data" } }),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
expectTypeOf(list.pages).toEqualTypeOf<{ data: Post[]; nextCursor: string | null }[]>();
|
|
107
|
+
expectTypeOf(list.pageParams).toEqualTypeOf<unknown[]>();
|
|
108
|
+
expectTypeOf(list.error).toEqualTypeOf<ExtractErrorType<typeof getTyped> | null>();
|
|
109
|
+
expectTypeOf<{ code: number }>().toMatchTypeOf<NonNullable<typeof list.error>>();
|
|
110
|
+
expectTypeOf(list.hasNextPage).toEqualTypeOf<boolean>();
|
|
111
|
+
expectTypeOf(list.isFetchNextPageError).toEqualTypeOf<boolean>();
|
|
112
|
+
expectTypeOf(list.abort).toEqualTypeOf<() => void>();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("should type the responses of the actions and the page events", () => {
|
|
116
|
+
const list = types(() => useInfiniteFetch(getPosts, { pagination: { page: "page" } }));
|
|
117
|
+
|
|
118
|
+
expectTypeOf(list.fetchNextPage).parameters.toEqualTypeOf<[]>();
|
|
119
|
+
expectTypeOf<NonNullable<Awaited<ReturnType<typeof list.fetchNextPage>>>["data"]>().toEqualTypeOf<Post[] | null>();
|
|
120
|
+
expectTypeOf<Awaited<ReturnType<typeof list.fetchNextPage>>>().toBeNullable();
|
|
121
|
+
expectTypeOf(list.fetchPreviousPage).toEqualTypeOf(list.fetchNextPage);
|
|
122
|
+
expectTypeOf(list.refetch).toEqualTypeOf(list.fetchNextPage);
|
|
123
|
+
expectTypeOf(list.reset).toEqualTypeOf(list.fetchNextPage);
|
|
124
|
+
|
|
125
|
+
types(() => {
|
|
126
|
+
list.onSuccess(({ response, request, param }) => {
|
|
127
|
+
expectTypeOf(response.data).toEqualTypeOf<Post[] | null>();
|
|
128
|
+
expectTypeOf(request.endpoint).toEqualTypeOf<"/posts">();
|
|
129
|
+
expectTypeOf(request.queryParams).toEqualTypeOf(getPosts.queryParams);
|
|
130
|
+
expectTypeOf(param).toBeUnknown();
|
|
131
|
+
});
|
|
132
|
+
list.onError(({ response }) => {
|
|
133
|
+
expectTypeOf(response.success).toEqualTypeOf<boolean>();
|
|
134
|
+
});
|
|
135
|
+
list.onReset(() => null);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("should type the context of select and uniqueBy", () => {
|
|
140
|
+
types(() =>
|
|
141
|
+
useInfiniteFetch(getFeed, {
|
|
142
|
+
pagination: { cursor: "cursor", next: "nextCursor", items: "data" },
|
|
143
|
+
uniqueBy: ({ item }) => {
|
|
144
|
+
expectTypeOf(item).toEqualTypeOf<Post>();
|
|
145
|
+
return item.id;
|
|
146
|
+
},
|
|
147
|
+
select: ({ items, pages }) => {
|
|
148
|
+
expectTypeOf(items).toEqualTypeOf<Post[]>();
|
|
149
|
+
expectTypeOf(pages).toEqualTypeOf<
|
|
150
|
+
{ data: Post[]; nextCursor: string | null; count: number; label: string }[]
|
|
151
|
+
>();
|
|
152
|
+
return pages.length;
|
|
153
|
+
},
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
types(() => useInfiniteFetch(getPosts, { pagination: { page: "page" }, uniqueBy: "id" }));
|
|
157
|
+
});
|
|
158
|
+
|
|
96
159
|
it("should reject a wrong pagination", () => {
|
|
97
160
|
types(() => {
|
|
98
161
|
// @ts-expect-error pagination is required
|
|
@@ -12,6 +12,7 @@ describe("useIsFetching [ Base ]", () => {
|
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
afterEach(() => {
|
|
15
|
+
vi.useRealTimers();
|
|
15
16
|
resetMocks();
|
|
16
17
|
});
|
|
17
18
|
|
|
@@ -25,10 +26,13 @@ describe("useIsFetching [ Base ]", () => {
|
|
|
25
26
|
});
|
|
26
27
|
|
|
27
28
|
it("should count the running fetch requests", async () => {
|
|
29
|
+
// The mock delays run on a fake clock, so each request settles only when the test moves time past it.
|
|
30
|
+
// On real timers a stall can settle both requests before the single running one is ever observed.
|
|
31
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
|
28
32
|
const usersRequest = createRequest({ endpoint: "/users" });
|
|
29
33
|
const postsRequest = createRequest({ endpoint: "/posts" });
|
|
30
34
|
mockRequest(usersRequest, { delay: 30 });
|
|
31
|
-
mockRequest(postsRequest, { delay:
|
|
35
|
+
mockRequest(postsRequest, { delay: 1000 });
|
|
32
36
|
|
|
33
37
|
const { result } = renderHook(() => useIsFetching(client));
|
|
34
38
|
expect(result.current).toBe(0);
|
|
@@ -37,14 +41,20 @@ describe("useIsFetching [ Base ]", () => {
|
|
|
37
41
|
client.fetchDispatcher.add(usersRequest);
|
|
38
42
|
client.fetchDispatcher.add(postsRequest);
|
|
39
43
|
});
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
await waitFor(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
expect(result.current).toBe(2);
|
|
45
|
+
|
|
46
|
+
// Small clock steps per real-timer poll: the users response gets real ticks to settle long before
|
|
47
|
+
// the clock can reach the posts delay, wherever msw registers its delay timers
|
|
48
|
+
await vi.waitFor(
|
|
49
|
+
async () => {
|
|
50
|
+
await act(() => vi.advanceTimersByTimeAsync(5));
|
|
51
|
+
expect(result.current).toBe(1);
|
|
52
|
+
},
|
|
53
|
+
{ interval: 5 },
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
await act(() => vi.advanceTimersByTimeAsync(1000));
|
|
57
|
+
await vi.waitFor(() => {
|
|
48
58
|
expect(result.current).toBe(0);
|
|
49
59
|
});
|
|
50
60
|
});
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { useFetch } from "hooks/use-fetch";
|
|
3
|
+
import type { UseIsFetchingOptionsType } from "hooks/use-is-fetching";
|
|
4
|
+
import { useIsFetching, useIsSubmitting } from "hooks/use-is-fetching";
|
|
5
|
+
import { StrictMode } from "react";
|
|
6
|
+
import { expectTypeOf } from "vitest";
|
|
7
|
+
|
|
8
|
+
import { client, createRequest } from "../../utils";
|
|
9
|
+
import { advance, countListeners, gateRequest, settle } from "../../utils/gate.utils";
|
|
10
|
+
|
|
11
|
+
type Data = { value: number };
|
|
12
|
+
|
|
13
|
+
describe("useIsFetching [ Lifecycle ]", () => {
|
|
14
|
+
let request = createRequest<Data, any, "/items/:id">({ endpoint: "/items/:id" });
|
|
15
|
+
let calls = gateRequest(request);
|
|
16
|
+
|
|
17
|
+
const item = (id: number) => request.setParams({ id });
|
|
18
|
+
|
|
19
|
+
const renderCounter = (options?: UseIsFetchingOptionsType, wrapper?: typeof StrictMode) => {
|
|
20
|
+
const counter = { renders: 0 };
|
|
21
|
+
const view = renderHook(
|
|
22
|
+
(props: UseIsFetchingOptionsType | undefined) => {
|
|
23
|
+
counter.renders += 1;
|
|
24
|
+
return useIsFetching(client, props);
|
|
25
|
+
},
|
|
26
|
+
{ initialProps: options, wrapper },
|
|
27
|
+
);
|
|
28
|
+
return { ...view, counter };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const send = (id: number) => {
|
|
32
|
+
act(() => {
|
|
33
|
+
client.fetchDispatcher.add(item(id));
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
vi.useFakeTimers();
|
|
39
|
+
client.clear();
|
|
40
|
+
request = createRequest<Data, any, "/items/:id">({ endpoint: "/items/:id" });
|
|
41
|
+
calls = gateRequest(request);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
vi.useRealTimers();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("when requests are running", () => {
|
|
49
|
+
it("should return zero when nothing is running", () => {
|
|
50
|
+
const { result } = renderCounter();
|
|
51
|
+
|
|
52
|
+
expect(result.current).toBe(0);
|
|
53
|
+
});
|
|
54
|
+
it("should count the request from its start to the response", async () => {
|
|
55
|
+
const { result } = renderCounter();
|
|
56
|
+
|
|
57
|
+
send(1);
|
|
58
|
+
expect(result.current).toBe(1);
|
|
59
|
+
|
|
60
|
+
await advance();
|
|
61
|
+
calls[0].succeed({ value: 1 });
|
|
62
|
+
await settle();
|
|
63
|
+
|
|
64
|
+
expect(result.current).toBe(0);
|
|
65
|
+
});
|
|
66
|
+
it("should count the request until the error response", async () => {
|
|
67
|
+
const { result } = renderCounter();
|
|
68
|
+
|
|
69
|
+
send(1);
|
|
70
|
+
await advance();
|
|
71
|
+
calls[0].fail();
|
|
72
|
+
await settle();
|
|
73
|
+
|
|
74
|
+
expect(result.current).toBe(0);
|
|
75
|
+
});
|
|
76
|
+
it("should count requests of different keys", async () => {
|
|
77
|
+
const { result } = renderCounter();
|
|
78
|
+
|
|
79
|
+
send(1);
|
|
80
|
+
send(2);
|
|
81
|
+
send(3);
|
|
82
|
+
expect(result.current).toBe(3);
|
|
83
|
+
|
|
84
|
+
await advance();
|
|
85
|
+
calls[1].succeed({ value: 2 });
|
|
86
|
+
await settle();
|
|
87
|
+
|
|
88
|
+
expect(result.current).toBe(2);
|
|
89
|
+
});
|
|
90
|
+
it("should count the deduplicated requests once", () => {
|
|
91
|
+
const { result } = renderCounter();
|
|
92
|
+
|
|
93
|
+
send(1);
|
|
94
|
+
send(1);
|
|
95
|
+
send(1);
|
|
96
|
+
|
|
97
|
+
expect(result.current).toBe(1);
|
|
98
|
+
});
|
|
99
|
+
it("should count the requests started before the hook mounted", () => {
|
|
100
|
+
send(1);
|
|
101
|
+
send(2);
|
|
102
|
+
|
|
103
|
+
const { result } = renderCounter();
|
|
104
|
+
|
|
105
|
+
expect(result.current).toBe(2);
|
|
106
|
+
});
|
|
107
|
+
it("should count the requests of the useFetch hooks", async () => {
|
|
108
|
+
const { result } = renderCounter();
|
|
109
|
+
renderHook(() => useFetch(item(1)).data);
|
|
110
|
+
renderHook(() => useFetch(item(1)).data);
|
|
111
|
+
renderHook(() => useFetch(item(2)).data);
|
|
112
|
+
|
|
113
|
+
expect(result.current).toBe(2);
|
|
114
|
+
|
|
115
|
+
await advance();
|
|
116
|
+
calls.forEach((call) => call.succeed({ value: 1 }));
|
|
117
|
+
await settle();
|
|
118
|
+
|
|
119
|
+
expect(result.current).toBe(0);
|
|
120
|
+
});
|
|
121
|
+
it("should keep counting the request of the unmounted hook until it finishes", async () => {
|
|
122
|
+
const { result } = renderCounter();
|
|
123
|
+
const { unmount } = renderHook(() => useFetch(item(1)).data);
|
|
124
|
+
await advance();
|
|
125
|
+
|
|
126
|
+
unmount();
|
|
127
|
+
expect(result.current).toBe(1);
|
|
128
|
+
|
|
129
|
+
calls[0].succeed({ value: 1 });
|
|
130
|
+
await settle();
|
|
131
|
+
|
|
132
|
+
expect(result.current).toBe(0);
|
|
133
|
+
});
|
|
134
|
+
it("should not count the canceled request", async () => {
|
|
135
|
+
const { result } = renderCounter();
|
|
136
|
+
send(1);
|
|
137
|
+
await advance();
|
|
138
|
+
|
|
139
|
+
act(() => {
|
|
140
|
+
client.fetchDispatcher.cancelRunningRequests(item(1).queryKey);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
expect(result.current).toBe(0);
|
|
144
|
+
});
|
|
145
|
+
it("should not count the request waiting for the connection", async () => {
|
|
146
|
+
const { result } = renderCounter();
|
|
147
|
+
act(() => {
|
|
148
|
+
client.appManager.setOnline(false);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
send(1);
|
|
152
|
+
expect(result.current).toBe(0);
|
|
153
|
+
|
|
154
|
+
act(() => {
|
|
155
|
+
client.appManager.setOnline(true);
|
|
156
|
+
});
|
|
157
|
+
expect(result.current).toBe(1);
|
|
158
|
+
});
|
|
159
|
+
it("should not count the request waiting for the retry", async () => {
|
|
160
|
+
const retried = createRequest<Data>({ retry: 1, retryTime: 5000 });
|
|
161
|
+
const retriedCalls = gateRequest(retried);
|
|
162
|
+
const { result } = renderCounter();
|
|
163
|
+
act(() => {
|
|
164
|
+
client.fetchDispatcher.add(retried);
|
|
165
|
+
});
|
|
166
|
+
await advance();
|
|
167
|
+
|
|
168
|
+
retriedCalls[0].fail();
|
|
169
|
+
await settle();
|
|
170
|
+
expect(result.current).toBe(0);
|
|
171
|
+
|
|
172
|
+
await advance(5000);
|
|
173
|
+
expect(result.current).toBe(1);
|
|
174
|
+
});
|
|
175
|
+
it("should not count the submit requests", () => {
|
|
176
|
+
const submit = createRequest<Data>({ method: "POST" });
|
|
177
|
+
gateRequest(submit);
|
|
178
|
+
const { result } = renderCounter();
|
|
179
|
+
const submitting = renderHook(() => useIsSubmitting(client));
|
|
180
|
+
|
|
181
|
+
act(() => {
|
|
182
|
+
client.submitDispatcher.add(submit);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(result.current).toBe(0);
|
|
186
|
+
expect(submitting.result.current).toBe(1);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("when filters are used", () => {
|
|
191
|
+
it("should count only the requests with the given query key", () => {
|
|
192
|
+
const { result } = renderCounter({ queryKey: item(1).queryKey });
|
|
193
|
+
|
|
194
|
+
send(1);
|
|
195
|
+
send(2);
|
|
196
|
+
|
|
197
|
+
expect(result.current).toBe(1);
|
|
198
|
+
});
|
|
199
|
+
it("should count only the requests matching the RegExp", () => {
|
|
200
|
+
const { result } = renderCounter({ queryKey: /items\/[12]_/ });
|
|
201
|
+
|
|
202
|
+
send(1);
|
|
203
|
+
send(2);
|
|
204
|
+
send(3);
|
|
205
|
+
|
|
206
|
+
expect(result.current).toBe(2);
|
|
207
|
+
});
|
|
208
|
+
it("should count only the requests accepted by the predicate", () => {
|
|
209
|
+
const { result } = renderCounter({
|
|
210
|
+
predicate: (running) => (running.params as unknown as { id: number }).id === 3,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
send(1);
|
|
214
|
+
send(3);
|
|
215
|
+
|
|
216
|
+
expect(result.current).toBe(1);
|
|
217
|
+
});
|
|
218
|
+
it("should combine the query key with the predicate", () => {
|
|
219
|
+
const { result } = renderCounter({
|
|
220
|
+
queryKey: /items/,
|
|
221
|
+
predicate: (running) => (running.params as unknown as { id: number }).id !== 1,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
send(1);
|
|
225
|
+
send(2);
|
|
226
|
+
|
|
227
|
+
expect(result.current).toBe(1);
|
|
228
|
+
});
|
|
229
|
+
it("should apply the filters changed between renders", () => {
|
|
230
|
+
const { result, rerender } = renderCounter({ queryKey: item(1).queryKey });
|
|
231
|
+
send(1);
|
|
232
|
+
send(2);
|
|
233
|
+
send(3);
|
|
234
|
+
expect(result.current).toBe(1);
|
|
235
|
+
|
|
236
|
+
rerender({});
|
|
237
|
+
expect(result.current).toBe(3);
|
|
238
|
+
|
|
239
|
+
rerender({ queryKey: "unknown" });
|
|
240
|
+
expect(result.current).toBe(0);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe("when the count does not change", () => {
|
|
245
|
+
it("should not rerender for the requests filtered out", async () => {
|
|
246
|
+
const { counter } = renderCounter({ queryKey: item(1).queryKey });
|
|
247
|
+
|
|
248
|
+
send(2);
|
|
249
|
+
await advance();
|
|
250
|
+
calls[0].succeed({ value: 2 });
|
|
251
|
+
await settle();
|
|
252
|
+
|
|
253
|
+
expect(counter.renders).toBe(1);
|
|
254
|
+
});
|
|
255
|
+
it("should render once per count change", async () => {
|
|
256
|
+
const { counter } = renderCounter();
|
|
257
|
+
|
|
258
|
+
send(1);
|
|
259
|
+
await advance();
|
|
260
|
+
calls[0].succeed({ value: 1 });
|
|
261
|
+
await settle();
|
|
262
|
+
|
|
263
|
+
// 0 -> 1 -> 0
|
|
264
|
+
expect(counter.renders).toBe(3);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
describe("when rendered in StrictMode", () => {
|
|
269
|
+
it("should count the running requests", async () => {
|
|
270
|
+
const { result } = renderCounter(undefined, StrictMode);
|
|
271
|
+
|
|
272
|
+
send(1);
|
|
273
|
+
expect(result.current).toBe(1);
|
|
274
|
+
|
|
275
|
+
await advance();
|
|
276
|
+
calls[0].succeed({ value: 1 });
|
|
277
|
+
await settle();
|
|
278
|
+
|
|
279
|
+
expect(result.current).toBe(0);
|
|
280
|
+
});
|
|
281
|
+
it("should remove the listeners after unmount", () => {
|
|
282
|
+
const baseline = countListeners(client);
|
|
283
|
+
const { unmount } = renderCounter(undefined, StrictMode);
|
|
284
|
+
|
|
285
|
+
unmount();
|
|
286
|
+
|
|
287
|
+
expect(countListeners(client)).toStrictEqual(baseline);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
describe("when checking the types", () => {
|
|
292
|
+
it("should return a number", () => {
|
|
293
|
+
expectTypeOf(useIsFetching).returns.toEqualTypeOf<number>();
|
|
294
|
+
expectTypeOf(useIsSubmitting).returns.toEqualTypeOf<number>();
|
|
295
|
+
});
|
|
296
|
+
it("should accept the client and optional filters", () => {
|
|
297
|
+
expectTypeOf(useIsFetching).parameter(1).toEqualTypeOf<UseIsFetchingOptionsType | undefined>();
|
|
298
|
+
expectTypeOf<UseIsFetchingOptionsType["queryKey"]>().toEqualTypeOf<string | RegExp | undefined>();
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
});
|