@hyper-fetch/react 9.5.0 → 9.5.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.mapper-failure.spec.ts +215 -0
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +9 -3
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +9 -3
- package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +625 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +12 -4
- package/dist/helpers/use-request-events/use-request-events.hooks.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.map +1 -1
- package/dist/index.mjs +25 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
- package/src/helpers/use-request-events/use-request-events.hooks.ts +11 -33
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +3 -5
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +4 -5
package/__tests__/features/helpers/use-request-events/use-request-events.mapper-failure.spec.ts
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { OptimisticRollbackArgs } from "@hyper-fetch/core";
|
|
2
|
+
import { Client } from "@hyper-fetch/core";
|
|
3
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
4
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
5
|
+
import { useFetch } from "hooks/use-fetch";
|
|
6
|
+
import { useSubmit } from "hooks/use-submit";
|
|
7
|
+
import { StrictMode } from "react";
|
|
8
|
+
|
|
9
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
10
|
+
|
|
11
|
+
const MAPPER_ERROR = new Error("Mapper failed");
|
|
12
|
+
|
|
13
|
+
// Failing mappers keep the response type, otherwise the mapped request would be typed with `never`
|
|
14
|
+
type FailingMapper = <Response>(response: Response) => Response | Promise<Response>;
|
|
15
|
+
|
|
16
|
+
const rejectingMapper: FailingMapper = async () => {
|
|
17
|
+
throw MAPPER_ERROR;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const throwingMapper: FailingMapper = () => {
|
|
21
|
+
throw MAPPER_ERROR;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const failures = [
|
|
25
|
+
{ name: "rejects", mapper: rejectingMapper },
|
|
26
|
+
{ name: "throws synchronously", mapper: throwingMapper },
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const createSetup = () => {
|
|
30
|
+
const client = new Client({ url: "shared-base-url" });
|
|
31
|
+
const logError = vi.spyOn(client.logger, "error");
|
|
32
|
+
const rollback = vi.fn((_args: OptimisticRollbackArgs<any>) => undefined);
|
|
33
|
+
|
|
34
|
+
const fetchRequest = client.createRequest<{ response: { ok: boolean } }>()({ endpoint: "/fetch" });
|
|
35
|
+
const submitRequest = client
|
|
36
|
+
.createRequest<{ response: { ok: boolean } }>()({ endpoint: "/submit", method: "POST" })
|
|
37
|
+
.setOptimistic(() => ({ rollback }));
|
|
38
|
+
|
|
39
|
+
return { client, logError, rollback, fetchRequest, submitRequest };
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe("useRequestEvents [ Response mapper failure ]", () => {
|
|
43
|
+
beforeAll(() => {
|
|
44
|
+
startServer();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
resetMocks();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
afterAll(() => {
|
|
52
|
+
stopServer();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe.each(failures)("when the mapper $name", ({ mapper }) => {
|
|
56
|
+
it("should call error callbacks of useSubmit with the mapper error", async () => {
|
|
57
|
+
const { submitRequest } = createSetup();
|
|
58
|
+
const request = submitRequest.setResponseMapper(mapper);
|
|
59
|
+
mockRequest(request, { data: { ok: true } });
|
|
60
|
+
const onSuccess = vi.fn();
|
|
61
|
+
const onError = vi.fn();
|
|
62
|
+
const onFinished = vi.fn();
|
|
63
|
+
|
|
64
|
+
const { result } = renderHook(() => useSubmit(request));
|
|
65
|
+
act(() => {
|
|
66
|
+
result.current.onSubmitSuccess(onSuccess);
|
|
67
|
+
result.current.onSubmitError(onError);
|
|
68
|
+
result.current.onSubmitFinished(onFinished);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
let response: Awaited<ReturnType<typeof result.current.submit>> | undefined;
|
|
72
|
+
await act(async () => {
|
|
73
|
+
response = await result.current.submit();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
await waitFor(() => {
|
|
77
|
+
expect(onError).toHaveBeenCalledTimes(1);
|
|
78
|
+
});
|
|
79
|
+
expect(onError.mock.calls[0][0].response.error).toBe(MAPPER_ERROR);
|
|
80
|
+
expect(onError.mock.calls[0][0].response.success).toBe(false);
|
|
81
|
+
expect(onFinished).toHaveBeenCalledTimes(1);
|
|
82
|
+
expect(onSuccess).not.toHaveBeenCalled();
|
|
83
|
+
expect(response?.error).toBe(MAPPER_ERROR);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should roll back useSubmit optimistic change once", async () => {
|
|
87
|
+
const { submitRequest, rollback } = createSetup();
|
|
88
|
+
const request = submitRequest.setResponseMapper(mapper);
|
|
89
|
+
mockRequest(request, { data: { ok: true } });
|
|
90
|
+
|
|
91
|
+
const { result } = renderHook(() => useSubmit(request));
|
|
92
|
+
await act(async () => {
|
|
93
|
+
await result.current.submit();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(rollback).toHaveBeenCalledTimes(1);
|
|
97
|
+
expect(rollback.mock.calls[0][0]).toMatchObject({ reason: "error" });
|
|
98
|
+
expect(rollback.mock.calls[0][0].response?.error).toBe(MAPPER_ERROR);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should roll back and call error callback once in StrictMode", async () => {
|
|
102
|
+
const { submitRequest, rollback } = createSetup();
|
|
103
|
+
const request = submitRequest.setResponseMapper(mapper);
|
|
104
|
+
mockRequest(request, { data: { ok: true } });
|
|
105
|
+
const onError = vi.fn();
|
|
106
|
+
|
|
107
|
+
const { result } = renderHook(() => useSubmit(request), { wrapper: StrictMode });
|
|
108
|
+
act(() => {
|
|
109
|
+
result.current.onSubmitError(onError);
|
|
110
|
+
});
|
|
111
|
+
await act(async () => {
|
|
112
|
+
await result.current.submit();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
await waitFor(() => {
|
|
116
|
+
expect(onError).toHaveBeenCalledTimes(1);
|
|
117
|
+
});
|
|
118
|
+
expect(rollback).toHaveBeenCalledTimes(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should roll back without calling callbacks when unmounted while pending", async () => {
|
|
122
|
+
const { client, submitRequest, rollback } = createSetup();
|
|
123
|
+
const request = submitRequest.setResponseMapper(mapper);
|
|
124
|
+
mockRequest(request, { data: { ok: true }, delay: 30 });
|
|
125
|
+
const onError = vi.fn();
|
|
126
|
+
|
|
127
|
+
const { result, unmount } = renderHook(() => useSubmit(request));
|
|
128
|
+
act(() => {
|
|
129
|
+
result.current.onSubmitError(onError);
|
|
130
|
+
});
|
|
131
|
+
// Hook listeners are attached right before the request starts
|
|
132
|
+
const unmountOnStart = client.requestManager.events.onRequestStart(() => {
|
|
133
|
+
unmountOnStart();
|
|
134
|
+
unmount();
|
|
135
|
+
});
|
|
136
|
+
let pending: ReturnType<typeof result.current.submit> | undefined;
|
|
137
|
+
act(() => {
|
|
138
|
+
pending = result.current.submit();
|
|
139
|
+
});
|
|
140
|
+
const response = await pending;
|
|
141
|
+
|
|
142
|
+
expect(response?.error).toBe(MAPPER_ERROR);
|
|
143
|
+
expect(rollback).toHaveBeenCalledTimes(1);
|
|
144
|
+
expect(onError).not.toHaveBeenCalled();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("should call error callbacks of useFetch with the mapper error", async () => {
|
|
148
|
+
const { fetchRequest, logError } = createSetup();
|
|
149
|
+
const request = fetchRequest.setResponseMapper(mapper);
|
|
150
|
+
mockRequest(request, { data: { ok: true } });
|
|
151
|
+
const onSuccess = vi.fn();
|
|
152
|
+
const onError = vi.fn();
|
|
153
|
+
const onFinished = vi.fn();
|
|
154
|
+
|
|
155
|
+
const { result } = renderHook(() => useFetch(request));
|
|
156
|
+
act(() => {
|
|
157
|
+
result.current.onSuccess(onSuccess);
|
|
158
|
+
result.current.onError(onError);
|
|
159
|
+
result.current.onFinished(onFinished);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
await waitFor(() => {
|
|
163
|
+
expect(onError).toHaveBeenCalledTimes(1);
|
|
164
|
+
});
|
|
165
|
+
expect(onError.mock.calls[0][0].response.error).toBe(MAPPER_ERROR);
|
|
166
|
+
expect(onFinished).toHaveBeenCalledTimes(1);
|
|
167
|
+
expect(onSuccess).not.toHaveBeenCalled();
|
|
168
|
+
expect(logError).toHaveBeenCalledWith(expect.objectContaining({ title: "Response mapper failed" }));
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("should expose the mapper error in useFetch state and stop loading", async () => {
|
|
172
|
+
const { fetchRequest } = createSetup();
|
|
173
|
+
const request = fetchRequest.setResponseMapper(mapper);
|
|
174
|
+
mockRequest(request, { data: { ok: true } });
|
|
175
|
+
|
|
176
|
+
const { result } = renderHook(() => useFetch(request));
|
|
177
|
+
|
|
178
|
+
await waitFor(() => {
|
|
179
|
+
expect(result.current.error).toBe(MAPPER_ERROR);
|
|
180
|
+
});
|
|
181
|
+
await waitFor(() => {
|
|
182
|
+
expect(result.current.loading).toBe(false);
|
|
183
|
+
});
|
|
184
|
+
expect(result.current.data).toBeNull();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should not break useFetch mounted with cached data", async () => {
|
|
188
|
+
const { client, fetchRequest } = createSetup();
|
|
189
|
+
const request = fetchRequest.setResponseMapper(mapper);
|
|
190
|
+
mockRequest(request, { data: { ok: true } });
|
|
191
|
+
client.cache.set(request, {
|
|
192
|
+
data: { ok: true },
|
|
193
|
+
error: null,
|
|
194
|
+
status: 200,
|
|
195
|
+
success: true,
|
|
196
|
+
extra: client.adapter.defaultExtra,
|
|
197
|
+
retries: 0,
|
|
198
|
+
isCanceled: false,
|
|
199
|
+
isOffline: false,
|
|
200
|
+
willRetry: false,
|
|
201
|
+
addedTimestamp: Date.now(),
|
|
202
|
+
triggerTimestamp: Date.now(),
|
|
203
|
+
requestTimestamp: Date.now(),
|
|
204
|
+
responseTimestamp: Date.now(),
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const { result } = renderHook(() => useFetch(request));
|
|
208
|
+
|
|
209
|
+
await waitFor(() => {
|
|
210
|
+
expect(result.current.error).toBe(MAPPER_ERROR);
|
|
211
|
+
});
|
|
212
|
+
expect(result.current.data).toBeNull();
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|
|
@@ -159,7 +159,12 @@ describe("useFetch [ Methods ]", () => {
|
|
|
159
159
|
const spy7 = vi.fn();
|
|
160
160
|
const spy8 = vi.fn();
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
let successMock = mockRequest(request, { status: 400 });
|
|
163
|
+
// Swapped on the response event, polling for the error state can lose the race with the retry timer
|
|
164
|
+
const unmountListener = client.requestManager.events.onResponse(() => {
|
|
165
|
+
unmountListener();
|
|
166
|
+
successMock = mockRequest(request);
|
|
167
|
+
});
|
|
163
168
|
const response = renderUseFetch(request.setRetry(1).setRetryTime(100));
|
|
164
169
|
|
|
165
170
|
act(() => {
|
|
@@ -173,8 +178,9 @@ describe("useFetch [ Methods ]", () => {
|
|
|
173
178
|
response.result.current.onOfflineError(spy8);
|
|
174
179
|
});
|
|
175
180
|
|
|
176
|
-
await
|
|
177
|
-
|
|
181
|
+
await waitFor(() => {
|
|
182
|
+
expect(spy5).toHaveBeenCalledTimes(1);
|
|
183
|
+
});
|
|
178
184
|
await testSuccessState(successMock, response);
|
|
179
185
|
|
|
180
186
|
expect(spy1).toHaveBeenCalledTimes(2);
|
|
@@ -199,7 +199,12 @@ describe("useSubmit [ Methods ]", () => {
|
|
|
199
199
|
const spy7 = vi.fn();
|
|
200
200
|
const spy8 = vi.fn();
|
|
201
201
|
|
|
202
|
-
|
|
202
|
+
let successMock = mockRequest(request, { status: 400 });
|
|
203
|
+
// Swapped on the response event, polling for the error state can lose the race with the retry timer
|
|
204
|
+
const unmountListener = client.requestManager.events.onResponse(() => {
|
|
205
|
+
unmountListener();
|
|
206
|
+
successMock = mockRequest(request);
|
|
207
|
+
});
|
|
203
208
|
const response = renderUseSubmit(request.setRetry(1).setRetryTime(100));
|
|
204
209
|
|
|
205
210
|
act(() => {
|
|
@@ -214,8 +219,9 @@ describe("useSubmit [ Methods ]", () => {
|
|
|
214
219
|
response.result.current.submit();
|
|
215
220
|
});
|
|
216
221
|
|
|
217
|
-
await
|
|
218
|
-
|
|
222
|
+
await waitFor(() => {
|
|
223
|
+
expect(spy5).toHaveBeenCalledTimes(1);
|
|
224
|
+
});
|
|
219
225
|
await testSuccessState(successMock, response);
|
|
220
226
|
|
|
221
227
|
expect(spy1).toHaveBeenCalledTimes(2);
|