@hyper-fetch/react 9.6.1 → 9.7.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.
- 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.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.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.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.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.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-helpers.spec.tsx +455 -0
- package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +6 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +5 -1
- 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__/utils/gate.utils.ts +103 -0
- package/__tests__/utils/gated-client.utils.ts +158 -0
- package/__tests__/utils/helpers.utils.ts +19 -0
- 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.constants.d.ts +1 -1
- package/dist/hooks/use-submit/use-submit.constants.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.types.d.ts +6 -1
- package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -1
- package/dist/index.mjs +633 -346
- 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 +141 -28
- 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.constants.ts +3 -1
- package/src/hooks/use-submit/use-submit.hooks.ts +167 -53
- package/src/hooks/use-submit/use-submit.types.ts +6 -0
- 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,285 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
2
|
+
import { useSubmit } from "hooks/use-submit";
|
|
3
|
+
|
|
4
|
+
import { createGatedClient } from "../../utils/gated-client.utils";
|
|
5
|
+
|
|
6
|
+
type User = { id: number; name: string };
|
|
7
|
+
|
|
8
|
+
const setup = (options?: { queued?: boolean; offline?: boolean }) => {
|
|
9
|
+
const gated = createGatedClient();
|
|
10
|
+
const request = gated.client.createRequest<{ response: User; payload: { name: string } }>()({
|
|
11
|
+
endpoint: "/users",
|
|
12
|
+
method: "POST",
|
|
13
|
+
queued: options?.queued ?? false,
|
|
14
|
+
offline: options?.offline ?? true,
|
|
15
|
+
});
|
|
16
|
+
const setOnline = (isOnline: boolean) => {
|
|
17
|
+
act(() => {
|
|
18
|
+
gated.client.appManager.setOnline(isOnline);
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
const renderSubmit = () => {
|
|
22
|
+
const spies = { success: vi.fn(), error: vi.fn(), offline: vi.fn(), finished: vi.fn(), requestStart: vi.fn() };
|
|
23
|
+
const view = renderHook(() => {
|
|
24
|
+
const submit = useSubmit(request, { dependencyTracking: false });
|
|
25
|
+
submit.onSubmitSuccess(spies.success);
|
|
26
|
+
submit.onSubmitError(spies.error);
|
|
27
|
+
submit.onSubmitOfflineError(spies.offline);
|
|
28
|
+
submit.onSubmitFinished(spies.finished);
|
|
29
|
+
submit.onSubmitRequestStart(spies.requestStart);
|
|
30
|
+
return submit;
|
|
31
|
+
});
|
|
32
|
+
return { ...view, spies };
|
|
33
|
+
};
|
|
34
|
+
return { ...gated, request, setOnline, renderSubmit };
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Run the pending timers and promise callbacks - for the tests using fake timers */
|
|
38
|
+
const advance = async (time = 0) => {
|
|
39
|
+
await act(async () => {
|
|
40
|
+
await vi.advanceTimersByTimeAsync(time);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
describe("useSubmit [ Offline ]", () => {
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
vi.useRealTimers();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("when submitting while the application is offline", () => {
|
|
50
|
+
it("should hold the request until the connection is back", async () => {
|
|
51
|
+
vi.useFakeTimers();
|
|
52
|
+
const { calls, setOnline, renderSubmit, client, request } = setup();
|
|
53
|
+
const { result } = renderSubmit();
|
|
54
|
+
setOnline(false);
|
|
55
|
+
|
|
56
|
+
act(() => {
|
|
57
|
+
result.current.submit({ payload: { name: "Maciej" } });
|
|
58
|
+
});
|
|
59
|
+
await advance(10_000);
|
|
60
|
+
|
|
61
|
+
expect(calls).toHaveLength(0);
|
|
62
|
+
expect(client.submitDispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
|
|
63
|
+
});
|
|
64
|
+
it("should keep the submit promise pending while offline", async () => {
|
|
65
|
+
vi.useFakeTimers();
|
|
66
|
+
const { setOnline, renderSubmit } = setup();
|
|
67
|
+
const { result } = renderSubmit();
|
|
68
|
+
const settled = vi.fn();
|
|
69
|
+
setOnline(false);
|
|
70
|
+
|
|
71
|
+
act(() => {
|
|
72
|
+
result.current.submit({ payload: { name: "Maciej" } }).then(settled);
|
|
73
|
+
});
|
|
74
|
+
await advance(10_000);
|
|
75
|
+
|
|
76
|
+
expect(settled).not.toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
it("should send the request when the connection is back", async () => {
|
|
79
|
+
const { waitForCall, setOnline, renderSubmit } = setup();
|
|
80
|
+
const { result } = renderSubmit();
|
|
81
|
+
setOnline(false);
|
|
82
|
+
act(() => {
|
|
83
|
+
result.current.submit({ payload: { name: "Maciej" } });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
setOnline(true);
|
|
87
|
+
|
|
88
|
+
const call = await waitForCall(0);
|
|
89
|
+
expect(call.request.payload).toStrictEqual({ name: "Maciej" });
|
|
90
|
+
expect(result.current.submitting).toBeTrue();
|
|
91
|
+
});
|
|
92
|
+
it("should resolve the submit and update the state once the held request is answered", async () => {
|
|
93
|
+
const { waitForCall, setOnline, renderSubmit } = setup();
|
|
94
|
+
const { result, spies } = renderSubmit();
|
|
95
|
+
let promise: ReturnType<typeof result.current.submit> | null = null;
|
|
96
|
+
setOnline(false);
|
|
97
|
+
act(() => {
|
|
98
|
+
promise = result.current.submit({ payload: { name: "Maciej" } });
|
|
99
|
+
});
|
|
100
|
+
setOnline(true);
|
|
101
|
+
const call = await waitForCall(0);
|
|
102
|
+
|
|
103
|
+
act(() => {
|
|
104
|
+
call.succeed({ id: 1, name: "Maciej" });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const response = await promise!;
|
|
108
|
+
expect(response.data).toStrictEqual({ id: 1, name: "Maciej" });
|
|
109
|
+
await waitFor(() => {
|
|
110
|
+
expect(result.current.data).toStrictEqual({ id: 1, name: "Maciej" });
|
|
111
|
+
});
|
|
112
|
+
expect(result.current.submitting).toBeFalse();
|
|
113
|
+
expect(spies.success).toHaveBeenCalledTimes(1);
|
|
114
|
+
expect(spies.offline).not.toHaveBeenCalled();
|
|
115
|
+
expect(spies.error).not.toHaveBeenCalled();
|
|
116
|
+
});
|
|
117
|
+
it("should send every held request when the connection is back", async () => {
|
|
118
|
+
const { waitForCalls, setOnline, renderSubmit } = setup();
|
|
119
|
+
const { result } = renderSubmit();
|
|
120
|
+
setOnline(false);
|
|
121
|
+
act(() => {
|
|
122
|
+
result.current.submit({ payload: { name: "First" } });
|
|
123
|
+
result.current.submit({ payload: { name: "Second" } });
|
|
124
|
+
result.current.submit({ payload: { name: "Third" } });
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
setOnline(true);
|
|
128
|
+
|
|
129
|
+
const calls = await waitForCalls(3);
|
|
130
|
+
expect(calls.map((call) => call.request.payload)).toStrictEqual([
|
|
131
|
+
{ name: "First" },
|
|
132
|
+
{ name: "Second" },
|
|
133
|
+
{ name: "Third" },
|
|
134
|
+
]);
|
|
135
|
+
});
|
|
136
|
+
it("should send the held queued requests one by one in order", async () => {
|
|
137
|
+
const { waitForCall, calls, setOnline, renderSubmit } = setup({ queued: true });
|
|
138
|
+
const { result } = renderSubmit();
|
|
139
|
+
setOnline(false);
|
|
140
|
+
act(() => {
|
|
141
|
+
result.current.submit({ payload: { name: "First" } });
|
|
142
|
+
result.current.submit({ payload: { name: "Second" } });
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
setOnline(true);
|
|
146
|
+
const firstCall = await waitForCall(0);
|
|
147
|
+
expect(calls).toHaveLength(1);
|
|
148
|
+
act(() => {
|
|
149
|
+
firstCall.succeed({ id: 1, name: "First" });
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const secondCall = await waitForCall(1);
|
|
153
|
+
expect(firstCall.request.payload).toStrictEqual({ name: "First" });
|
|
154
|
+
expect(secondCall.request.payload).toStrictEqual({ name: "Second" });
|
|
155
|
+
});
|
|
156
|
+
it("should send the held request even when the hook unmounted in the meantime", async () => {
|
|
157
|
+
const { waitForCall, setOnline, renderSubmit } = setup();
|
|
158
|
+
const { result, unmount } = renderSubmit();
|
|
159
|
+
let promise: ReturnType<typeof result.current.submit> | null = null;
|
|
160
|
+
setOnline(false);
|
|
161
|
+
act(() => {
|
|
162
|
+
promise = result.current.submit({ payload: { name: "Maciej" } });
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
unmount();
|
|
166
|
+
setOnline(true);
|
|
167
|
+
const call = await waitForCall(0);
|
|
168
|
+
call.succeed({ id: 1, name: "Maciej" });
|
|
169
|
+
|
|
170
|
+
const response = await promise!;
|
|
171
|
+
expect(response.data).toStrictEqual({ id: 1, name: "Maciej" });
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe("when the connection drops during the request", () => {
|
|
176
|
+
it("should call the offline callback and keep the request for later", async () => {
|
|
177
|
+
const { waitForCall, setOnline, renderSubmit, client, request } = setup();
|
|
178
|
+
const { result, spies } = renderSubmit();
|
|
179
|
+
act(() => {
|
|
180
|
+
result.current.submit({ payload: { name: "Maciej" } });
|
|
181
|
+
});
|
|
182
|
+
const call = await waitForCall(0);
|
|
183
|
+
|
|
184
|
+
setOnline(false);
|
|
185
|
+
act(() => {
|
|
186
|
+
call.fail({ message: "Network error" }, 0);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
await waitFor(() => {
|
|
190
|
+
expect(spies.offline).toHaveBeenCalledTimes(1);
|
|
191
|
+
});
|
|
192
|
+
expect(spies.error).not.toHaveBeenCalled();
|
|
193
|
+
expect(client.submitDispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
|
|
194
|
+
});
|
|
195
|
+
it("should keep the submit promise pending until the request is sent again", async () => {
|
|
196
|
+
const { waitForCall, setOnline, renderSubmit } = setup();
|
|
197
|
+
const { result, spies } = renderSubmit();
|
|
198
|
+
const settled = vi.fn();
|
|
199
|
+
act(() => {
|
|
200
|
+
result.current.submit({ payload: { name: "Maciej" } }).then(settled);
|
|
201
|
+
});
|
|
202
|
+
const call = await waitForCall(0);
|
|
203
|
+
|
|
204
|
+
setOnline(false);
|
|
205
|
+
act(() => {
|
|
206
|
+
call.fail({ message: "Network error" }, 0);
|
|
207
|
+
});
|
|
208
|
+
await waitFor(() => {
|
|
209
|
+
expect(spies.offline).toHaveBeenCalledTimes(1);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
expect(settled).not.toHaveBeenCalled();
|
|
213
|
+
});
|
|
214
|
+
it("should send the request again when the connection is back", async () => {
|
|
215
|
+
const { waitForCall, setOnline, renderSubmit } = setup();
|
|
216
|
+
const { result, spies } = renderSubmit();
|
|
217
|
+
let promise: ReturnType<typeof result.current.submit> | null = null;
|
|
218
|
+
act(() => {
|
|
219
|
+
promise = result.current.submit({ payload: { name: "Maciej" } });
|
|
220
|
+
});
|
|
221
|
+
const call = await waitForCall(0);
|
|
222
|
+
setOnline(false);
|
|
223
|
+
act(() => {
|
|
224
|
+
call.fail({ message: "Network error" }, 0);
|
|
225
|
+
});
|
|
226
|
+
await waitFor(() => {
|
|
227
|
+
expect(spies.offline).toHaveBeenCalledTimes(1);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
setOnline(true);
|
|
231
|
+
const retried = await waitForCall(1);
|
|
232
|
+
act(() => {
|
|
233
|
+
retried.succeed({ id: 1, name: "Maciej" });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const response = await promise!;
|
|
237
|
+
expect(retried.requestId).toBe(call.requestId);
|
|
238
|
+
expect(response.data).toStrictEqual({ id: 1, name: "Maciej" });
|
|
239
|
+
await waitFor(() => {
|
|
240
|
+
expect(result.current.data).toStrictEqual({ id: 1, name: "Maciej" });
|
|
241
|
+
});
|
|
242
|
+
expect(result.current.error).toBeNull();
|
|
243
|
+
expect(spies.success).toHaveBeenCalledTimes(1);
|
|
244
|
+
expect(spies.requestStart).toHaveBeenCalledTimes(2);
|
|
245
|
+
});
|
|
246
|
+
it("should fail right away when the request does not wait for the network", async () => {
|
|
247
|
+
const { waitForCall, setOnline, renderSubmit, client, request } = setup({ offline: false });
|
|
248
|
+
const { result, spies } = renderSubmit();
|
|
249
|
+
let promise: ReturnType<typeof result.current.submit> | null = null;
|
|
250
|
+
act(() => {
|
|
251
|
+
promise = result.current.submit({ payload: { name: "Maciej" } });
|
|
252
|
+
});
|
|
253
|
+
const call = await waitForCall(0);
|
|
254
|
+
|
|
255
|
+
setOnline(false);
|
|
256
|
+
act(() => {
|
|
257
|
+
call.fail({ message: "Network error" }, 0);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const response = await promise!;
|
|
261
|
+
expect(response.success).toBeFalse();
|
|
262
|
+
expect(response.error).toStrictEqual({ message: "Network error" });
|
|
263
|
+
expect(spies.error).toHaveBeenCalledTimes(1);
|
|
264
|
+
expect(spies.offline).not.toHaveBeenCalled();
|
|
265
|
+
expect(client.submitDispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
266
|
+
});
|
|
267
|
+
it("should not send the failed request again when it does not wait for the network", async () => {
|
|
268
|
+
vi.useFakeTimers();
|
|
269
|
+
const { calls, setOnline, renderSubmit } = setup({ offline: false });
|
|
270
|
+
const { result } = renderSubmit();
|
|
271
|
+
act(() => {
|
|
272
|
+
result.current.submit({ payload: { name: "Maciej" } });
|
|
273
|
+
});
|
|
274
|
+
await advance();
|
|
275
|
+
setOnline(false);
|
|
276
|
+
calls[0].fail({ message: "Network error" }, 0);
|
|
277
|
+
await advance();
|
|
278
|
+
|
|
279
|
+
setOnline(true);
|
|
280
|
+
await advance(10_000);
|
|
281
|
+
|
|
282
|
+
expect(calls).toHaveLength(1);
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
});
|