@hyper-fetch/core 7.5.1 → 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/browser/index.js +2 -1
- package/dist/browser/index.js.map +2 -2
- package/dist/index.js +14 -2
- 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/adapter/adapter.utils.ts +2 -1
- package/src/http-adapter/http-adapter.server.ts +15 -1
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { ResponseSuccessType } from "adapter";
|
|
4
|
+
import { ResponseDetailsType } from "managers";
|
|
5
|
+
import { createCache } from "../../utils";
|
|
6
|
+
import { Client } from "client";
|
|
7
|
+
import { HttpAdapterType, xhrExtra } from "http-adapter";
|
|
8
|
+
|
|
9
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
10
|
+
|
|
11
|
+
describe("Cache [ Base ]", () => {
|
|
12
|
+
const response: ResponseSuccessType<unknown, HttpAdapterType> = {
|
|
13
|
+
data: 123,
|
|
14
|
+
error: null,
|
|
15
|
+
status: 200,
|
|
16
|
+
success: true,
|
|
17
|
+
extra: xhrExtra,
|
|
18
|
+
requestTimestamp: Date.now(),
|
|
19
|
+
responseTimestamp: Date.now(),
|
|
20
|
+
};
|
|
21
|
+
const details: ResponseDetailsType = {
|
|
22
|
+
retries: 0,
|
|
23
|
+
requestTimestamp: +new Date(),
|
|
24
|
+
responseTimestamp: +new Date(),
|
|
25
|
+
addedTimestamp: +new Date(),
|
|
26
|
+
triggerTimestamp: +new Date(),
|
|
27
|
+
isCanceled: false,
|
|
28
|
+
isOffline: false,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
let client = new Client({ url: "shared-base-url" });
|
|
32
|
+
let request = client.createRequest()({ endpoint: "/shared-endpoint" });
|
|
33
|
+
let cache = createCache(client);
|
|
34
|
+
|
|
35
|
+
beforeAll(() => {
|
|
36
|
+
startServer();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
client = new Client({ url: "shared-base-url" });
|
|
41
|
+
request = client.createRequest()({ endpoint: "/shared-endpoint" });
|
|
42
|
+
cache = createCache(client);
|
|
43
|
+
resetMocks();
|
|
44
|
+
jest.resetAllMocks();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterAll(() => {
|
|
48
|
+
stopServer();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("When lifecycle events get triggered", () => {
|
|
52
|
+
it("should initialize cache", async () => {
|
|
53
|
+
expect(cache.get(request.cacheKey)).not.toBeDefined();
|
|
54
|
+
|
|
55
|
+
cache.set(request.setCache(true), { ...response, ...details, data: "TEST!" });
|
|
56
|
+
|
|
57
|
+
expect(cache.get(request.cacheKey)).toBeDefined();
|
|
58
|
+
expect(cache.get(request.cacheKey)?.data).toBe("TEST!");
|
|
59
|
+
|
|
60
|
+
cache.set(request.setCache(true), (previous) => ({ ...response, ...details, data: `${previous?.data} WOW!` }));
|
|
61
|
+
|
|
62
|
+
expect(cache.get(request.cacheKey)?.data).toBe("TEST! WOW!");
|
|
63
|
+
});
|
|
64
|
+
it("should update cache", async () => {
|
|
65
|
+
cache.set(request.setCache(true), { ...response, ...details });
|
|
66
|
+
cache.update(request.setCache(true), { ...response, ...details, data: "SUPER TEST!" });
|
|
67
|
+
|
|
68
|
+
expect(cache.get(request.cacheKey)?.data).toBe("SUPER TEST!");
|
|
69
|
+
|
|
70
|
+
cache.update(request.setCache(true), (previous) => ({ ...response, ...details, data: `${previous?.data} WOW!` }));
|
|
71
|
+
|
|
72
|
+
expect(cache.get(request.cacheKey)?.data).toBe("SUPER TEST! WOW!");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe("When managing cache data", () => {
|
|
76
|
+
it("should add element to cache and emit set event", async () => {
|
|
77
|
+
const trigger = jest.fn();
|
|
78
|
+
const unmount = cache.events.onDataByKey(request.cacheKey, trigger);
|
|
79
|
+
|
|
80
|
+
cache.set(request.setCache(true), { ...response, ...details });
|
|
81
|
+
unmount();
|
|
82
|
+
|
|
83
|
+
expect(trigger).toHaveBeenCalledTimes(1);
|
|
84
|
+
expect(cache.get(request.cacheKey)).toBeDefined();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should delete cache", async () => {
|
|
88
|
+
cache.set(request.setCache(true), { ...response, ...details });
|
|
89
|
+
cache.delete(request.cacheKey);
|
|
90
|
+
await sleep(1);
|
|
91
|
+
|
|
92
|
+
expect(cache.get(request.cacheKey)).not.toBeDefined();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should invalidate and remove cache", async () => {
|
|
96
|
+
const trigger = jest.fn();
|
|
97
|
+
const unmount = cache.events.onInvalidateByKey(request.cacheKey, trigger);
|
|
98
|
+
|
|
99
|
+
cache.set(request.setCache(true), { ...response, ...details });
|
|
100
|
+
await cache.invalidate(request.cacheKey);
|
|
101
|
+
await sleep(1);
|
|
102
|
+
|
|
103
|
+
const data = cache.get(request.cacheKey);
|
|
104
|
+
expect(data).toBeDefined();
|
|
105
|
+
expect(data?.staleTime).toBe(0);
|
|
106
|
+
expect(trigger).toHaveBeenCalledTimes(1);
|
|
107
|
+
unmount();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should not add to cache when useCache is set to false", async () => {
|
|
111
|
+
const trigger = jest.fn();
|
|
112
|
+
const unmount = cache.events.onDataByKey(request.cacheKey, trigger);
|
|
113
|
+
|
|
114
|
+
cache.set(request.setCache(false), { ...response, ...details });
|
|
115
|
+
unmount();
|
|
116
|
+
|
|
117
|
+
expect(trigger).toHaveBeenCalledTimes(1);
|
|
118
|
+
expect(cache.get(request.cacheKey)).not.toBeDefined();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should allow to get cache keys", async () => {
|
|
122
|
+
cache.set(request.setCacheKey("1"), { ...response, ...details });
|
|
123
|
+
cache.set(request.setCacheKey("2"), { ...response, ...details });
|
|
124
|
+
cache.set(request.setCacheKey("3"), { ...response, ...details });
|
|
125
|
+
|
|
126
|
+
expect(cache.keys()).toHaveLength(3);
|
|
127
|
+
expect(cache.keys()).toStrictEqual(["1", "2", "3"]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("should handle both function and non-function responses", async () => {
|
|
131
|
+
// Test direct value response
|
|
132
|
+
cache.set(request.setCache(true), { ...response, ...details, data: "DIRECT" });
|
|
133
|
+
expect(cache.get(request.cacheKey)?.data).toBe("DIRECT");
|
|
134
|
+
|
|
135
|
+
// Test function response with no previous data
|
|
136
|
+
cache.delete(request.cacheKey); // Clear previous data
|
|
137
|
+
cache.set(request.setCache(true), () => ({ ...response, ...details, data: "FUNCTION NO PREV" }));
|
|
138
|
+
expect(cache.get(request.cacheKey)?.data).toBe("FUNCTION NO PREV");
|
|
139
|
+
|
|
140
|
+
// Test function response with previous data
|
|
141
|
+
cache.set(request.setCache(true), (prev) => ({
|
|
142
|
+
...response,
|
|
143
|
+
...details,
|
|
144
|
+
data: `${prev?.data} UPDATED`,
|
|
145
|
+
}));
|
|
146
|
+
expect(cache.get(request.cacheKey)?.data).toBe("FUNCTION NO PREV UPDATED");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("should properly merge partial responses with existing cache data", async () => {
|
|
150
|
+
// Set initial cache data
|
|
151
|
+
const initialData = {
|
|
152
|
+
...response,
|
|
153
|
+
...details,
|
|
154
|
+
data: { field1: "value1", field2: "value2" },
|
|
155
|
+
};
|
|
156
|
+
cache.set(request.setCache(true), initialData);
|
|
157
|
+
|
|
158
|
+
// Test direct value partial update
|
|
159
|
+
cache.update(request.setCache(true), { data: { field2: "updated2", field3: "value3" } });
|
|
160
|
+
|
|
161
|
+
let result = cache.get(request.cacheKey);
|
|
162
|
+
expect(result?.data).toEqual({
|
|
163
|
+
field2: "updated2",
|
|
164
|
+
field3: "value3",
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
cache.delete(request.cacheKey);
|
|
168
|
+
|
|
169
|
+
// Cannot update if cache is not set
|
|
170
|
+
cache.update(request.setCache(true), (prev) => {
|
|
171
|
+
expect(prev).toBeNull();
|
|
172
|
+
return {
|
|
173
|
+
...prev,
|
|
174
|
+
data: {
|
|
175
|
+
...prev?.data,
|
|
176
|
+
field1: "updated1",
|
|
177
|
+
field4: "value4",
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
result = cache.get(request.cacheKey);
|
|
183
|
+
expect(result).toBeUndefined();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("When CacheStore gets cleared before triggering cache actions", () => {
|
|
188
|
+
it("should return undefined when removed cache entity", async () => {
|
|
189
|
+
const trigger = jest.fn();
|
|
190
|
+
|
|
191
|
+
cache.events.onInvalidateByKey(request.cacheKey, trigger);
|
|
192
|
+
|
|
193
|
+
cache.set(request.setCache(false), { ...response, ...details });
|
|
194
|
+
cache.clear();
|
|
195
|
+
|
|
196
|
+
expect(trigger).toHaveBeenCalledTimes(0);
|
|
197
|
+
expect(cache.get(request.cacheKey)).not.toBeDefined();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("invalidate", () => {
|
|
202
|
+
it("should set staleTime to 0 when invalidating with existing cache", async () => {
|
|
203
|
+
// Set initial cache data
|
|
204
|
+
const initialData = {
|
|
205
|
+
...response,
|
|
206
|
+
...details,
|
|
207
|
+
data: "initial",
|
|
208
|
+
staleTime: 1000,
|
|
209
|
+
};
|
|
210
|
+
cache.set(request.setCache(true), initialData);
|
|
211
|
+
|
|
212
|
+
// Verify initial staleTime
|
|
213
|
+
expect(cache.get(request.cacheKey)?.staleTime).toBeGreaterThan(0);
|
|
214
|
+
|
|
215
|
+
// Update the value
|
|
216
|
+
await cache.invalidate(request.cacheKey);
|
|
217
|
+
|
|
218
|
+
// Verify staleTime is set to 0
|
|
219
|
+
const updatedCache = cache.get(request.cacheKey);
|
|
220
|
+
expect(updatedCache?.staleTime).toBe(0);
|
|
221
|
+
});
|
|
222
|
+
it("should not set staleTime to 0 when invalidating with non-existing cache", async () => {
|
|
223
|
+
cache.storage.set(request.cacheKey, undefined as any);
|
|
224
|
+
|
|
225
|
+
// Update the value
|
|
226
|
+
await cache.invalidate(request.cacheKey);
|
|
227
|
+
|
|
228
|
+
// Verify staleTime is set to 0
|
|
229
|
+
const updatedCache = cache.get(request.cacheKey);
|
|
230
|
+
expect(updatedCache).toBeUndefined();
|
|
231
|
+
});
|
|
232
|
+
it("should invalidate cache when given a Request instance", async () => {
|
|
233
|
+
// Create a mock Request instance
|
|
234
|
+
const mockRequest = client.createRequest()({ endpoint: "/shared-endpoint" });
|
|
235
|
+
|
|
236
|
+
// Add some data to cache with the request's cacheKey
|
|
237
|
+
const data = {
|
|
238
|
+
data: "test",
|
|
239
|
+
success: true,
|
|
240
|
+
staleTime: 1000,
|
|
241
|
+
version: "0.0.1",
|
|
242
|
+
cacheKey: mockRequest.cacheKey,
|
|
243
|
+
cacheTime: 2000,
|
|
244
|
+
responseTimestamp: Date.now(),
|
|
245
|
+
error: null,
|
|
246
|
+
status: 200,
|
|
247
|
+
extra: xhrExtra,
|
|
248
|
+
requestTimestamp: Date.now(),
|
|
249
|
+
retries: 0,
|
|
250
|
+
isCanceled: false,
|
|
251
|
+
isOffline: false,
|
|
252
|
+
addedTimestamp: Date.now(),
|
|
253
|
+
triggerTimestamp: Date.now(),
|
|
254
|
+
};
|
|
255
|
+
cache.storage.set(mockRequest.cacheKey, data);
|
|
256
|
+
|
|
257
|
+
await cache.invalidate(mockRequest);
|
|
258
|
+
|
|
259
|
+
const invalidatedData = cache.storage.get(mockRequest.cacheKey);
|
|
260
|
+
|
|
261
|
+
expect(invalidatedData?.staleTime).toBe(0);
|
|
262
|
+
});
|
|
263
|
+
it("should not invalidate cache when it is empty", async () => {
|
|
264
|
+
// Create a mock Request instance
|
|
265
|
+
const mockRequest = client
|
|
266
|
+
.createRequest()({ endpoint: "/shared-endpoint" })
|
|
267
|
+
.setMock(() => ({
|
|
268
|
+
data: "test" as any,
|
|
269
|
+
status: 200,
|
|
270
|
+
}));
|
|
271
|
+
|
|
272
|
+
cache.invalidate([new RegExp(mockRequest.cacheKey)]);
|
|
273
|
+
|
|
274
|
+
const spy = jest.spyOn(cache.events, "emitInvalidation");
|
|
275
|
+
expect(spy).not.toHaveBeenCalled();
|
|
276
|
+
});
|
|
277
|
+
it("should not invalidate cache when app is offline", async () => {
|
|
278
|
+
// Create a mock Request instance
|
|
279
|
+
const mockRequest = client
|
|
280
|
+
.createRequest()({ endpoint: "/shared-endpoint", cacheTime: -100 })
|
|
281
|
+
.setMock(() => ({
|
|
282
|
+
data: "test" as any,
|
|
283
|
+
status: 200,
|
|
284
|
+
}));
|
|
285
|
+
|
|
286
|
+
await mockRequest.send({
|
|
287
|
+
onResponse: () => {
|
|
288
|
+
client.appManager.setOnline(false);
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
cache.garbageCollectors.clear();
|
|
292
|
+
cache.invalidate([new RegExp(mockRequest.cacheKey)]);
|
|
293
|
+
|
|
294
|
+
const spy = jest.spyOn(cache.events, "emitInvalidation");
|
|
295
|
+
expect(spy).not.toHaveBeenCalled();
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("should not invalidate cache when app goes offline", async () => {
|
|
299
|
+
// Create a mock Request instance
|
|
300
|
+
const mockRequest = client
|
|
301
|
+
.createRequest()({ endpoint: "/shared-endpoint", cacheTime: 100 })
|
|
302
|
+
.setMock(() => ({
|
|
303
|
+
data: "test" as any,
|
|
304
|
+
status: 200,
|
|
305
|
+
}));
|
|
306
|
+
|
|
307
|
+
await mockRequest.send();
|
|
308
|
+
cache.invalidate([new RegExp(mockRequest.cacheKey)]);
|
|
309
|
+
client.appManager.setOnline(false);
|
|
310
|
+
await sleep(150);
|
|
311
|
+
|
|
312
|
+
const spy = jest.spyOn(cache.events, "emitInvalidation");
|
|
313
|
+
expect(spy).not.toHaveBeenCalled();
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("should handle array of mixed invalidation keys (Request, string, RegExp)", async () => {
|
|
317
|
+
// Create test data with different keys
|
|
318
|
+
const mockRequest = client.createRequest()({ endpoint: "/shared-endpoint" });
|
|
319
|
+
|
|
320
|
+
const testData = {
|
|
321
|
+
[mockRequest.cacheKey]: "request data",
|
|
322
|
+
"string-key": "string data",
|
|
323
|
+
"test-prefix-123": "regex data 1",
|
|
324
|
+
"test-prefix-456": "regex data 2",
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// Set up cache with test data
|
|
328
|
+
Object.entries(testData).forEach(([key, value]) => {
|
|
329
|
+
cache.storage.set(key, {
|
|
330
|
+
data: value,
|
|
331
|
+
success: true,
|
|
332
|
+
staleTime: 1000,
|
|
333
|
+
version: "0.0.1",
|
|
334
|
+
cacheKey: key,
|
|
335
|
+
cacheTime: 2000,
|
|
336
|
+
responseTimestamp: Date.now(),
|
|
337
|
+
error: null,
|
|
338
|
+
status: 200,
|
|
339
|
+
extra: xhrExtra,
|
|
340
|
+
requestTimestamp: Date.now(),
|
|
341
|
+
retries: 0,
|
|
342
|
+
isCanceled: false,
|
|
343
|
+
isOffline: false,
|
|
344
|
+
addedTimestamp: Date.now(),
|
|
345
|
+
triggerTimestamp: Date.now(),
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
// Invalidate using array of different key types
|
|
350
|
+
await cache.invalidate([mockRequest, "string-key", /^test-prefix-/]);
|
|
351
|
+
|
|
352
|
+
// Verify all matching keys were invalidated
|
|
353
|
+
Object.entries(testData).forEach(([key]) => {
|
|
354
|
+
expect(cache.storage.get(key)?.staleTime).toBe(0);
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
});
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { sleep } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { CacheValueType } from "cache";
|
|
4
|
+
import { createCache } from "../../utils";
|
|
5
|
+
import { Client } from "client";
|
|
6
|
+
import { xhrExtra } from "http-adapter";
|
|
7
|
+
import { Plugin } from "plugin";
|
|
8
|
+
|
|
9
|
+
describe("Cache [ Events ]", () => {
|
|
10
|
+
const cacheKey = "test";
|
|
11
|
+
|
|
12
|
+
let client = new Client({ url: "shared-base-url" });
|
|
13
|
+
let request = client.createRequest()({ endpoint: "shared-endpoint", cacheKey, cache: true });
|
|
14
|
+
let cache = createCache(client);
|
|
15
|
+
const spy = jest.fn();
|
|
16
|
+
|
|
17
|
+
const cacheData: CacheValueType = {
|
|
18
|
+
data: null,
|
|
19
|
+
error: null,
|
|
20
|
+
status: 200,
|
|
21
|
+
success: true,
|
|
22
|
+
extra: xhrExtra,
|
|
23
|
+
retries: 0,
|
|
24
|
+
requestTimestamp: +new Date(),
|
|
25
|
+
responseTimestamp: +new Date(),
|
|
26
|
+
addedTimestamp: +new Date(),
|
|
27
|
+
triggerTimestamp: +new Date(),
|
|
28
|
+
cached: true,
|
|
29
|
+
isCanceled: false,
|
|
30
|
+
isOffline: false,
|
|
31
|
+
cacheKey: request.cacheKey,
|
|
32
|
+
staleTime: request.staleTime,
|
|
33
|
+
version: cache.version,
|
|
34
|
+
cacheTime: 300000,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
client = new Client({ url: "shared-base-url" });
|
|
39
|
+
request = client.createRequest()({ endpoint: "shared-endpoint", cacheKey, cache: true });
|
|
40
|
+
cache = createCache(client);
|
|
41
|
+
jest.resetAllMocks();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("when options events are triggered", () => {
|
|
45
|
+
it("should trigger onChange event when data is set", async () => {
|
|
46
|
+
const plugin = new Plugin({ name: "change" }).onCacheItemChange(spy);
|
|
47
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
48
|
+
|
|
49
|
+
newCache.set(request, cacheData);
|
|
50
|
+
|
|
51
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
52
|
+
expect(spy).toHaveBeenCalledWith({
|
|
53
|
+
cache: newCache,
|
|
54
|
+
cacheKey: request.cacheKey,
|
|
55
|
+
newData: cacheData,
|
|
56
|
+
prevData: null,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
it("should trigger onDelete event when data is deleted", async () => {
|
|
60
|
+
const plugin = new Plugin({ name: "delete" }).onCacheItemDelete(spy);
|
|
61
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
62
|
+
|
|
63
|
+
newCache.delete(request.cacheKey);
|
|
64
|
+
|
|
65
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
66
|
+
expect(spy).toHaveBeenCalledWith({ cache: newCache, cacheKey: request.cacheKey });
|
|
67
|
+
});
|
|
68
|
+
it("should trigger onData event when data is set", async () => {
|
|
69
|
+
const plugin = new Plugin({ name: "data" });
|
|
70
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
71
|
+
|
|
72
|
+
newCache.events.onData(spy);
|
|
73
|
+
newCache.set(request, cacheData);
|
|
74
|
+
|
|
75
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
76
|
+
expect(spy).toHaveBeenCalledWith(
|
|
77
|
+
expect.objectContaining({
|
|
78
|
+
data: null,
|
|
79
|
+
cacheKey: request.cacheKey,
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
it("should trigger onDelete event when data is deleted", async () => {
|
|
84
|
+
const plugin = new Plugin({ name: "delete" });
|
|
85
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
86
|
+
|
|
87
|
+
newCache.events.onDelete(spy);
|
|
88
|
+
newCache.delete(request.cacheKey);
|
|
89
|
+
|
|
90
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
91
|
+
expect(spy).toHaveBeenCalledWith(request.cacheKey);
|
|
92
|
+
});
|
|
93
|
+
it("should trigger onDeleteByKey event when specific cache key is deleted", async () => {
|
|
94
|
+
const plugin = new Plugin({ name: "deleteByKey" });
|
|
95
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
96
|
+
|
|
97
|
+
newCache.events.onDeleteByKey(request.cacheKey, spy);
|
|
98
|
+
newCache.delete(request.cacheKey);
|
|
99
|
+
|
|
100
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
101
|
+
});
|
|
102
|
+
it("should properly cleanup onData listener when unmount function is called", async () => {
|
|
103
|
+
const plugin = new Plugin({ name: "data" });
|
|
104
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
105
|
+
|
|
106
|
+
const unmount = newCache.events.onData(spy);
|
|
107
|
+
unmount();
|
|
108
|
+
newCache.set(request, cacheData);
|
|
109
|
+
|
|
110
|
+
expect(spy).not.toHaveBeenCalled();
|
|
111
|
+
});
|
|
112
|
+
it("should properly cleanup onDataByKey listener when unmount function is called", async () => {
|
|
113
|
+
const plugin = new Plugin({ name: "dataByKey" });
|
|
114
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
115
|
+
|
|
116
|
+
const unmount = newCache.events.onDataByKey(request.cacheKey, spy);
|
|
117
|
+
unmount();
|
|
118
|
+
newCache.set(request, cacheData);
|
|
119
|
+
|
|
120
|
+
expect(spy).not.toHaveBeenCalled();
|
|
121
|
+
});
|
|
122
|
+
it("should properly cleanup onDelete listener when unmount function is called", async () => {
|
|
123
|
+
const plugin = new Plugin({ name: "delete" });
|
|
124
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
125
|
+
|
|
126
|
+
const unmount = newCache.events.onDelete(spy);
|
|
127
|
+
unmount();
|
|
128
|
+
newCache.delete(request.cacheKey);
|
|
129
|
+
|
|
130
|
+
expect(spy).not.toHaveBeenCalled();
|
|
131
|
+
});
|
|
132
|
+
it("should properly cleanup onDeleteByKey listener when unmount function is called", async () => {
|
|
133
|
+
const plugin = new Plugin({ name: "deleteByKey" });
|
|
134
|
+
const newCache = createCache(client.addPlugin(plugin));
|
|
135
|
+
|
|
136
|
+
const unmount = newCache.events.onDeleteByKey(request.cacheKey, spy);
|
|
137
|
+
unmount();
|
|
138
|
+
newCache.delete(request.cacheKey);
|
|
139
|
+
|
|
140
|
+
expect(spy).not.toHaveBeenCalled();
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
describe("when invalidate event is triggered", () => {
|
|
144
|
+
it("should invalidate cache using cache key", async () => {
|
|
145
|
+
cache.set(request, {
|
|
146
|
+
data: {},
|
|
147
|
+
error: null,
|
|
148
|
+
status: 200,
|
|
149
|
+
success: true,
|
|
150
|
+
extra: xhrExtra,
|
|
151
|
+
retries: 0,
|
|
152
|
+
requestTimestamp: +new Date(),
|
|
153
|
+
responseTimestamp: +new Date(),
|
|
154
|
+
addedTimestamp: +new Date(),
|
|
155
|
+
triggerTimestamp: +new Date(),
|
|
156
|
+
isCanceled: false,
|
|
157
|
+
isOffline: false,
|
|
158
|
+
});
|
|
159
|
+
cache.events.onInvalidateByKey(cacheKey, spy);
|
|
160
|
+
await cache.invalidate(cacheKey);
|
|
161
|
+
await sleep(1);
|
|
162
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
163
|
+
});
|
|
164
|
+
it("should invalidate cache using regex", async () => {
|
|
165
|
+
cache.set(request, {
|
|
166
|
+
data: null,
|
|
167
|
+
error: null,
|
|
168
|
+
status: 200,
|
|
169
|
+
success: true,
|
|
170
|
+
extra: xhrExtra,
|
|
171
|
+
retries: 0,
|
|
172
|
+
requestTimestamp: +new Date(),
|
|
173
|
+
responseTimestamp: +new Date(),
|
|
174
|
+
addedTimestamp: +new Date(),
|
|
175
|
+
triggerTimestamp: +new Date(),
|
|
176
|
+
isCanceled: false,
|
|
177
|
+
isOffline: false,
|
|
178
|
+
});
|
|
179
|
+
cache.events.onInvalidateByKey(cacheKey, spy);
|
|
180
|
+
await cache.invalidate(new RegExp(cacheKey));
|
|
181
|
+
await sleep(1);
|
|
182
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
183
|
+
});
|
|
184
|
+
it("should trigger onInvalidate event when cache is invalidated", async () => {
|
|
185
|
+
cache.set(request, cacheData);
|
|
186
|
+
cache.events.onInvalidate(spy);
|
|
187
|
+
|
|
188
|
+
await cache.invalidate(cacheKey);
|
|
189
|
+
await sleep(1);
|
|
190
|
+
|
|
191
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
192
|
+
expect(spy).toHaveBeenCalledWith(cacheKey);
|
|
193
|
+
});
|
|
194
|
+
it("should properly cleanup onInvalidate listener when unmount function is called", async () => {
|
|
195
|
+
cache.set(request, cacheData);
|
|
196
|
+
const unmount = cache.events.onInvalidate(spy);
|
|
197
|
+
|
|
198
|
+
unmount();
|
|
199
|
+
await cache.invalidate(cacheKey);
|
|
200
|
+
await sleep(1);
|
|
201
|
+
|
|
202
|
+
expect(spy).not.toHaveBeenCalled();
|
|
203
|
+
});
|
|
204
|
+
it("should properly cleanup onInvalidateByKey listener when unmount function is called", async () => {
|
|
205
|
+
cache.set(request, cacheData);
|
|
206
|
+
const unmount = cache.events.onInvalidateByKey(cacheKey, spy);
|
|
207
|
+
|
|
208
|
+
unmount();
|
|
209
|
+
await cache.invalidate(cacheKey);
|
|
210
|
+
await sleep(1);
|
|
211
|
+
|
|
212
|
+
expect(spy).not.toHaveBeenCalled();
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|