@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.
Files changed (98) hide show
  1. package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +1 -1
  2. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.keys.spec.ts +294 -0
  3. package/__tests__/features/helpers/utils.spec.ts +127 -0
  4. package/__tests__/features/provider/provider.config.spec.tsx +172 -0
  5. package/__tests__/features/use-app-manager/use-app-manager.events.spec.tsx +497 -0
  6. package/__tests__/features/use-cache/use-cache.state.spec.tsx +326 -0
  7. package/__tests__/features/use-emitter/use-emitter.lifecycle.spec.tsx +417 -0
  8. package/__tests__/features/use-fetch/use-fetch.bounce-timers.spec.ts +199 -0
  9. package/__tests__/features/use-fetch/use-fetch.cache-events.spec.ts +212 -0
  10. package/__tests__/features/use-fetch/use-fetch.callbacks.spec.tsx +359 -0
  11. package/__tests__/features/use-fetch/use-fetch.hydration.spec.tsx +126 -0
  12. package/__tests__/features/use-fetch/use-fetch.infinite.spec.ts +146 -0
  13. package/__tests__/features/use-fetch/use-fetch.initial-state.spec.ts +340 -0
  14. package/__tests__/features/use-fetch/use-fetch.keys.spec.tsx +559 -0
  15. package/__tests__/features/use-fetch/use-fetch.lifecycle.spec.ts +348 -0
  16. package/__tests__/features/use-fetch/use-fetch.mounting.spec.tsx +411 -0
  17. package/__tests__/features/use-fetch/use-fetch.pagination.spec.ts +177 -0
  18. package/__tests__/features/use-fetch/use-fetch.refresh-interval.spec.ts +355 -0
  19. package/__tests__/features/use-fetch/use-fetch.response-order.spec.ts +105 -0
  20. package/__tests__/features/use-fetch/use-fetch.retry.spec.ts +235 -0
  21. package/__tests__/features/use-fetch/use-fetch.return-types.spec.ts +186 -0
  22. package/__tests__/features/use-fetch/use-fetch.scope.spec.ts +333 -0
  23. package/__tests__/features/use-fetch/use-fetch.ssr.spec.tsx +195 -0
  24. package/__tests__/features/use-fetch/use-fetch.suspense-edge.spec.tsx +391 -0
  25. package/__tests__/features/use-fetch/use-fetch.throwing-callbacks.spec.tsx +112 -0
  26. package/__tests__/features/use-fetch/use-fetch.tracking.spec.tsx +364 -0
  27. package/__tests__/features/use-fetch/use-fetch.window-events.spec.ts +300 -0
  28. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.parity.spec.tsx +1027 -0
  29. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.resubscribe.spec.tsx +267 -0
  30. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.typescript.spec.ts +64 -1
  31. package/__tests__/features/use-is-fetching/use-is-fetching.lifecycle.spec.tsx +301 -0
  32. package/__tests__/features/use-listener/use-listener.lifecycle.spec.tsx +507 -0
  33. package/__tests__/features/use-listener/use-listener.ssr.spec.tsx +89 -0
  34. package/__tests__/features/use-queue/use-queue.controls.spec.tsx +593 -0
  35. package/__tests__/features/use-queue/use-queue.list.spec.tsx +560 -0
  36. package/__tests__/features/use-queue/use-queue.progress.spec.tsx +650 -0
  37. package/__tests__/features/use-submit/use-submit.abort.spec.tsx +270 -0
  38. package/__tests__/features/use-submit/use-submit.actions.spec.tsx +446 -0
  39. package/__tests__/features/use-submit/use-submit.arguments.spec.tsx +732 -0
  40. package/__tests__/features/use-submit/use-submit.bounce.spec.tsx +644 -0
  41. package/__tests__/features/use-submit/use-submit.callbacks.spec.tsx +860 -0
  42. package/__tests__/features/use-submit/use-submit.chaining.spec.tsx +167 -0
  43. package/__tests__/features/use-submit/use-submit.double-submit.spec.tsx +552 -0
  44. package/__tests__/features/use-submit/use-submit.offline.spec.tsx +285 -0
  45. package/__tests__/features/use-submit/use-submit.optimistic-cache.spec.tsx +619 -0
  46. package/__tests__/features/use-submit/use-submit.optimistic-helpers.spec.tsx +455 -0
  47. package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +6 -0
  48. package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +5 -1
  49. package/__tests__/features/use-submit/use-submit.state.spec.tsx +1025 -0
  50. package/__tests__/features/use-submit/use-submit.submit-types.spec.ts +194 -0
  51. package/__tests__/features/use-submit/use-submit.submitting.spec.tsx +180 -0
  52. package/__tests__/utils/gate.utils.ts +103 -0
  53. package/__tests__/utils/gated-client.utils.ts +158 -0
  54. package/__tests__/utils/helpers.utils.ts +19 -0
  55. package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
  56. package/dist/helpers/use-request-events/use-request-events.types.d.ts +1 -1
  57. package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -1
  58. package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -1
  59. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +8 -0
  60. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
  61. package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
  62. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
  63. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.hooks.d.ts.map +1 -1
  64. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts +3 -0
  65. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts.map +1 -1
  66. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts +5 -3
  67. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts.map +1 -1
  68. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts +11 -1
  69. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts.map +1 -1
  70. package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -1
  71. package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -1
  72. package/dist/hooks/use-submit/use-submit.constants.d.ts +1 -1
  73. package/dist/hooks/use-submit/use-submit.constants.d.ts.map +1 -1
  74. package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
  75. package/dist/hooks/use-submit/use-submit.types.d.ts +6 -1
  76. package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -1
  77. package/dist/index.mjs +633 -346
  78. package/dist/index.mjs.map +1 -1
  79. package/dist/provider/provider.d.ts.map +1 -1
  80. package/package.json +1 -1
  81. package/src/helpers/use-request-events/use-request-events.hooks.ts +141 -28
  82. package/src/helpers/use-request-events/use-request-events.types.ts +1 -1
  83. package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +148 -101
  84. package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +26 -5
  85. package/src/hooks/use-cache/use-cache.hooks.ts +20 -5
  86. package/src/hooks/use-fetch/use-fetch.hooks.ts +194 -113
  87. package/src/hooks/use-infinite-fetch/use-infinite-fetch.hooks.ts +55 -26
  88. package/src/hooks/use-infinite-fetch/use-infinite-fetch.store.ts +35 -29
  89. package/src/hooks/use-infinite-fetch/use-infinite-fetch.types.ts +5 -3
  90. package/src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts +25 -1
  91. package/src/hooks/use-listener/use-listener.hooks.ts +2 -1
  92. package/src/hooks/use-queue/use-queue.hooks.ts +85 -60
  93. package/src/hooks/use-submit/use-submit.constants.ts +3 -1
  94. package/src/hooks/use-submit/use-submit.hooks.ts +167 -53
  95. package/src/hooks/use-submit/use-submit.types.ts +6 -0
  96. package/src/provider/provider.tsx +9 -1
  97. package/__tests__/features/use-fetch/use-fetch.pagination.ts +0 -36
  98. package/__tests__/features/use-fetch/use-fetch.retry.ts +0 -38
@@ -0,0 +1,560 @@
1
+ import type { RequestInstance } from "@hyper-fetch/core";
2
+ import { act, renderHook, waitFor } from "@testing-library/react";
3
+ import type { QueueRequest } from "hooks/use-queue";
4
+ import { useQueue } from "hooks/use-queue";
5
+ import { Provider } from "provider";
6
+ import type { ReactNode } from "react";
7
+ import { StrictMode } from "react";
8
+
9
+ import { createGatedClient } from "../../utils/gated-client.utils";
10
+
11
+ const setup = (options?: { queued?: boolean; method?: "POST" | "GET"; endpoint?: string }) => {
12
+ const gated = createGatedClient();
13
+ const request = gated.client.createRequest<{ response: { id: number }; payload: { name: string } }>()({
14
+ endpoint: options?.endpoint ?? "/items",
15
+ method: options?.method ?? "POST",
16
+ queued: options?.queued ?? true,
17
+ });
18
+ const dispatcher = options?.method === "GET" ? gated.client.fetchDispatcher : gated.client.submitDispatcher;
19
+ /** Adds the request to the dispatcher and returns its id */
20
+ const add = (req: RequestInstance = request) => {
21
+ let requestId = "";
22
+ act(() => {
23
+ requestId = dispatcher.add(req);
24
+ });
25
+ return requestId;
26
+ };
27
+ return { ...gated, request, dispatcher, add };
28
+ };
29
+
30
+ const getIds = (requests: QueueRequest<RequestInstance>[]) => requests.map((item) => item.requestId);
31
+
32
+ describe("useQueue [ List ]", () => {
33
+ describe("given the queue is empty", () => {
34
+ it("should start with an empty list and a running queue", () => {
35
+ const { request } = setup();
36
+ const { result } = renderHook(() => useQueue(request));
37
+
38
+ expect(result.current.requests).toStrictEqual([]);
39
+ expect(result.current.stopped).toBeFalse();
40
+ });
41
+ it("should expose the dispatcher that handles the request", () => {
42
+ const { request, client } = setup();
43
+ const { result } = renderHook(() => useQueue(request));
44
+
45
+ expect(result.current.dispatcher).toBe(client.submitDispatcher);
46
+ });
47
+ it("should add the request to the list once it is queued", async () => {
48
+ const { request, add, waitForCall } = setup();
49
+ const { result } = renderHook(() => useQueue(request));
50
+
51
+ const requestId = add();
52
+ await waitForCall(0);
53
+
54
+ expect(getIds(result.current.requests)).toStrictEqual([requestId]);
55
+ });
56
+ it("should describe a freshly queued request", async () => {
57
+ const { request, add, waitForCall } = setup();
58
+ const { result } = renderHook(() => useQueue(request));
59
+
60
+ const requestId = add(request.setPayload({ name: "Maciej" }));
61
+ await waitForCall(0);
62
+
63
+ const [item] = result.current.requests;
64
+ expect(item.requestId).toBe(requestId);
65
+ expect(item.request.payload).toStrictEqual({ name: "Maciej" });
66
+ expect(item.stopped).toBeFalse();
67
+ expect(item.retries).toBe(0);
68
+ expect(item.success).toBeFalse();
69
+ expect(item.failed).toBeFalse();
70
+ expect(item.canceled).toBeFalse();
71
+ expect(item.removed).toBeFalse();
72
+ expect(item.timestamp).toBeNumber();
73
+ expect(item.startRequest).toBeFunction();
74
+ expect(item.stopRequest).toBeFunction();
75
+ expect(item.deleteRequest).toBeFunction();
76
+ });
77
+ });
78
+
79
+ describe("given requests are already queued before the hook mounts", () => {
80
+ it("should list them on the very first render", () => {
81
+ const { request, dispatcher } = setup();
82
+ dispatcher.stop(request.queryKey);
83
+ const first = dispatcher.add(request);
84
+ const second = dispatcher.add(request);
85
+ const renders: string[][] = [];
86
+
87
+ renderHook(() => {
88
+ const queue = useQueue(request);
89
+ renders.push(getIds(queue.requests));
90
+ return queue;
91
+ });
92
+
93
+ expect(renders[0]).toStrictEqual([first, second]);
94
+ });
95
+ it("should read the stopped status on the very first render", () => {
96
+ const { request, dispatcher } = setup();
97
+ dispatcher.stop(request.queryKey);
98
+ const renders: boolean[] = [];
99
+
100
+ renderHook(() => {
101
+ const queue = useQueue(request);
102
+ renders.push(queue.stopped);
103
+ return queue;
104
+ });
105
+
106
+ expect(renders[0]).toBeTrue();
107
+ });
108
+ it("should list the running request and the waiting ones", async () => {
109
+ const { request, dispatcher, waitForCall } = setup();
110
+ const first = dispatcher.add(request);
111
+ const second = dispatcher.add(request);
112
+ const third = dispatcher.add(request);
113
+ await waitForCall(0);
114
+
115
+ const { result } = renderHook(() => useQueue(request));
116
+
117
+ expect(getIds(result.current.requests)).toStrictEqual([first, second, third]);
118
+ });
119
+ });
120
+
121
+ describe("given several requests are queued one after another", () => {
122
+ it("should keep the earlier requests when another one is added", async () => {
123
+ const { request, add, waitForCall } = setup();
124
+ const { result } = renderHook(() => useQueue(request));
125
+
126
+ const first = add();
127
+ await waitForCall(0);
128
+ const second = add();
129
+
130
+ expect(getIds(result.current.requests)).toStrictEqual([first, second]);
131
+ });
132
+ it("should list every queued request in the order it was added", async () => {
133
+ const { request, add, waitForCall } = setup();
134
+ const { result } = renderHook(() => useQueue(request));
135
+
136
+ const first = add();
137
+ await waitForCall(0);
138
+ const second = add();
139
+ const third = add();
140
+ const fourth = add();
141
+
142
+ expect(getIds(result.current.requests)).toStrictEqual([first, second, third, fourth]);
143
+ });
144
+ it("should list requests added in a single batch", async () => {
145
+ const { request, dispatcher, waitForCall } = setup();
146
+ const { result } = renderHook(() => useQueue(request));
147
+ const ids: string[] = [];
148
+
149
+ act(() => {
150
+ ids.push(dispatcher.add(request), dispatcher.add(request), dispatcher.add(request));
151
+ });
152
+ await waitForCall(0);
153
+
154
+ expect(getIds(result.current.requests)).toStrictEqual(ids);
155
+ });
156
+ it("should mirror the dispatcher queue after every change", async () => {
157
+ const { request, dispatcher, add, waitForCall } = setup();
158
+ const { result } = renderHook(() => useQueue(request));
159
+
160
+ for (let i = 0; i < 6; i += 1) {
161
+ add();
162
+ const queueIds = dispatcher.getQueue(request.queryKey).requests.map((item) => item.requestId);
163
+ expect(getIds(result.current.requests)).toStrictEqual(queueIds);
164
+ }
165
+ await waitForCall(0);
166
+ });
167
+ it("should remove only the finished request and keep the rest in order", async () => {
168
+ const { request, add, waitForCall } = setup();
169
+ const { result } = renderHook(() => useQueue(request));
170
+
171
+ add();
172
+ const firstCall = await waitForCall(0);
173
+ const second = add();
174
+ const third = add();
175
+
176
+ act(() => {
177
+ firstCall.succeed();
178
+ });
179
+
180
+ await waitFor(() => {
181
+ expect(getIds(result.current.requests)).toStrictEqual([second, third]);
182
+ });
183
+ });
184
+ it("should drain the list one by one as the queued requests finish", async () => {
185
+ const { request, add, waitForCall } = setup();
186
+ const { result } = renderHook(() => useQueue(request));
187
+
188
+ const ids = [add(), add(), add()];
189
+
190
+ for (let index = 0; index < ids.length; index += 1) {
191
+ // eslint-disable-next-line no-await-in-loop
192
+ const call = await waitForCall(index);
193
+ expect(call.requestId).toBe(ids[index]);
194
+ expect(getIds(result.current.requests)).toStrictEqual(ids.slice(index));
195
+ act(() => {
196
+ call.succeed();
197
+ });
198
+ // eslint-disable-next-line no-await-in-loop
199
+ await waitFor(() => {
200
+ expect(getIds(result.current.requests)).toStrictEqual(ids.slice(index + 1));
201
+ });
202
+ }
203
+
204
+ expect(result.current.requests).toStrictEqual([]);
205
+ });
206
+ it("should remove a failed request and continue with the next one", async () => {
207
+ const { request, add, waitForCall } = setup();
208
+ const { result } = renderHook(() => useQueue(request));
209
+
210
+ add();
211
+ const second = add();
212
+ const firstCall = await waitForCall(0);
213
+
214
+ act(() => {
215
+ firstCall.fail();
216
+ });
217
+
218
+ const secondCall = await waitForCall(1);
219
+ expect(secondCall.requestId).toBe(second);
220
+ await waitFor(() => {
221
+ expect(getIds(result.current.requests)).toStrictEqual([second]);
222
+ });
223
+ });
224
+ it("should keep the list in order when a waiting request is deleted from the middle", async () => {
225
+ const { request, add, waitForCall, calls } = setup();
226
+ const { result } = renderHook(() => useQueue(request));
227
+
228
+ const first = add();
229
+ await waitForCall(0);
230
+ add();
231
+ const third = add();
232
+
233
+ act(() => {
234
+ result.current.requests[1].deleteRequest();
235
+ });
236
+
237
+ expect(getIds(result.current.requests)).toStrictEqual([first, third]);
238
+ expect(calls).toHaveLength(1);
239
+ });
240
+ it("should handle a long queue", async () => {
241
+ const { request, dispatcher, waitForCall } = setup();
242
+ const { result } = renderHook(() => useQueue(request));
243
+ const ids: string[] = [];
244
+
245
+ act(() => {
246
+ for (let i = 0; i < 100; i += 1) {
247
+ ids.push(dispatcher.add(request));
248
+ }
249
+ });
250
+ const firstCall = await waitForCall(0);
251
+
252
+ expect(getIds(result.current.requests)).toStrictEqual(ids);
253
+
254
+ act(() => {
255
+ firstCall.succeed();
256
+ });
257
+
258
+ await waitFor(() => {
259
+ expect(getIds(result.current.requests)).toStrictEqual(ids.slice(1));
260
+ });
261
+ });
262
+ it("should give every item its own unique request id", async () => {
263
+ const { request, add, waitForCall } = setup();
264
+ const { result } = renderHook(() => useQueue(request));
265
+
266
+ add();
267
+ add();
268
+ add();
269
+ await waitForCall(0);
270
+
271
+ expect(new Set(getIds(result.current.requests)).size).toBe(3);
272
+ });
273
+ });
274
+
275
+ describe("given requests are sent all at once", () => {
276
+ it("should list every running request", async () => {
277
+ const { request, add, waitForCalls } = setup({ queued: false });
278
+ const { result } = renderHook(() => useQueue(request));
279
+
280
+ const ids = [add(), add(), add()];
281
+ await waitForCalls(3);
282
+
283
+ expect(getIds(result.current.requests)).toStrictEqual(ids);
284
+ });
285
+ it("should remove the requests in the order they finish", async () => {
286
+ const { request, add, waitForCalls } = setup({ queued: false });
287
+ const { result } = renderHook(() => useQueue(request));
288
+
289
+ const [first, second, third] = [add(), add(), add()];
290
+ const calls = await waitForCalls(3);
291
+
292
+ act(() => {
293
+ calls[1].succeed();
294
+ });
295
+ await waitFor(() => {
296
+ expect(getIds(result.current.requests)).toStrictEqual([first, third]);
297
+ });
298
+
299
+ act(() => {
300
+ calls[2].succeed();
301
+ });
302
+ await waitFor(() => {
303
+ expect(getIds(result.current.requests)).toStrictEqual([first]);
304
+ });
305
+ expect(second).toBeString();
306
+ });
307
+ });
308
+
309
+ describe("given requests of other queues exist", () => {
310
+ it("should not list requests with a different query key", async () => {
311
+ const { request, client, add, waitForCalls } = setup();
312
+ const otherRequest = client.createRequest()({ endpoint: "/other", method: "POST", queued: true });
313
+ const { result } = renderHook(() => useQueue(request));
314
+
315
+ const requestId = add();
316
+ add(otherRequest);
317
+ await waitForCalls(2);
318
+
319
+ expect(getIds(result.current.requests)).toStrictEqual([requestId]);
320
+ });
321
+ it("should not react to responses of other queues", async () => {
322
+ const { request, client, add, waitForCalls } = setup();
323
+ const otherRequest = client.createRequest()({ endpoint: "/other", method: "POST", queued: true });
324
+ const { result } = renderHook(() => useQueue(request));
325
+
326
+ const requestId = add();
327
+ add(otherRequest);
328
+ const calls = await waitForCalls(2);
329
+
330
+ act(() => {
331
+ calls[1].succeed();
332
+ });
333
+ await waitFor(() => {
334
+ expect(client.submitDispatcher.getQueue(otherRequest.queryKey).requests).toHaveLength(0);
335
+ });
336
+
337
+ expect(getIds(result.current.requests)).toStrictEqual([requestId]);
338
+ expect(result.current.requests[0].success).toBeFalse();
339
+ });
340
+ it("should follow the new queue when the request changes", async () => {
341
+ const { request, client, add, waitForCalls } = setup();
342
+ const otherRequest = client.createRequest()({ endpoint: "/other", method: "POST", queued: true });
343
+ const { result, rerender } = renderHook(({ current }: { current: RequestInstance }) => useQueue(current), {
344
+ initialProps: { current: request as RequestInstance },
345
+ });
346
+
347
+ add();
348
+ const otherId = add(otherRequest);
349
+ await waitForCalls(2);
350
+
351
+ rerender({ current: otherRequest });
352
+
353
+ expect(getIds(result.current.requests)).toStrictEqual([otherId]);
354
+ });
355
+ it("should stop listening to the previous queue when the request changes", async () => {
356
+ const { request, client, add, waitForCalls } = setup();
357
+ const otherRequest = client.createRequest()({ endpoint: "/other", method: "POST", queued: true });
358
+ const { result, rerender } = renderHook(({ current }: { current: RequestInstance }) => useQueue(current), {
359
+ initialProps: { current: request as RequestInstance },
360
+ });
361
+
362
+ rerender({ current: otherRequest });
363
+ add();
364
+ add();
365
+ await waitForCalls(1);
366
+
367
+ expect(result.current.requests).toStrictEqual([]);
368
+ });
369
+ });
370
+
371
+ describe("given the dispatcherType option", () => {
372
+ it("should read a GET request from the fetch dispatcher by default", async () => {
373
+ const { request, client, add, waitForCall } = setup({ method: "GET" });
374
+ const { result } = renderHook(() => useQueue(request));
375
+
376
+ const requestId = add();
377
+ await waitForCall(0);
378
+
379
+ expect(result.current.dispatcher).toBe(client.fetchDispatcher);
380
+ expect(getIds(result.current.requests)).toStrictEqual([requestId]);
381
+ });
382
+ it("should read the submit dispatcher when asked to", async () => {
383
+ const { request, client, add, waitForCall } = setup({ method: "GET" });
384
+ const { result } = renderHook(() => useQueue(request, { dispatcherType: "submit" }));
385
+
386
+ add();
387
+ await waitForCall(0);
388
+
389
+ expect(result.current.dispatcher).toBe(client.submitDispatcher);
390
+ expect(result.current.requests).toStrictEqual([]);
391
+ });
392
+ it("should read the fetch dispatcher for a mutation when asked to", async () => {
393
+ const { request, client } = setup();
394
+ const { result } = renderHook(() => useQueue(request, { dispatcherType: "fetch" }));
395
+ let requestId = "";
396
+
397
+ act(() => {
398
+ requestId = client.fetchDispatcher.add(request);
399
+ });
400
+
401
+ expect(result.current.dispatcher).toBe(client.fetchDispatcher);
402
+ expect(getIds(result.current.requests)).toStrictEqual([requestId]);
403
+ });
404
+ it("should take the options from the provider config", async () => {
405
+ const { request, client } = setup({ method: "GET" });
406
+ const wrapper = ({ children }: { children: ReactNode }) => (
407
+ <Provider config={{ useQueueConfig: { dispatcherType: "submit" } }}>{children}</Provider>
408
+ );
409
+
410
+ const { result } = renderHook(() => useQueue(request), { wrapper });
411
+
412
+ expect(result.current.dispatcher).toBe(client.submitDispatcher);
413
+ });
414
+ it("should prefer the hook options over the provider config", async () => {
415
+ const { request, client } = setup({ method: "GET" });
416
+ const wrapper = ({ children }: { children: ReactNode }) => (
417
+ <Provider config={{ useQueueConfig: { dispatcherType: "submit" } }}>{children}</Provider>
418
+ );
419
+
420
+ const { result } = renderHook(() => useQueue(request, { dispatcherType: "fetch" }), { wrapper });
421
+
422
+ expect(result.current.dispatcher).toBe(client.fetchDispatcher);
423
+ });
424
+ });
425
+
426
+ describe("given two hooks watch the same queue", () => {
427
+ it("should show the same list in both", async () => {
428
+ const { request, add, waitForCall } = setup();
429
+ const first = renderHook(() => useQueue(request));
430
+ const second = renderHook(() => useQueue(request));
431
+
432
+ const ids = [add(), add(), add()];
433
+ await waitForCall(0);
434
+
435
+ expect(getIds(first.result.current.requests)).toStrictEqual(ids);
436
+ expect(getIds(second.result.current.requests)).toStrictEqual(ids);
437
+ });
438
+ it("should keep the second hook updated after the first one unmounts", async () => {
439
+ const { request, add, waitForCall } = setup();
440
+ const first = renderHook(() => useQueue(request));
441
+ const second = renderHook(() => useQueue(request));
442
+
443
+ first.unmount();
444
+ const ids = [add(), add()];
445
+ await waitForCall(0);
446
+
447
+ expect(getIds(second.result.current.requests)).toStrictEqual(ids);
448
+ });
449
+ it("should show a hook mounted later the current queue", async () => {
450
+ const { request, add, waitForCall } = setup();
451
+ const first = renderHook(() => useQueue(request));
452
+
453
+ const ids = [add(), add()];
454
+ const call = await waitForCall(0);
455
+ act(() => {
456
+ call.upload(5, 10);
457
+ });
458
+
459
+ const second = renderHook(() => useQueue(request));
460
+
461
+ expect(getIds(second.result.current.requests)).toStrictEqual(ids);
462
+ expect(getIds(first.result.current.requests)).toStrictEqual(ids);
463
+ });
464
+ });
465
+
466
+ describe("given the hook unmounts and mounts again", () => {
467
+ it("should pick up the requests queued in the meantime", async () => {
468
+ const { request, add, waitForCall } = setup();
469
+ const first = renderHook(() => useQueue(request));
470
+ const firstId = add();
471
+ await waitForCall(0);
472
+ first.unmount();
473
+
474
+ const secondId = add();
475
+ const second = renderHook(() => useQueue(request));
476
+
477
+ expect(getIds(second.result.current.requests)).toStrictEqual([firstId, secondId]);
478
+ });
479
+ it("should not list requests that finished while it was unmounted", async () => {
480
+ const { request, add, waitForCall, client } = setup();
481
+ const first = renderHook(() => useQueue(request));
482
+ add();
483
+ const secondId = add();
484
+ const call = await waitForCall(0);
485
+ first.unmount();
486
+
487
+ call.succeed();
488
+ await waitForCall(1);
489
+ await waitFor(() => {
490
+ expect(client.submitDispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
491
+ });
492
+
493
+ const second = renderHook(() => useQueue(request));
494
+
495
+ expect(getIds(second.result.current.requests)).toStrictEqual([secondId]);
496
+ });
497
+ it("should not update or warn after the unmount", async () => {
498
+ const errorSpy = vi.spyOn(console, "error").mockImplementation(() => null);
499
+ const { request, add, waitForCall, client } = setup();
500
+ const view = renderHook(() => useQueue(request));
501
+ add();
502
+ add();
503
+ const call = await waitForCall(0);
504
+ const beforeUnmount = view.result.current.requests;
505
+
506
+ view.unmount();
507
+ call.upload(1, 2);
508
+ call.succeed();
509
+ await waitForCall(1);
510
+ await waitFor(() => {
511
+ expect(client.submitDispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
512
+ });
513
+
514
+ expect(view.result.current.requests).toBe(beforeUnmount);
515
+ expect(errorSpy).not.toHaveBeenCalled();
516
+ errorSpy.mockRestore();
517
+ });
518
+ });
519
+
520
+ describe("given the hook is rendered in StrictMode", () => {
521
+ it("should list the queued requests", async () => {
522
+ const { request, add, waitForCall } = setup();
523
+ const { result } = renderHook(() => useQueue(request), { wrapper: StrictMode });
524
+
525
+ const ids = [add(), add()];
526
+ await waitForCall(0);
527
+
528
+ expect(getIds(result.current.requests)).toStrictEqual(ids);
529
+ });
530
+ it("should keep following the queue after the double mount", async () => {
531
+ const { request, add, waitForCall } = setup();
532
+ const { result } = renderHook(() => useQueue(request), { wrapper: StrictMode });
533
+
534
+ add();
535
+ const second = add();
536
+ const call = await waitForCall(0);
537
+ act(() => {
538
+ call.succeed();
539
+ });
540
+
541
+ await waitFor(() => {
542
+ expect(getIds(result.current.requests)).toStrictEqual([second]);
543
+ });
544
+ });
545
+ it("should subscribe once", async () => {
546
+ const plain = setup();
547
+ const strict = setup();
548
+ const count = (emitter: {
549
+ eventNames: () => (string | symbol)[];
550
+ listenerCount: (name: string | symbol) => number;
551
+ }) => emitter.eventNames().reduce((sum, name) => sum + emitter.listenerCount(name), 0);
552
+
553
+ renderHook(() => useQueue(plain.request));
554
+ renderHook(() => useQueue(strict.request), { wrapper: StrictMode });
555
+
556
+ expect(count(strict.client.requestManager.emitter)).toBe(count(plain.client.requestManager.emitter));
557
+ expect(count(strict.client.submitDispatcher.emitter)).toBe(count(plain.client.submitDispatcher.emitter));
558
+ });
559
+ });
560
+ });