@hyper-fetch/react 9.6.0 → 9.6.1
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/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.refresh.spec.ts +36 -13
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +11 -2
- package/__tests__/features/use-is-fetching/use-is-fetching.base.spec.tsx +19 -9
- 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.optimistic-settlement.spec.ts +2 -1
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +51 -35
- package/__tests__/features/use-submit/use-submit.unmount.spec.ts +2 -1
- 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/index.mjs +3 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +6 -4
|
@@ -45,7 +45,8 @@ describe("useSubmit [ Concurrency ]", () => {
|
|
|
45
45
|
await testSuccessState(mock, responseTwo);
|
|
46
46
|
});
|
|
47
47
|
it("should start in loading mode when another request is ongoing", async () => {
|
|
48
|
-
|
|
48
|
+
// The default 20ms mock can resolve during the render pause below, so keep the request in flight explicitly
|
|
49
|
+
mockRequest(request, { delay: 1000 });
|
|
49
50
|
const responseOne = renderUseSubmit(request);
|
|
50
51
|
|
|
51
52
|
act(() => {
|
|
@@ -12,11 +12,42 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
12
12
|
|
|
13
13
|
let request = createRequest<null, null>({ method: "POST" });
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Bouncing is decided by elapsed time, so the timing specs run on a fake clock. With real timers a stall between two
|
|
17
|
+
* submits moves them into separate bounce windows and the request count changes.
|
|
18
|
+
*/
|
|
19
|
+
const useFakeClock = () => {
|
|
20
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
|
|
21
|
+
};
|
|
22
|
+
const advance = (ms: number) => act(() => vi.advanceTimersByTimeAsync(ms));
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Submits four times, 10ms apart, all inside one bounce window
|
|
26
|
+
*/
|
|
27
|
+
const submitFourTimes = async (submit: () => unknown) => {
|
|
28
|
+
act(() => {
|
|
29
|
+
submit();
|
|
30
|
+
});
|
|
31
|
+
await advance(10);
|
|
32
|
+
act(() => {
|
|
33
|
+
submit();
|
|
34
|
+
});
|
|
35
|
+
await advance(10);
|
|
36
|
+
act(() => {
|
|
37
|
+
submit();
|
|
38
|
+
});
|
|
39
|
+
await advance(10);
|
|
40
|
+
act(() => {
|
|
41
|
+
submit();
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
15
45
|
beforeAll(() => {
|
|
16
46
|
startServer();
|
|
17
47
|
});
|
|
18
48
|
|
|
19
49
|
afterEach(() => {
|
|
50
|
+
vi.useRealTimers();
|
|
20
51
|
resetMocks();
|
|
21
52
|
});
|
|
22
53
|
|
|
@@ -33,52 +64,41 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
33
64
|
describe("given debounce is active", () => {
|
|
34
65
|
describe("when request is about to change", () => {
|
|
35
66
|
it("should debounce single request", async () => {
|
|
36
|
-
|
|
37
|
-
|
|
67
|
+
useFakeClock();
|
|
68
|
+
const spy = vi.fn();
|
|
38
69
|
mockRequest(request);
|
|
39
70
|
const response = renderUseSubmit(request, hookDebounceOptions);
|
|
40
71
|
|
|
41
72
|
act(() => {
|
|
42
|
-
response.result.current.onSubmitRequestStart(
|
|
43
|
-
startTime = Date.now();
|
|
44
|
-
});
|
|
45
|
-
submitTime = Date.now();
|
|
73
|
+
response.result.current.onSubmitRequestStart(spy);
|
|
46
74
|
response.result.current.submit();
|
|
47
75
|
});
|
|
48
76
|
|
|
49
|
-
await
|
|
50
|
-
|
|
51
|
-
});
|
|
77
|
+
await advance(hookDebounceOptions.bounceTime - 1);
|
|
78
|
+
expect(spy).not.toHaveBeenCalled();
|
|
52
79
|
|
|
53
|
-
|
|
80
|
+
await advance(1);
|
|
81
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
54
82
|
});
|
|
55
83
|
it("should debounce multiple request triggers by bounceTime", async () => {
|
|
84
|
+
useFakeClock();
|
|
56
85
|
const spy = vi.fn();
|
|
57
|
-
let submitTime: number | null = null;
|
|
58
|
-
let startTime: number | null = null;
|
|
59
86
|
mockRequest(request);
|
|
60
87
|
const response = renderUseSubmit(request, hookDebounceOptions);
|
|
61
88
|
|
|
62
|
-
|
|
63
|
-
response.result.current.onSubmitRequestStart(
|
|
64
|
-
spy();
|
|
65
|
-
startTime = Date.now();
|
|
66
|
-
});
|
|
67
|
-
submitTime = Date.now();
|
|
68
|
-
response.result.current.submit();
|
|
69
|
-
await waitForRender(10);
|
|
70
|
-
response.result.current.submit();
|
|
71
|
-
await waitForRender(10);
|
|
72
|
-
response.result.current.submit();
|
|
73
|
-
await waitForRender(10);
|
|
74
|
-
response.result.current.submit();
|
|
89
|
+
act(() => {
|
|
90
|
+
response.result.current.onSubmitRequestStart(spy);
|
|
75
91
|
});
|
|
92
|
+
await submitFourTimes(response.result.current.submit);
|
|
76
93
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
94
|
+
// The window restarts with every submit, so it closes bounceTime after the last one
|
|
95
|
+
await advance(hookDebounceOptions.bounceTime - 1);
|
|
96
|
+
expect(spy).not.toHaveBeenCalled();
|
|
80
97
|
|
|
81
|
-
|
|
98
|
+
await advance(1);
|
|
99
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
100
|
+
|
|
101
|
+
await advance(1000);
|
|
82
102
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
83
103
|
});
|
|
84
104
|
it("should resolve debounced methods", async () => {
|
|
@@ -106,39 +126,24 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
106
126
|
expect(value).toHaveLength(4);
|
|
107
127
|
});
|
|
108
128
|
it("should change debounce time", async () => {
|
|
129
|
+
useFakeClock();
|
|
109
130
|
const newBounceTime = 200;
|
|
110
131
|
const spy = vi.fn();
|
|
111
|
-
let submitTime: number | null = null;
|
|
112
|
-
let startTime: number | null = null;
|
|
113
132
|
mockRequest(request);
|
|
114
133
|
const response = renderUseSubmit(request, hookDebounceOptions);
|
|
115
134
|
|
|
116
|
-
|
|
117
|
-
|
|
135
|
+
act(() => {
|
|
136
|
+
response.rerender({ bounceTime: newBounceTime });
|
|
118
137
|
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
await act(async () => {
|
|
123
|
-
response.result.current.onSubmitRequestStart(() => {
|
|
124
|
-
spy();
|
|
125
|
-
startTime = Date.now();
|
|
126
|
-
});
|
|
127
|
-
submitTime = Date.now();
|
|
128
|
-
response.result.current.submit();
|
|
129
|
-
await waitForRender(10);
|
|
130
|
-
response.result.current.submit();
|
|
131
|
-
await waitForRender(10);
|
|
132
|
-
response.result.current.submit();
|
|
133
|
-
await waitForRender(10);
|
|
134
|
-
response.result.current.submit();
|
|
138
|
+
act(() => {
|
|
139
|
+
response.result.current.onSubmitRequestStart(spy);
|
|
135
140
|
});
|
|
141
|
+
await submitFourTimes(response.result.current.submit);
|
|
136
142
|
|
|
137
|
-
await
|
|
138
|
-
|
|
139
|
-
});
|
|
143
|
+
await advance(newBounceTime - 1);
|
|
144
|
+
expect(spy).not.toHaveBeenCalled();
|
|
140
145
|
|
|
141
|
-
|
|
146
|
+
await advance(1);
|
|
142
147
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
143
148
|
});
|
|
144
149
|
});
|
|
@@ -148,15 +153,11 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
148
153
|
describe("when request is about to change", () => {
|
|
149
154
|
it("should not debounce multiple request triggers", async () => {
|
|
150
155
|
const spy = vi.fn();
|
|
151
|
-
let startTime: number | null = null;
|
|
152
156
|
mockRequest(request, { delay: 0 });
|
|
153
157
|
const response = renderUseSubmit(request);
|
|
154
158
|
|
|
155
159
|
await act(async () => {
|
|
156
|
-
response.result.current.onSubmitRequestStart(
|
|
157
|
-
spy();
|
|
158
|
-
startTime = Date.now();
|
|
159
|
-
});
|
|
160
|
+
response.result.current.onSubmitRequestStart(spy);
|
|
160
161
|
response.result.current.submit();
|
|
161
162
|
await waitForRender(10);
|
|
162
163
|
response.result.current.submit();
|
|
@@ -167,10 +168,8 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
167
168
|
});
|
|
168
169
|
|
|
169
170
|
await waitFor(() => {
|
|
170
|
-
expect(
|
|
171
|
+
expect(spy).toHaveBeenCalledTimes(4);
|
|
171
172
|
});
|
|
172
|
-
|
|
173
|
-
expect(spy).toHaveBeenCalledTimes(4);
|
|
174
173
|
});
|
|
175
174
|
});
|
|
176
175
|
});
|
|
@@ -178,52 +177,48 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
178
177
|
describe("given throttle is active", () => {
|
|
179
178
|
describe("when request is about to change", () => {
|
|
180
179
|
it("should throttle single request", async () => {
|
|
181
|
-
|
|
182
|
-
|
|
180
|
+
useFakeClock();
|
|
181
|
+
const spy = vi.fn();
|
|
183
182
|
mockRequest(request);
|
|
184
183
|
const response = renderUseSubmit(request, hookThrottleOptions);
|
|
185
184
|
|
|
186
185
|
act(() => {
|
|
187
|
-
response.result.current.onSubmitRequestStart(
|
|
188
|
-
startTime = Date.now();
|
|
189
|
-
});
|
|
190
|
-
submitTime = Date.now();
|
|
186
|
+
response.result.current.onSubmitRequestStart(spy);
|
|
191
187
|
response.result.current.submit();
|
|
192
188
|
response.result.current.submit();
|
|
193
189
|
});
|
|
194
190
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
191
|
+
// Nothing was throttled yet, so the first submit is sent right away and the second waits for the timeout
|
|
192
|
+
await advance(0);
|
|
193
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
194
|
+
|
|
195
|
+
await advance(hookThrottleOptions.bounceTimeout - 1);
|
|
196
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
197
|
+
|
|
198
|
+
await advance(1);
|
|
199
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
199
200
|
});
|
|
200
201
|
it("should throttle multiple request triggers by bounceTime", async () => {
|
|
202
|
+
useFakeClock();
|
|
201
203
|
const spy = vi.fn();
|
|
202
|
-
let submitTime: number | null = null;
|
|
203
|
-
let startTime: number | null = null;
|
|
204
204
|
mockRequest(request);
|
|
205
205
|
const response = renderUseSubmit(request, hookThrottleOptions);
|
|
206
206
|
|
|
207
|
-
|
|
208
|
-
response.result.current.onSubmitRequestStart(
|
|
209
|
-
spy();
|
|
210
|
-
startTime = Date.now();
|
|
211
|
-
});
|
|
212
|
-
submitTime = Date.now();
|
|
213
|
-
response.result.current.submit();
|
|
214
|
-
await waitForRender(10);
|
|
215
|
-
response.result.current.submit();
|
|
216
|
-
await waitForRender(10);
|
|
217
|
-
response.result.current.submit();
|
|
218
|
-
await waitForRender(10);
|
|
219
|
-
response.result.current.submit();
|
|
207
|
+
act(() => {
|
|
208
|
+
response.result.current.onSubmitRequestStart(spy);
|
|
220
209
|
});
|
|
210
|
+
await submitFourTimes(response.result.current.submit);
|
|
211
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
221
212
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
213
|
+
// Only the last throttled submit is sent, bounceTimeout after it
|
|
214
|
+
await advance(hookThrottleOptions.bounceTimeout - 1);
|
|
215
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
216
|
+
|
|
217
|
+
await advance(1);
|
|
218
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
219
|
+
|
|
220
|
+
await advance(1000);
|
|
221
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
227
222
|
});
|
|
228
223
|
it("should resolve throttled methods", async () => {
|
|
229
224
|
mockRequest(request);
|
|
@@ -250,39 +245,26 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
250
245
|
expect(value).toHaveLength(4);
|
|
251
246
|
});
|
|
252
247
|
it("should change throttle time", async () => {
|
|
248
|
+
useFakeClock();
|
|
253
249
|
const newBounceTime = 200;
|
|
254
250
|
const spy = vi.fn();
|
|
255
|
-
let submitTime: number | null = null;
|
|
256
|
-
let startTime: number | null = null;
|
|
257
251
|
mockRequest(request);
|
|
258
252
|
const response = renderUseSubmit(request, hookThrottleOptions);
|
|
259
253
|
|
|
260
|
-
|
|
261
|
-
|
|
254
|
+
act(() => {
|
|
255
|
+
response.rerender({ bounceTime: newBounceTime, bounceTimeout: newBounceTime });
|
|
262
256
|
});
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
await act(async () => {
|
|
267
|
-
response.result.current.onSubmitRequestStart(() => {
|
|
268
|
-
spy();
|
|
269
|
-
startTime = Date.now();
|
|
270
|
-
});
|
|
271
|
-
submitTime = Date.now();
|
|
272
|
-
response.result.current.submit();
|
|
273
|
-
await waitForRender(10);
|
|
274
|
-
response.result.current.submit();
|
|
275
|
-
await waitForRender(10);
|
|
276
|
-
response.result.current.submit();
|
|
277
|
-
await waitForRender(10);
|
|
278
|
-
response.result.current.submit();
|
|
257
|
+
act(() => {
|
|
258
|
+
response.result.current.onSubmitRequestStart(spy);
|
|
279
259
|
});
|
|
260
|
+
await submitFourTimes(response.result.current.submit);
|
|
261
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
280
262
|
|
|
281
|
-
await
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
263
|
+
await advance(newBounceTime - 1);
|
|
264
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
265
|
+
|
|
266
|
+
await advance(1);
|
|
267
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
286
268
|
});
|
|
287
269
|
});
|
|
288
270
|
});
|
|
@@ -291,15 +273,11 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
291
273
|
describe("when request is about to change", () => {
|
|
292
274
|
it("should not throttle multiple request triggers", async () => {
|
|
293
275
|
const spy = vi.fn();
|
|
294
|
-
let startTime: number | null = null;
|
|
295
276
|
mockRequest(request, { delay: 0 });
|
|
296
277
|
const response = renderUseSubmit(request);
|
|
297
278
|
|
|
298
279
|
await act(async () => {
|
|
299
|
-
response.result.current.onSubmitRequestStart(
|
|
300
|
-
spy();
|
|
301
|
-
startTime = Date.now();
|
|
302
|
-
});
|
|
280
|
+
response.result.current.onSubmitRequestStart(spy);
|
|
303
281
|
response.result.current.submit();
|
|
304
282
|
await waitForRender(10);
|
|
305
283
|
response.result.current.submit();
|
|
@@ -310,10 +288,8 @@ describe("useSubmit [ Bounce ]", () => {
|
|
|
310
288
|
});
|
|
311
289
|
|
|
312
290
|
await waitFor(() => {
|
|
313
|
-
expect(
|
|
291
|
+
expect(spy).toHaveBeenCalledTimes(4);
|
|
314
292
|
});
|
|
315
|
-
|
|
316
|
-
expect(spy).toHaveBeenCalledTimes(4);
|
|
317
293
|
});
|
|
318
294
|
});
|
|
319
295
|
});
|
|
@@ -433,7 +433,8 @@ describe("useSubmit [ Optimistic settlement ]", () => {
|
|
|
433
433
|
|
|
434
434
|
it("should roll back aborted request", async () => {
|
|
435
435
|
const setup = createSetup();
|
|
436
|
-
|
|
436
|
+
// waitFor polls every 50ms, so the request has to stay in flight well past that to be aborted, not resolved
|
|
437
|
+
mockRequest(setup.mutation, { delay: 1000 });
|
|
437
438
|
const { result, unmount } = renderHook(() => useSubmit(setup.mutation));
|
|
438
439
|
|
|
439
440
|
const pending = startSubmit(result.current.submit);
|
|
@@ -1,18 +1,42 @@
|
|
|
1
|
+
import type { RequestInstance } from "@hyper-fetch/core";
|
|
1
2
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
3
|
import { act, waitFor } from "@testing-library/react";
|
|
3
4
|
|
|
4
|
-
import { client, createRequest, renderUseSubmit
|
|
5
|
+
import { client, createRequest, renderUseSubmit } from "../../utils";
|
|
5
6
|
|
|
6
7
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
7
8
|
|
|
8
9
|
describe("useSubmit [ Retry ]", () => {
|
|
9
10
|
let request = createRequest<{ response: null; payload: null }>({ method: "POST" });
|
|
10
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Submits and resolves once the last attempt has responded, so the attempt count is final.
|
|
14
|
+
* Waiting for a fixed time instead loses attempts on a slow machine.
|
|
15
|
+
*/
|
|
16
|
+
const submitUntilSettled = async (retryRequest: RequestInstance) => {
|
|
17
|
+
const onStart = vi.fn();
|
|
18
|
+
const onFinished = vi.fn();
|
|
19
|
+
const response = renderUseSubmit(retryRequest);
|
|
20
|
+
|
|
21
|
+
act(() => {
|
|
22
|
+
response.result.current.onSubmitRequestStart(onStart);
|
|
23
|
+
response.result.current.onSubmitFinished(({ details }) => onFinished(details.willRetry));
|
|
24
|
+
response.result.current.submit();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
await waitFor(() => {
|
|
28
|
+
expect(onFinished).toHaveBeenLastCalledWith(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return { onStart, onFinished };
|
|
32
|
+
};
|
|
33
|
+
|
|
11
34
|
beforeAll(() => {
|
|
12
35
|
startServer();
|
|
13
36
|
});
|
|
14
37
|
|
|
15
38
|
afterEach(() => {
|
|
39
|
+
vi.useRealTimers();
|
|
16
40
|
resetMocks();
|
|
17
41
|
});
|
|
18
42
|
|
|
@@ -28,65 +52,57 @@ describe("useSubmit [ Retry ]", () => {
|
|
|
28
52
|
|
|
29
53
|
describe("when request retry attribute is set to false", () => {
|
|
30
54
|
it("should not retry request on failure", async () => {
|
|
31
|
-
const spy = vi.fn();
|
|
32
55
|
mockRequest(request, { status: 400, delay: 5 });
|
|
33
|
-
const response = renderUseSubmit(request.setRetry(0).setRetryTime(0));
|
|
34
56
|
|
|
35
|
-
|
|
36
|
-
response.result.current.onSubmitRequestStart(spy);
|
|
37
|
-
response.result.current.submit();
|
|
38
|
-
});
|
|
57
|
+
const { onStart, onFinished } = await submitUntilSettled(request.setRetry(0).setRetryTime(0));
|
|
39
58
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
expect(spy).toHaveBeenCalledTimes(1);
|
|
59
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
60
|
+
expect(onFinished).toHaveBeenCalledTimes(1);
|
|
43
61
|
});
|
|
44
62
|
});
|
|
45
63
|
describe("when request retry attribute is set to true", () => {
|
|
46
64
|
it("should retry request once", async () => {
|
|
47
|
-
const spy = vi.fn();
|
|
48
65
|
mockRequest(request, { status: 400, delay: 5 });
|
|
49
|
-
const response = renderUseSubmit(request.setRetry(1).setRetryTime(10));
|
|
50
|
-
|
|
51
|
-
act(() => {
|
|
52
|
-
response.result.current.onSubmitRequestStart(spy);
|
|
53
|
-
response.result.current.submit();
|
|
54
|
-
});
|
|
55
66
|
|
|
56
|
-
await
|
|
67
|
+
const { onStart, onFinished } = await submitUntilSettled(request.setRetry(1).setRetryTime(10));
|
|
57
68
|
|
|
58
|
-
expect(
|
|
69
|
+
expect(onStart).toHaveBeenCalledTimes(2);
|
|
70
|
+
expect(onFinished.mock.calls).toStrictEqual([[true], [false]]);
|
|
59
71
|
});
|
|
60
72
|
it("should retry request twice", async () => {
|
|
61
|
-
const spy = vi.fn();
|
|
62
73
|
mockRequest(request, { status: 400, delay: 5 });
|
|
63
|
-
const response = renderUseSubmit(request.setRetry(2).setRetryTime(10));
|
|
64
|
-
|
|
65
|
-
act(() => {
|
|
66
|
-
response.result.current.onSubmitRequestStart(spy);
|
|
67
|
-
response.result.current.submit();
|
|
68
|
-
});
|
|
69
74
|
|
|
70
|
-
await
|
|
75
|
+
const { onStart, onFinished } = await submitUntilSettled(request.setRetry(2).setRetryTime(10));
|
|
71
76
|
|
|
72
|
-
expect(
|
|
77
|
+
expect(onStart).toHaveBeenCalledTimes(3);
|
|
78
|
+
expect(onFinished.mock.calls).toStrictEqual([[true], [true], [false]]);
|
|
73
79
|
});
|
|
74
80
|
it("should trigger retries with the config interval", async () => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const
|
|
81
|
+
// Node can fire a real timer earlier than Date.now() says, so the interval is measured on a fake clock
|
|
82
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
|
|
83
|
+
const retryTime = 100;
|
|
84
|
+
const startTimes: number[] = [];
|
|
85
|
+
const finishTimes: number[] = [];
|
|
86
|
+
mockRequest(request, { status: 400, delay: 5 });
|
|
87
|
+
const response = renderUseSubmit(request.setRetry(1).setRetryTime(retryTime));
|
|
78
88
|
|
|
79
89
|
act(() => {
|
|
80
90
|
response.result.current.onSubmitRequestStart(() => {
|
|
81
|
-
|
|
91
|
+
startTimes.push(Date.now());
|
|
92
|
+
});
|
|
93
|
+
response.result.current.onSubmitFinished(() => {
|
|
94
|
+
finishTimes.push(Date.now());
|
|
82
95
|
});
|
|
83
96
|
response.result.current.submit();
|
|
84
97
|
});
|
|
85
98
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
99
|
+
// vi.waitFor polls on real timers - the RTL waitFor would wait on the faked setTimeout forever
|
|
100
|
+
await vi.waitFor(async () => {
|
|
101
|
+
await act(() => vi.advanceTimersByTimeAsync(20));
|
|
102
|
+
expect(startTimes).toHaveLength(2);
|
|
89
103
|
});
|
|
104
|
+
|
|
105
|
+
expect(startTimes[1] - finishTimes[0]).toBeGreaterThanOrEqual(retryTime);
|
|
90
106
|
});
|
|
91
107
|
});
|
|
92
108
|
});
|
|
@@ -199,7 +199,8 @@ describe("useSubmit [ Unmount ]", () => {
|
|
|
199
199
|
|
|
200
200
|
it("should roll back once and fire no callback after abort", async () => {
|
|
201
201
|
const setup = createSetup();
|
|
202
|
-
|
|
202
|
+
// Two polling waitFors run before the abort, so the request has to stay in flight far longer than their 50ms
|
|
203
|
+
mockRequest(setup.mutation, { data: { ok: true }, delay: 1000 });
|
|
203
204
|
|
|
204
205
|
const { requestId, callbacks, pending } = await submitAndUnmount(setup);
|
|
205
206
|
await waitFor(() => {
|
|
@@ -33,8 +33,10 @@ export const emitDownloadProgress = <T extends RequestInstance>(
|
|
|
33
33
|
},
|
|
34
34
|
) => {
|
|
35
35
|
const values = options || { total: 50, loaded: 25 };
|
|
36
|
-
|
|
37
|
-
const
|
|
36
|
+
// One clock read so the elapsed time is exactly 20s, even when the clock ticks in between
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
const startTimestamp = new Date(now - 20_000);
|
|
39
|
+
const progressTimestamp = new Date(now);
|
|
38
40
|
|
|
39
41
|
const progress = getProgressData(startTimestamp, progressTimestamp, values);
|
|
40
42
|
request.client.requestManager.events.emitDownloadProgress({
|
|
@@ -55,8 +57,10 @@ export const emitUploadProgress = <T extends RequestInstance>(
|
|
|
55
57
|
},
|
|
56
58
|
) => {
|
|
57
59
|
const values = options || { total: 50, loaded: 25 };
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
+
// One clock read so the elapsed time is exactly 20s, even when the clock ticks in between
|
|
61
|
+
const now = Date.now();
|
|
62
|
+
const startTimestamp = new Date(now - 20_000);
|
|
63
|
+
const progressTimestamp = new Date(now);
|
|
60
64
|
|
|
61
65
|
const progress = getProgressData(startTimestamp, progressTimestamp, values);
|
|
62
66
|
request.client.requestManager.events.emitUploadProgress({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-request-events.hooks.d.ts","sourceRoot":"","sources":["../../../src/helpers/use-request-events/use-request-events.hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,eAAe,EAQhB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAOV,0BAA0B,EAC1B,yBAAyB,EAE1B,MAAM,SAAS,CAAC;AAIjB;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,eAAe,EAAE,8EAOzD,yBAAyB,CAAC,CAAC,CAAC,KAAG,0BAA0B,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"use-request-events.hooks.d.ts","sourceRoot":"","sources":["../../../src/helpers/use-request-events/use-request-events.hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,eAAe,EAQhB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAOV,0BAA0B,EAC1B,yBAAyB,EAE1B,MAAM,SAAS,CAAC;AAIjB;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,eAAe,EAAE,8EAOzD,yBAAyB,CAAC,CAAC,CAAC,KAAG,0BAA0B,CAAC,CAAC,CA6T7D,CAAC"}
|
package/dist/index.mjs
CHANGED
|
@@ -237,9 +237,11 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
237
237
|
return (values) => {
|
|
238
238
|
const data = runResponseMapper(request, values.response);
|
|
239
239
|
if (data instanceof Promise) return (async () => {
|
|
240
|
+
const response = await data;
|
|
241
|
+
if (!isMounted.current) return;
|
|
240
242
|
handleResponseCallbacks({
|
|
241
243
|
...values,
|
|
242
|
-
response
|
|
244
|
+
response
|
|
243
245
|
});
|
|
244
246
|
})();
|
|
245
247
|
return handleResponseCallbacks(values);
|