@hyper-fetch/core 7.5.2 → 7.5.3
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/adapter/adapter.base.spec.ts +387 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +1023 -0
- package/__tests__/features/adapter/adapter.browser.spec.ts +103 -0
- package/__tests__/features/adapter/adapter.server.spec.ts +177 -0
- package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
- package/__tests__/features/cache/cache.base.spec.ts +358 -0
- package/__tests__/features/cache/cache.events.spec.ts +215 -0
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +172 -0
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +129 -0
- package/__tests__/features/cache/cache.utils.spec.ts +119 -0
- package/__tests__/features/client/client.auth.spec.ts +125 -0
- package/__tests__/features/client/client.base.spec.ts +149 -0
- package/__tests__/features/client/client.hydration.spec.ts +171 -0
- package/__tests__/features/client/client.interceptor.spec.ts +225 -0
- package/__tests__/features/client/client.methods.spec.ts +188 -0
- package/__tests__/features/client/client.middleware.spec.ts +118 -0
- package/__tests__/features/client/client.utils.spec.ts +292 -0
- package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +304 -0
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
- package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
- package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +125 -0
- package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
- package/__tests__/features/managers/app/app.manager.ssr.spec.ts +40 -0
- package/__tests__/features/managers/logger/logger.base.spec.ts +107 -0
- package/__tests__/features/managers/logger/logger.utils.spec.ts +100 -0
- package/__tests__/features/managers/request/request.manager.base.spec.ts +222 -0
- package/__tests__/features/managers/request/request.manager.events.spec.ts +280 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +567 -0
- package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
- package/__tests__/features/request/request.base.spec.ts +199 -0
- package/__tests__/features/request/request.cloning.spec.ts +47 -0
- package/__tests__/features/request/request.sending.spec.ts +272 -0
- package/__tests__/features/request/request.setters.spec.ts +157 -0
- package/__tests__/features/request/request.typescript.spec.ts +423 -0
- package/__tests__/features/request/request.utils.spec.ts +77 -0
- package/__tests__/features/sdk/sdk.base.spec.ts +130 -0
- package/__tests__/global.d.ts +1 -0
- package/__tests__/jest.setup.ts +13 -0
- package/__tests__/shared/callbacks.shared.ts +9 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/progress.shared.ts +35 -0
- package/__tests__/tsconfig.json +13 -0
- package/__tests__/utils/adapter.utils.ts +38 -0
- package/__tests__/utils/cache.utils.ts +19 -0
- package/__tests__/utils/callbacks.utils.ts +47 -0
- package/__tests__/utils/dispatcher.utils.ts +6 -0
- package/__tests__/utils/index.ts +4 -0
- package/dist/index.js +12 -1
- package/dist/index.js.map +2 -2
- package/jest.config.ts +6 -0
- package/package.json +1 -1
- package/project.json +4 -0
- package/src/http-adapter/http-adapter.server.ts +15 -1
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { waitFor } from "@testing-library/dom";
|
|
2
|
+
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { AdapterInstance, ResponseType, getErrorMessage } from "adapter";
|
|
5
|
+
import { ResponseDetailsType } from "managers";
|
|
6
|
+
import { createDispatcher, createAdapter } from "../../utils";
|
|
7
|
+
import { Client } from "client";
|
|
8
|
+
import { xhrExtra } from "http-adapter";
|
|
9
|
+
|
|
10
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
11
|
+
|
|
12
|
+
describe("Dispatcher [ Events ]", () => {
|
|
13
|
+
const adapterSpy = jest.fn();
|
|
14
|
+
|
|
15
|
+
let adapter = createAdapter({ callback: adapterSpy });
|
|
16
|
+
let client = new Client({ url: "shared-base-url" }).setAdapter(adapter);
|
|
17
|
+
let request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
18
|
+
let dispatcher = createDispatcher(client);
|
|
19
|
+
|
|
20
|
+
beforeAll(() => {
|
|
21
|
+
startServer();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
resetMocks();
|
|
26
|
+
jest.resetAllMocks();
|
|
27
|
+
adapter = createAdapter({ callback: adapterSpy });
|
|
28
|
+
client = new Client({ url: "shared-base-url" }).setAdapter(adapter);
|
|
29
|
+
request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
30
|
+
dispatcher = createDispatcher(client);
|
|
31
|
+
mockRequest(request, { status: 200, delay: 0 });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
afterAll(() => {
|
|
35
|
+
stopServer();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("When using dispatcher events", () => {
|
|
39
|
+
it("should emit loading event", async () => {
|
|
40
|
+
const spy = jest.fn();
|
|
41
|
+
const unmount = client.requestManager.events.onLoadingByQueue(request.queryKey, spy);
|
|
42
|
+
dispatcher.add(request);
|
|
43
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
44
|
+
unmount();
|
|
45
|
+
dispatcher.add(request);
|
|
46
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
47
|
+
});
|
|
48
|
+
it("should emit drained event", async () => {
|
|
49
|
+
const spy = jest.fn();
|
|
50
|
+
const unmount = dispatcher.events.onDrainedByKey(request.queryKey, spy);
|
|
51
|
+
const requestId = dispatcher.add(request.setQueued(true));
|
|
52
|
+
dispatcher.delete(request.queryKey, requestId, request.abortKey);
|
|
53
|
+
await waitFor(() => {
|
|
54
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
55
|
+
});
|
|
56
|
+
unmount();
|
|
57
|
+
const requestId2 = dispatcher.add(request.setQueued(true));
|
|
58
|
+
dispatcher.delete(request.queryKey, requestId2, request.abortKey);
|
|
59
|
+
await waitFor(() => {
|
|
60
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
it("should emit queue status change event", async () => {
|
|
64
|
+
const spy = jest.fn();
|
|
65
|
+
const unmount = dispatcher.events.onQueueStatusChangeByKey(request.queryKey, spy);
|
|
66
|
+
dispatcher.stop(request.queryKey);
|
|
67
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
68
|
+
unmount();
|
|
69
|
+
dispatcher.stop(request.queryKey);
|
|
70
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
71
|
+
});
|
|
72
|
+
it("should emit queue change event", async () => {
|
|
73
|
+
const spy = jest.fn();
|
|
74
|
+
const unmount = dispatcher.events.onQueueChangeByKey(request.queryKey, spy);
|
|
75
|
+
dispatcher.add(request);
|
|
76
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
77
|
+
unmount();
|
|
78
|
+
dispatcher.add(request);
|
|
79
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
80
|
+
});
|
|
81
|
+
it("should handle listen change events correctly", async () => {
|
|
82
|
+
const spy = jest.fn();
|
|
83
|
+
const unmount = dispatcher.emitter.onListener(request.queryKey, spy);
|
|
84
|
+
|
|
85
|
+
const listener = () => {};
|
|
86
|
+
// Should be called when a new listener is added
|
|
87
|
+
dispatcher.emitter.on(request.queryKey, listener);
|
|
88
|
+
expect(dispatcher.emitter.listeners(request.queryKey).length).toBe(1);
|
|
89
|
+
expect(spy).toHaveBeenCalledWith(1);
|
|
90
|
+
|
|
91
|
+
dispatcher.emitter.off(request.queryKey, listener);
|
|
92
|
+
|
|
93
|
+
expect(dispatcher.emitter.listeners(request.queryKey).length).toBe(0);
|
|
94
|
+
expect(spy).toHaveBeenCalledWith(0);
|
|
95
|
+
|
|
96
|
+
// Should be called when a listener is removed
|
|
97
|
+
unmount();
|
|
98
|
+
|
|
99
|
+
dispatcher.emitter.addListener(request.queryKey, listener);
|
|
100
|
+
expect(dispatcher.emitter.listeners(request.queryKey).length).toBe(1);
|
|
101
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
102
|
+
});
|
|
103
|
+
it("should emit proper data response", async () => {
|
|
104
|
+
let response: [ResponseType<unknown, unknown, AdapterInstance>, ResponseDetailsType];
|
|
105
|
+
const mock = mockRequest(request);
|
|
106
|
+
|
|
107
|
+
client.requestManager.events.onResponseByCache(request.cacheKey, (data) => {
|
|
108
|
+
response = [data.response, data.details];
|
|
109
|
+
});
|
|
110
|
+
dispatcher.add(request);
|
|
111
|
+
|
|
112
|
+
const adapterResponse: ResponseType<unknown, unknown, AdapterInstance> = {
|
|
113
|
+
data: mock,
|
|
114
|
+
error: null,
|
|
115
|
+
status: 200,
|
|
116
|
+
success: true,
|
|
117
|
+
extra: { headers: { "content-type": "application/json", "content-length": "2" } },
|
|
118
|
+
requestTimestamp: expect.toBeNumber(),
|
|
119
|
+
responseTimestamp: expect.toBeNumber(),
|
|
120
|
+
};
|
|
121
|
+
const responseDetails: Omit<ResponseDetailsType, "timestamp"> = {
|
|
122
|
+
retries: 0,
|
|
123
|
+
isCanceled: false,
|
|
124
|
+
isOffline: false,
|
|
125
|
+
triggerTimestamp: expect.toBeNumber(),
|
|
126
|
+
requestTimestamp: expect.toBeNumber(),
|
|
127
|
+
responseTimestamp: expect.toBeNumber(),
|
|
128
|
+
addedTimestamp: expect.toBeNumber(),
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
await waitFor(() => {
|
|
132
|
+
expect(response).toEqual([adapterResponse, responseDetails]);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
it("should emit proper failed response", async () => {
|
|
136
|
+
let response: [ResponseType<unknown, unknown, AdapterInstance>, ResponseDetailsType];
|
|
137
|
+
const mock = mockRequest(request, { status: 400 });
|
|
138
|
+
|
|
139
|
+
client.requestManager.events.onResponseByCache(request.cacheKey, (data) => {
|
|
140
|
+
response = [data.response, data.details];
|
|
141
|
+
});
|
|
142
|
+
dispatcher.add(request);
|
|
143
|
+
|
|
144
|
+
const adapterResponse: ResponseType<unknown, unknown, AdapterInstance> = {
|
|
145
|
+
data: null,
|
|
146
|
+
error: mock,
|
|
147
|
+
status: 400,
|
|
148
|
+
success: false,
|
|
149
|
+
extra: { headers: { "content-type": "application/json", "content-length": "19" } },
|
|
150
|
+
requestTimestamp: expect.toBeNumber(),
|
|
151
|
+
responseTimestamp: expect.toBeNumber(),
|
|
152
|
+
};
|
|
153
|
+
const responseDetails: Omit<ResponseDetailsType, "timestamp"> = {
|
|
154
|
+
retries: 0,
|
|
155
|
+
isCanceled: false,
|
|
156
|
+
isOffline: false,
|
|
157
|
+
triggerTimestamp: expect.toBeNumber(),
|
|
158
|
+
requestTimestamp: expect.toBeNumber(),
|
|
159
|
+
responseTimestamp: expect.toBeNumber(),
|
|
160
|
+
addedTimestamp: expect.toBeNumber(),
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
await waitFor(() => {
|
|
164
|
+
expect(response).toEqual([adapterResponse, responseDetails]);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
it("should emit proper retry response", async () => {
|
|
168
|
+
let response: [ResponseType<unknown, unknown, AdapterInstance>, ResponseDetailsType];
|
|
169
|
+
const requestWithRetry = request.setRetry(1).setRetryTime(50);
|
|
170
|
+
mockRequest(requestWithRetry, { status: 400, delay: 0 });
|
|
171
|
+
|
|
172
|
+
client.requestManager.events.onResponseByCache(requestWithRetry.cacheKey, (data) => {
|
|
173
|
+
response = [data.response, data.details];
|
|
174
|
+
});
|
|
175
|
+
dispatcher.add(requestWithRetry);
|
|
176
|
+
|
|
177
|
+
await waitFor(() => {
|
|
178
|
+
expect(response).toBeDefined();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const mock = mockRequest(requestWithRetry);
|
|
182
|
+
|
|
183
|
+
const adapterResponse: ResponseType<unknown, unknown, AdapterInstance> = {
|
|
184
|
+
data: mock,
|
|
185
|
+
error: null,
|
|
186
|
+
status: 200,
|
|
187
|
+
success: true,
|
|
188
|
+
extra: { headers: { "content-type": "application/json", "content-length": "2" } },
|
|
189
|
+
requestTimestamp: expect.toBeNumber(),
|
|
190
|
+
responseTimestamp: expect.toBeNumber(),
|
|
191
|
+
};
|
|
192
|
+
const responseDetails: Omit<ResponseDetailsType, "timestamp"> = {
|
|
193
|
+
retries: 1,
|
|
194
|
+
isCanceled: false,
|
|
195
|
+
isOffline: false,
|
|
196
|
+
triggerTimestamp: expect.toBeNumber(),
|
|
197
|
+
requestTimestamp: expect.toBeNumber(),
|
|
198
|
+
responseTimestamp: expect.toBeNumber(),
|
|
199
|
+
addedTimestamp: expect.toBeNumber(),
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
await waitFor(() => {
|
|
203
|
+
expect(response).toEqual([adapterResponse, responseDetails]);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
it("should emit proper cancel response", async () => {
|
|
207
|
+
let response: [ResponseType<unknown, unknown, AdapterInstance>, ResponseDetailsType];
|
|
208
|
+
mockRequest(request, { status: 400 });
|
|
209
|
+
|
|
210
|
+
client.requestManager.events.onResponseByCache(request.cacheKey, (data) => {
|
|
211
|
+
response = [data.response, data.details];
|
|
212
|
+
});
|
|
213
|
+
dispatcher.add(request);
|
|
214
|
+
await sleep(1);
|
|
215
|
+
client.requestManager.abortAll();
|
|
216
|
+
|
|
217
|
+
const adapterResponse: ResponseType<unknown, unknown, AdapterInstance> = {
|
|
218
|
+
data: null,
|
|
219
|
+
error: getErrorMessage("abort"),
|
|
220
|
+
status: 0,
|
|
221
|
+
success: false,
|
|
222
|
+
extra: xhrExtra,
|
|
223
|
+
requestTimestamp: expect.toBeNumber(),
|
|
224
|
+
responseTimestamp: expect.toBeNumber(),
|
|
225
|
+
};
|
|
226
|
+
const responseDetails: Omit<ResponseDetailsType, "timestamp"> = {
|
|
227
|
+
retries: 0,
|
|
228
|
+
isCanceled: true,
|
|
229
|
+
isOffline: false,
|
|
230
|
+
addedTimestamp: expect.toBeNumber(),
|
|
231
|
+
requestTimestamp: expect.toBeNumber(),
|
|
232
|
+
responseTimestamp: expect.toBeNumber(),
|
|
233
|
+
triggerTimestamp: expect.toBeNumber(),
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
await waitFor(() => {
|
|
237
|
+
expect(response).toEqual([adapterResponse, responseDetails]);
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
it("should emit and unsubscribe from global drained event", async () => {
|
|
241
|
+
const spy = jest.fn();
|
|
242
|
+
const unmount = dispatcher.events.onDrained(spy);
|
|
243
|
+
|
|
244
|
+
// Add and complete multiple requests to trigger drain
|
|
245
|
+
const request1 = dispatcher.add(request.setQueued(true));
|
|
246
|
+
const request2 = dispatcher.add(request.setQueued(true));
|
|
247
|
+
|
|
248
|
+
dispatcher.delete(request.queryKey, request1, request.abortKey);
|
|
249
|
+
dispatcher.delete(request.queryKey, request2, request.abortKey);
|
|
250
|
+
|
|
251
|
+
await waitFor(() => {
|
|
252
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// Test unsubscribe
|
|
256
|
+
unmount();
|
|
257
|
+
|
|
258
|
+
const request3 = dispatcher.add(request.setQueued(true));
|
|
259
|
+
dispatcher.delete(request.queryKey, request3, request.abortKey);
|
|
260
|
+
|
|
261
|
+
await sleep(10); // Give time for potential unwanted events
|
|
262
|
+
expect(spy).toHaveBeenCalledTimes(1); // Should not be called again
|
|
263
|
+
});
|
|
264
|
+
it("should emit and unsubscribe from global queue status change event", async () => {
|
|
265
|
+
const spy = jest.fn();
|
|
266
|
+
const unmount = dispatcher.events.onQueueStatusChange(spy);
|
|
267
|
+
|
|
268
|
+
// Trigger queue status change
|
|
269
|
+
dispatcher.stop(request.queryKey);
|
|
270
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
271
|
+
expect(spy).toHaveBeenCalledWith(
|
|
272
|
+
expect.objectContaining({ stopped: true, requests: [], queryKey: request.queryKey }),
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
// Test unsubscribe
|
|
276
|
+
unmount();
|
|
277
|
+
|
|
278
|
+
// Should not trigger spy after unmount
|
|
279
|
+
dispatcher.stop(request.queryKey);
|
|
280
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
281
|
+
});
|
|
282
|
+
it("should emit and unsubscribe from global queue change event", async () => {
|
|
283
|
+
const spy = jest.fn();
|
|
284
|
+
const unmount = dispatcher.events.onQueueChange(spy);
|
|
285
|
+
|
|
286
|
+
// Add request to trigger queue change
|
|
287
|
+
dispatcher.add(request);
|
|
288
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
289
|
+
expect(spy).toHaveBeenCalledWith(
|
|
290
|
+
expect.objectContaining({
|
|
291
|
+
requests: expect.any(Array),
|
|
292
|
+
queryKey: request.queryKey,
|
|
293
|
+
}),
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
// Test unsubscribe
|
|
297
|
+
unmount();
|
|
298
|
+
|
|
299
|
+
// Should not trigger spy after unmount
|
|
300
|
+
dispatcher.add(request);
|
|
301
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
});
|