@hyper-fetch/core 9.6.1 → 9.6.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/adapter/adapter.bindings.progress.spec.ts +120 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -6
- package/__tests__/features/adapter/adapter.defaults.spec.ts +87 -0
- package/__tests__/features/adapter/adapter.mapping.spec.ts +257 -0
- package/__tests__/features/cache/cache.lazy-storage.clear.spec.ts +145 -0
- package/__tests__/features/cache/cache.lazy-storage.resilience.spec.ts +667 -0
- package/__tests__/features/cache/cache.scoped-events.spec.ts +183 -0
- package/__tests__/features/cache/cache.semantics.spec.ts +785 -0
- package/__tests__/features/cache/cache.structural-sharing.spec.ts +565 -0
- package/__tests__/features/cache/cache.timing.spec.ts +448 -0
- package/__tests__/features/client/client.clear.spec.ts +313 -0
- package/__tests__/features/client/client.configuration.spec.ts +416 -0
- package/__tests__/features/client/client.hydration.round-trip.spec.ts +324 -0
- package/__tests__/features/client/client.interceptor.spec.ts +54 -45
- package/__tests__/features/client/client.interceptors.pipeline.spec.ts +599 -0
- package/__tests__/features/client/client.user-errors.spec.ts +220 -0
- package/__tests__/features/dispatcher/dispatcher.cleanup.spec.ts +534 -0
- package/__tests__/features/dispatcher/dispatcher.control.spec.ts +381 -0
- package/__tests__/features/dispatcher/dispatcher.modes.spec.ts +527 -0
- package/__tests__/features/dispatcher/dispatcher.offline.spec.ts +278 -0
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +35 -12
- package/__tests__/features/dispatcher/dispatcher.reentrancy.spec.ts +309 -0
- package/__tests__/features/dispatcher/dispatcher.response-order.spec.ts +241 -0
- package/__tests__/features/dispatcher/dispatcher.retry-flow.spec.ts +554 -0
- package/__tests__/features/dispatcher/dispatcher.retry-removal.spec.ts +196 -0
- package/__tests__/features/dispatcher/dispatcher.retry.spec.ts +4 -3
- package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +917 -0
- package/__tests__/features/managers/app/app.manager.events.spec.ts +434 -0
- package/__tests__/features/managers/logger/logger.levels.spec.ts +199 -0
- package/__tests__/features/managers/request/request.manager.abort.spec.ts +449 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +22 -3
- package/__tests__/features/mocker/mocker.errors.spec.ts +99 -0
- package/__tests__/features/mocker/mocker.outcomes.spec.ts +308 -0
- package/__tests__/features/plugin/plugin.lifecycle.spec.ts +457 -0
- package/__tests__/features/request/request.async-mapper-removal.spec.ts +138 -0
- package/__tests__/features/request/request.immutability.spec.ts +259 -0
- package/__tests__/features/request/request.inference.spec.ts +269 -0
- package/__tests__/features/request/request.lifecycle.spec.ts +682 -0
- package/__tests__/features/request/request.optimistic-settlement.spec.ts +35 -17
- package/__tests__/features/request/request.values.spec.ts +408 -0
- package/__tests__/features/sdk/sdk.builder.spec.ts +304 -0
- package/__tests__/features/utils/emitter.listeners.spec.ts +202 -0
- package/__tests__/utils/controlled-adapter.utils.ts +119 -0
- package/__tests__/utils/fixtures.utils.ts +193 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/cache/cache.d.ts +12 -1
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/cache/cache.events.d.ts +20 -6
- package/dist/cache/cache.events.d.ts.map +1 -1
- package/dist/cache/cache.utils.d.ts +1 -0
- package/dist/cache/cache.utils.d.ts.map +1 -1
- package/dist/client/client.d.ts +3 -3
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.utils.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.d.ts +26 -5
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/index.mjs +2127 -1741
- package/dist/index.mjs.map +1 -1
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
- package/dist/managers/app/app.manager.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -1
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
- package/dist/managers/logger/logger.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.types.d.ts +2 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -1
- package/dist/mocker/mocker.d.ts +1 -1
- package/dist/mocker/mocker.d.ts.map +1 -1
- package/dist/plugin/plugin.d.ts +2 -0
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/request/request.d.ts +1 -1
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/utils/emitter.utils.d.ts.map +1 -1
- package/dist/utils/error.utils.d.ts +17 -0
- package/dist/utils/error.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.bindings.ts +46 -18
- package/src/adapter/adapter.utils.ts +47 -7
- package/src/cache/cache.events.ts +24 -8
- package/src/cache/cache.ts +109 -21
- package/src/cache/cache.utils.ts +33 -10
- package/src/client/client.ts +99 -7
- package/src/client/client.utils.ts +10 -4
- package/src/dispatcher/dispatcher.ts +273 -133
- package/src/http-adapter/http-adapter.fetch.ts +15 -3
- package/src/managers/app/app.manager.constants.ts +12 -1
- package/src/managers/app/app.manager.ts +43 -23
- package/src/managers/app/app.manager.utils.ts +8 -4
- package/src/managers/logger/logger.manager.ts +21 -8
- package/src/managers/request/request.manager.ts +7 -1
- package/src/managers/request/request.manager.types.ts +2 -0
- package/src/mocker/mocker.ts +14 -11
- package/src/plugin/plugin.ts +6 -0
- package/src/request/request.ts +31 -22
- package/src/request/request.utils.ts +23 -15
- package/src/utils/emitter.utils.ts +9 -2
- package/src/utils/error.utils.ts +29 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,785 @@
|
|
|
1
|
+
import type { CacheValueType } from "cache";
|
|
2
|
+
import { Client } from "client";
|
|
3
|
+
import { Plugin } from "plugin";
|
|
4
|
+
import { scopeKey } from "request";
|
|
5
|
+
|
|
6
|
+
import { createCacheErrorResponse, createCacheResponse } from "../../utils/fixtures.utils";
|
|
7
|
+
|
|
8
|
+
type User = { id: number; name: string };
|
|
9
|
+
|
|
10
|
+
describe("Cache [ Semantics ]", () => {
|
|
11
|
+
let client = new Client({ url: "shared-base-url" });
|
|
12
|
+
let request = client.createRequest<{ response: any; error: { message: string } }>()({ endpoint: "/users" });
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
client = new Client({ url: "shared-base-url" });
|
|
16
|
+
request = client.createRequest<{ response: any; error: { message: string } }>()({ endpoint: "/users" });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
client.cache.clear();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("When reading the data", () => {
|
|
24
|
+
it("should return undefined for a key that was never set", () => {
|
|
25
|
+
expect(client.cache.get(request.cacheKey)).toBeUndefined();
|
|
26
|
+
expect(request.read()).toBeUndefined();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return the same reference on consecutive reads", () => {
|
|
30
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
31
|
+
|
|
32
|
+
expect(client.cache.get(request.cacheKey)).toBe(client.cache.get(request.cacheKey));
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should read the response through the request instance", () => {
|
|
36
|
+
const response = createCacheResponse<User>({ id: 1, name: "Kate" });
|
|
37
|
+
client.cache.set(request, response);
|
|
38
|
+
|
|
39
|
+
expect(request.read()).toStrictEqual({
|
|
40
|
+
data: { id: 1, name: "Kate" },
|
|
41
|
+
error: null,
|
|
42
|
+
status: 200,
|
|
43
|
+
success: true,
|
|
44
|
+
extra: response.extra,
|
|
45
|
+
requestTimestamp: response.requestTimestamp,
|
|
46
|
+
responseTimestamp: response.responseTimestamp,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should list only the keys of the stored elements", () => {
|
|
51
|
+
client.cache.set(request.setCacheKey("first"), createCacheResponse(1));
|
|
52
|
+
client.cache.set(request.setCacheKey("second"), createCacheResponse(2));
|
|
53
|
+
client.cache.set(request.setCacheKey("skipped").setCache(false), createCacheResponse(3));
|
|
54
|
+
|
|
55
|
+
expect(client.cache.keys()).toStrictEqual(["first", "second"]);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("When setting the data", () => {
|
|
60
|
+
it.each([
|
|
61
|
+
{ name: "zero", value: 0 },
|
|
62
|
+
{ name: "false", value: false },
|
|
63
|
+
{ name: "empty string", value: "" },
|
|
64
|
+
{ name: "empty array", value: [] },
|
|
65
|
+
{ name: "empty object", value: {} },
|
|
66
|
+
])("should store the falsy response data - $name", ({ value }) => {
|
|
67
|
+
client.cache.set(request, createCacheResponse(value));
|
|
68
|
+
|
|
69
|
+
expect(client.cache.get(request.cacheKey)?.data).toStrictEqual(value);
|
|
70
|
+
expect(request.read()?.data).toStrictEqual(value);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should emit the falsy response data to the listeners", () => {
|
|
74
|
+
const spy = vi.fn();
|
|
75
|
+
client.cache.events.onDataByKey(request.cacheKey, spy);
|
|
76
|
+
|
|
77
|
+
client.cache.set(request, createCacheResponse(0));
|
|
78
|
+
|
|
79
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
80
|
+
expect(spy.mock.calls[0][0].data).toBe(0);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should store null when the successful response has no data", () => {
|
|
84
|
+
const response: Partial<ReturnType<typeof createCacheResponse>> = createCacheResponse(1);
|
|
85
|
+
delete response.data;
|
|
86
|
+
|
|
87
|
+
client.cache.set(request, response as ReturnType<typeof createCacheResponse>);
|
|
88
|
+
|
|
89
|
+
expect(client.cache.get(request.cacheKey)?.data).toBeNull();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("should store the request settings together with the response", () => {
|
|
93
|
+
const configured = request.setStaleTime(1234).setCacheTime(5678);
|
|
94
|
+
client.cache.set(configured, createCacheResponse({ id: 1 }));
|
|
95
|
+
|
|
96
|
+
expect(client.cache.get(configured.cacheKey)).toStrictEqual(
|
|
97
|
+
expect.objectContaining({
|
|
98
|
+
cacheKey: configured.cacheKey,
|
|
99
|
+
scope: null,
|
|
100
|
+
staleTime: 1234,
|
|
101
|
+
cacheTime: 5678,
|
|
102
|
+
version: client.cache.version,
|
|
103
|
+
cached: true,
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should replace the element with the latest response", () => {
|
|
109
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
110
|
+
client.cache.set(request, createCacheResponse({ id: 2 }, { status: 201 }));
|
|
111
|
+
|
|
112
|
+
expect(client.cache.get(request.cacheKey)).toStrictEqual(
|
|
113
|
+
expect.objectContaining({ data: { id: 2 }, status: 201 }),
|
|
114
|
+
);
|
|
115
|
+
expect(client.cache.keys()).toHaveLength(1);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should pass null to the setter function when there is no cached element", () => {
|
|
119
|
+
const setter = vi.fn(() => createCacheResponse({ id: 1 }));
|
|
120
|
+
|
|
121
|
+
client.cache.set(request, setter);
|
|
122
|
+
|
|
123
|
+
expect(setter).toHaveBeenCalledTimes(1);
|
|
124
|
+
expect(setter).toHaveBeenCalledWith(null);
|
|
125
|
+
expect(client.cache.get(request.cacheKey)?.data).toStrictEqual({ id: 1 });
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("should pass the cached element to the setter function", () => {
|
|
129
|
+
client.cache.set(request, createCacheResponse({ count: 1 }));
|
|
130
|
+
const previous = client.cache.get(request.cacheKey);
|
|
131
|
+
const setter = vi.fn((prev: { data: { count: number } | null } | null) =>
|
|
132
|
+
createCacheResponse({ count: (prev?.data?.count ?? 0) + 1 }),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
client.cache.set(request, setter);
|
|
136
|
+
|
|
137
|
+
expect(setter).toHaveBeenCalledWith(previous);
|
|
138
|
+
expect(client.cache.get(request.cacheKey)?.data).toStrictEqual({ count: 2 });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("should keep the elements of different cache keys apart", () => {
|
|
142
|
+
const listRequest = client.createRequest<{ response: any; queryParams: { page: number } }>()({
|
|
143
|
+
endpoint: "/users",
|
|
144
|
+
});
|
|
145
|
+
const first = listRequest.setQueryParams({ page: 1 });
|
|
146
|
+
const second = listRequest.setQueryParams({ page: 2 });
|
|
147
|
+
|
|
148
|
+
client.cache.set(first, createCacheResponse("first"));
|
|
149
|
+
client.cache.set(second, createCacheResponse("second"));
|
|
150
|
+
|
|
151
|
+
expect(first.cacheKey).not.toBe(second.cacheKey);
|
|
152
|
+
expect(first.read()?.data).toBe("first");
|
|
153
|
+
expect(second.read()?.data).toBe("second");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("should share the element between requests with the same cache key", () => {
|
|
157
|
+
const first = request.setCacheKey("shared");
|
|
158
|
+
const second = client.createRequest<{ response: any }>()({ endpoint: "/other", cacheKey: "shared" });
|
|
159
|
+
|
|
160
|
+
client.cache.set(first, createCacheResponse("value"));
|
|
161
|
+
|
|
162
|
+
expect(second.read()?.data).toBe("value");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("should not store the response of a request with the cache disabled", () => {
|
|
166
|
+
const spy = vi.fn();
|
|
167
|
+
const uncached = request.setCache(false);
|
|
168
|
+
client.cache.events.onDataByKey(uncached.cacheKey, spy);
|
|
169
|
+
|
|
170
|
+
client.cache.set(uncached, createCacheResponse({ id: 1 }));
|
|
171
|
+
|
|
172
|
+
expect(client.cache.get(uncached.cacheKey)).toBeUndefined();
|
|
173
|
+
expect(client.cache.keys()).toHaveLength(0);
|
|
174
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
175
|
+
expect(spy.mock.calls[0][0]).toStrictEqual(expect.objectContaining({ data: { id: 1 }, cached: false }));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("should not schedule the garbage collection for a response which was not stored", () => {
|
|
179
|
+
client.cache.set(request.setCache(false), createCacheResponse({ id: 1 }));
|
|
180
|
+
|
|
181
|
+
expect(client.cache.garbageCollectors.size).toBe(0);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe("When the error response arrives", () => {
|
|
186
|
+
it("should not store the error response when nothing is cached", () => {
|
|
187
|
+
client.cache.set(request, createCacheErrorResponse({ message: "Failed" }));
|
|
188
|
+
|
|
189
|
+
expect(client.cache.get(request.cacheKey)).toBeUndefined();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("should emit the error response when nothing is cached", () => {
|
|
193
|
+
const spy = vi.fn();
|
|
194
|
+
client.cache.events.onDataByKey(request.cacheKey, spy);
|
|
195
|
+
|
|
196
|
+
client.cache.set(request, createCacheErrorResponse({ message: "Failed" }));
|
|
197
|
+
|
|
198
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
199
|
+
expect(spy.mock.calls[0][0]).toStrictEqual(
|
|
200
|
+
expect.objectContaining({ data: null, error: { message: "Failed" }, success: false }),
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("should keep the cached data untouched", () => {
|
|
205
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
206
|
+
const cached = client.cache.get(request.cacheKey);
|
|
207
|
+
|
|
208
|
+
client.cache.set(request, createCacheErrorResponse({ message: "Failed" }));
|
|
209
|
+
|
|
210
|
+
expect(client.cache.get(request.cacheKey)).toBe(cached);
|
|
211
|
+
expect(request.read()).toStrictEqual(expect.objectContaining({ data: { id: 1 }, error: null, success: true }));
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should emit the error together with the previously cached data", () => {
|
|
215
|
+
const spy = vi.fn();
|
|
216
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
217
|
+
client.cache.events.onDataByKey(request.cacheKey, spy);
|
|
218
|
+
|
|
219
|
+
client.cache.set(request, createCacheErrorResponse({ message: "Failed" }));
|
|
220
|
+
|
|
221
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
222
|
+
expect(spy.mock.calls[0][0]).toStrictEqual(
|
|
223
|
+
expect.objectContaining({ data: { id: 1 }, error: { message: "Failed" }, success: false, status: 400 }),
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("should keep the falsy cached data in the emitted error response", () => {
|
|
228
|
+
const spy = vi.fn();
|
|
229
|
+
client.cache.set(request, createCacheResponse(0));
|
|
230
|
+
client.cache.events.onDataByKey(request.cacheKey, spy);
|
|
231
|
+
|
|
232
|
+
client.cache.set(request, createCacheErrorResponse({ message: "Failed" }));
|
|
233
|
+
|
|
234
|
+
expect(spy.mock.calls[0][0].data).toBe(0);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("should recover with the next successful response", () => {
|
|
238
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
239
|
+
client.cache.set(request, createCacheErrorResponse({ message: "Failed" }));
|
|
240
|
+
client.cache.set(request, createCacheResponse({ id: 2 }));
|
|
241
|
+
|
|
242
|
+
expect(request.read()).toStrictEqual(expect.objectContaining({ data: { id: 2 }, error: null, success: true }));
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe("When updating the data", () => {
|
|
247
|
+
it("should not create the element when nothing is cached", () => {
|
|
248
|
+
const spy = vi.fn();
|
|
249
|
+
client.cache.events.onData(spy);
|
|
250
|
+
|
|
251
|
+
client.cache.update(request, { data: { id: 1 } });
|
|
252
|
+
|
|
253
|
+
expect(client.cache.get(request.cacheKey)).toBeUndefined();
|
|
254
|
+
expect(spy).not.toHaveBeenCalled();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("should merge the partial response with the cached element", () => {
|
|
258
|
+
const response = createCacheResponse<User>({ id: 1, name: "Kate" }, { status: 201 });
|
|
259
|
+
client.cache.set(request, response);
|
|
260
|
+
|
|
261
|
+
client.cache.update(request, { data: { id: 1, name: "Maya" } });
|
|
262
|
+
|
|
263
|
+
expect(client.cache.get(request.cacheKey)).toStrictEqual(
|
|
264
|
+
expect.objectContaining({
|
|
265
|
+
data: { id: 1, name: "Maya" },
|
|
266
|
+
status: 201,
|
|
267
|
+
success: true,
|
|
268
|
+
responseTimestamp: response.responseTimestamp,
|
|
269
|
+
requestTimestamp: response.requestTimestamp,
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("should update the data to a falsy value", () => {
|
|
275
|
+
client.cache.set(request, createCacheResponse(5));
|
|
276
|
+
|
|
277
|
+
client.cache.update(request, { data: 0 });
|
|
278
|
+
|
|
279
|
+
expect(client.cache.get(request.cacheKey)?.data).toBe(0);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("should pass the cached element to the update function", () => {
|
|
283
|
+
client.cache.set(request, createCacheResponse({ items: [1, 2] }));
|
|
284
|
+
|
|
285
|
+
client.cache.update(request, (previous) => ({ data: { items: [...(previous?.data?.items ?? []), 3] } }));
|
|
286
|
+
|
|
287
|
+
expect(client.cache.get(request.cacheKey)?.data).toStrictEqual({ items: [1, 2, 3] });
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("should emit the updated element", () => {
|
|
291
|
+
const spy = vi.fn();
|
|
292
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
293
|
+
client.cache.events.onDataByKey(request.cacheKey, spy);
|
|
294
|
+
|
|
295
|
+
client.cache.update(request, { data: { id: 2 } });
|
|
296
|
+
|
|
297
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
298
|
+
expect(spy.mock.calls[0][0].data).toStrictEqual({ id: 2 });
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it("should update only the element of the given request", () => {
|
|
302
|
+
const other = request.setCacheKey("other");
|
|
303
|
+
client.cache.set(request, createCacheResponse("first"));
|
|
304
|
+
client.cache.set(other, createCacheResponse("second"));
|
|
305
|
+
|
|
306
|
+
client.cache.update(request, { data: "changed" });
|
|
307
|
+
|
|
308
|
+
expect(request.read()?.data).toBe("changed");
|
|
309
|
+
expect(other.read()?.data).toBe("second");
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
describe("When deleting the data", () => {
|
|
314
|
+
it("should remove the element and notify the listeners", () => {
|
|
315
|
+
const globalSpy = vi.fn();
|
|
316
|
+
const keySpy = vi.fn();
|
|
317
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
318
|
+
client.cache.events.onDelete(globalSpy);
|
|
319
|
+
client.cache.events.onDeleteByKey(request.cacheKey, keySpy);
|
|
320
|
+
|
|
321
|
+
client.cache.delete(request.cacheKey);
|
|
322
|
+
|
|
323
|
+
expect(client.cache.get(request.cacheKey)).toBeUndefined();
|
|
324
|
+
expect(client.cache.keys()).toHaveLength(0);
|
|
325
|
+
expect(globalSpy).toHaveBeenCalledTimes(1);
|
|
326
|
+
expect(globalSpy).toHaveBeenCalledWith(request.cacheKey);
|
|
327
|
+
expect(keySpy).toHaveBeenCalledTimes(1);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("should remove only the given element", () => {
|
|
331
|
+
const other = request.setCacheKey("other");
|
|
332
|
+
const otherSpy = vi.fn();
|
|
333
|
+
client.cache.set(request, createCacheResponse("first"));
|
|
334
|
+
client.cache.set(other, createCacheResponse("second"));
|
|
335
|
+
client.cache.events.onDeleteByKey(other.cacheKey, otherSpy);
|
|
336
|
+
|
|
337
|
+
client.cache.delete(request.cacheKey);
|
|
338
|
+
|
|
339
|
+
expect(other.read()?.data).toBe("second");
|
|
340
|
+
expect(otherSpy).not.toHaveBeenCalled();
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("should allow to set the element again after it was deleted", () => {
|
|
344
|
+
client.cache.set(request, createCacheResponse("first"));
|
|
345
|
+
client.cache.delete(request.cacheKey);
|
|
346
|
+
client.cache.set(request, createCacheResponse("second"));
|
|
347
|
+
|
|
348
|
+
expect(request.read()?.data).toBe("second");
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("should not pass the deleted element to the setter function", () => {
|
|
352
|
+
const setter = vi.fn(() => createCacheResponse("second"));
|
|
353
|
+
client.cache.set(request, createCacheResponse("first"));
|
|
354
|
+
client.cache.delete(request.cacheKey);
|
|
355
|
+
|
|
356
|
+
client.cache.set(request, setter);
|
|
357
|
+
|
|
358
|
+
expect(setter).toHaveBeenCalledWith(null);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe("When invalidating the data", () => {
|
|
363
|
+
it("should mark the element as stale and keep its data", () => {
|
|
364
|
+
client.cache.set(request.setStaleTime(60_000), createCacheResponse({ id: 1 }));
|
|
365
|
+
|
|
366
|
+
client.cache.invalidate(request.cacheKey);
|
|
367
|
+
|
|
368
|
+
expect(client.cache.get(request.cacheKey)).toStrictEqual(
|
|
369
|
+
expect.objectContaining({ data: { id: 1 }, staleTime: 0, success: true }),
|
|
370
|
+
);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("should notify the listeners about a key without the cached data", () => {
|
|
374
|
+
const globalSpy = vi.fn();
|
|
375
|
+
const keySpy = vi.fn();
|
|
376
|
+
client.cache.events.onInvalidate(globalSpy);
|
|
377
|
+
client.cache.events.onInvalidateByKey("missing", keySpy);
|
|
378
|
+
|
|
379
|
+
client.cache.invalidate("missing");
|
|
380
|
+
|
|
381
|
+
expect(globalSpy).toHaveBeenCalledWith("missing");
|
|
382
|
+
expect(keySpy).toHaveBeenCalledTimes(1);
|
|
383
|
+
expect(client.cache.get("missing")).toBeUndefined();
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it("should invalidate only the elements matching the expression", () => {
|
|
387
|
+
client.cache.set(request.setCacheKey("users-1").setStaleTime(1000), createCacheResponse(1));
|
|
388
|
+
client.cache.set(request.setCacheKey("users-2").setStaleTime(1000), createCacheResponse(2));
|
|
389
|
+
client.cache.set(request.setCacheKey("posts-1").setStaleTime(1000), createCacheResponse(3));
|
|
390
|
+
|
|
391
|
+
client.cache.invalidate(/^users-/);
|
|
392
|
+
|
|
393
|
+
expect(client.cache.get("users-1")?.staleTime).toBe(0);
|
|
394
|
+
expect(client.cache.get("users-2")?.staleTime).toBe(0);
|
|
395
|
+
expect(client.cache.get("posts-1")?.staleTime).toBe(1000);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it.each([
|
|
399
|
+
{ name: "global", expression: /users/g },
|
|
400
|
+
{ name: "sticky", expression: /users/y },
|
|
401
|
+
{ name: "global and sticky", expression: /users/gy },
|
|
402
|
+
])("should invalidate every matching element with the $name expression", ({ expression }) => {
|
|
403
|
+
const spy = vi.fn();
|
|
404
|
+
const keys = ["users-1", "users-2", "users-3", "users-4"];
|
|
405
|
+
keys.forEach((key) => client.cache.set(request.setCacheKey(key).setStaleTime(1000), createCacheResponse(key)));
|
|
406
|
+
client.cache.events.onInvalidate(spy);
|
|
407
|
+
|
|
408
|
+
client.cache.invalidate(expression);
|
|
409
|
+
|
|
410
|
+
expect(spy.mock.calls.map(([key]) => key)).toStrictEqual(keys);
|
|
411
|
+
keys.forEach((key) => expect(client.cache.get(key)?.staleTime).toBe(0));
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("should not change the state of the passed expression", () => {
|
|
415
|
+
const expression = /users/g;
|
|
416
|
+
client.cache.set(request.setCacheKey("users-1"), createCacheResponse(1));
|
|
417
|
+
client.cache.set(request.setCacheKey("users-2"), createCacheResponse(2));
|
|
418
|
+
|
|
419
|
+
client.cache.invalidate(expression);
|
|
420
|
+
|
|
421
|
+
expect(expression.lastIndex).toBe(0);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("should respect the case insensitive expression", () => {
|
|
425
|
+
client.cache.set(request.setCacheKey("USERS").setStaleTime(1000), createCacheResponse(1));
|
|
426
|
+
|
|
427
|
+
client.cache.invalidate(/users/i);
|
|
428
|
+
|
|
429
|
+
expect(client.cache.get("USERS")?.staleTime).toBe(0);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it("should invalidate every element when called without arguments", () => {
|
|
433
|
+
const spy = vi.fn();
|
|
434
|
+
client.cache.set(request.setCacheKey("first").setStaleTime(1000), createCacheResponse(1));
|
|
435
|
+
client.cache.set(request.setCacheKey("second").setStaleTime(1000), createCacheResponse(2));
|
|
436
|
+
client.cache.events.onInvalidate(spy);
|
|
437
|
+
|
|
438
|
+
client.cache.invalidate();
|
|
439
|
+
|
|
440
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
441
|
+
expect(client.cache.get("first")?.staleTime).toBe(0);
|
|
442
|
+
expect(client.cache.get("second")?.staleTime).toBe(0);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("should not notify anyone when the cache is empty and the expression is used", () => {
|
|
446
|
+
const spy = vi.fn();
|
|
447
|
+
client.cache.events.onInvalidate(spy);
|
|
448
|
+
|
|
449
|
+
client.cache.invalidate(/.*/);
|
|
450
|
+
client.cache.invalidate();
|
|
451
|
+
|
|
452
|
+
expect(spy).not.toHaveBeenCalled();
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("should invalidate the list of keys, requests and expressions", () => {
|
|
456
|
+
const byRequest = request.setCacheKey("by-request").setStaleTime(1000);
|
|
457
|
+
client.cache.set(byRequest, createCacheResponse(1));
|
|
458
|
+
client.cache.set(request.setCacheKey("by-key").setStaleTime(1000), createCacheResponse(2));
|
|
459
|
+
client.cache.set(request.setCacheKey("by-expression").setStaleTime(1000), createCacheResponse(3));
|
|
460
|
+
client.cache.set(request.setCacheKey("untouched").setStaleTime(1000), createCacheResponse(4));
|
|
461
|
+
|
|
462
|
+
client.cache.invalidate([byRequest, "by-key", /expression$/]);
|
|
463
|
+
|
|
464
|
+
expect(client.cache.get("by-request")?.staleTime).toBe(0);
|
|
465
|
+
expect(client.cache.get("by-key")?.staleTime).toBe(0);
|
|
466
|
+
expect(client.cache.get("by-expression")?.staleTime).toBe(0);
|
|
467
|
+
expect(client.cache.get("untouched")?.staleTime).toBe(1000);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("should make the element fresh again with the next response", () => {
|
|
471
|
+
const configured = request.setStaleTime(60_000);
|
|
472
|
+
client.cache.set(configured, createCacheResponse({ id: 1 }));
|
|
473
|
+
client.cache.invalidate(configured);
|
|
474
|
+
|
|
475
|
+
client.cache.set(configured, createCacheResponse({ id: 2 }));
|
|
476
|
+
|
|
477
|
+
expect(client.cache.get(configured.cacheKey)).toStrictEqual(
|
|
478
|
+
expect.objectContaining({ data: { id: 2 }, staleTime: 60_000 }),
|
|
479
|
+
);
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it("should not emit the data event", () => {
|
|
483
|
+
const spy = vi.fn();
|
|
484
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
485
|
+
client.cache.events.onData(spy);
|
|
486
|
+
|
|
487
|
+
client.cache.invalidate(request.cacheKey);
|
|
488
|
+
|
|
489
|
+
expect(spy).not.toHaveBeenCalled();
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it("should not reschedule the garbage collection", () => {
|
|
493
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
494
|
+
const collector = client.cache.garbageCollectors.get(request.cacheKey);
|
|
495
|
+
|
|
496
|
+
client.cache.invalidate(request.cacheKey);
|
|
497
|
+
|
|
498
|
+
expect(client.cache.garbageCollectors.get(request.cacheKey)).toBe(collector);
|
|
499
|
+
});
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
describe("When requests are scoped", () => {
|
|
503
|
+
it("should keep the same cache key apart for different scopes", () => {
|
|
504
|
+
const first = request.setScope("user-1");
|
|
505
|
+
const second = request.setScope("user-2");
|
|
506
|
+
|
|
507
|
+
client.cache.set(first, createCacheResponse("first"));
|
|
508
|
+
client.cache.set(second, createCacheResponse("second"));
|
|
509
|
+
|
|
510
|
+
expect(first.cacheKey).toBe(second.cacheKey);
|
|
511
|
+
expect(first.read()?.data).toBe("first");
|
|
512
|
+
expect(second.read()?.data).toBe("second");
|
|
513
|
+
expect(request.read()).toBeUndefined();
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it("should store the scope and the logical cache key in the element", () => {
|
|
517
|
+
const scoped = request.setScope("user-1");
|
|
518
|
+
|
|
519
|
+
client.cache.set(scoped, createCacheResponse("value"));
|
|
520
|
+
|
|
521
|
+
expect(client.cache.keys()).toStrictEqual([scopeKey(scoped.cacheKey, "user-1")]);
|
|
522
|
+
expect(client.cache.get(scopeKey(scoped.cacheKey, "user-1"))).toStrictEqual(
|
|
523
|
+
expect.objectContaining({ cacheKey: scoped.cacheKey, scope: "user-1" }),
|
|
524
|
+
);
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
it("should update only the element of the given scope", () => {
|
|
528
|
+
const first = request.setScope("user-1");
|
|
529
|
+
const second = request.setScope("user-2");
|
|
530
|
+
client.cache.set(first, createCacheResponse("first"));
|
|
531
|
+
client.cache.set(second, createCacheResponse("second"));
|
|
532
|
+
|
|
533
|
+
client.cache.update(first, { data: "changed" });
|
|
534
|
+
|
|
535
|
+
expect(first.read()?.data).toBe("changed");
|
|
536
|
+
expect(second.read()?.data).toBe("second");
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
it("should invalidate only the element of the given scope", () => {
|
|
540
|
+
const first = request.setScope("user-1").setStaleTime(1000);
|
|
541
|
+
const second = request.setScope("user-2").setStaleTime(1000);
|
|
542
|
+
client.cache.set(first, createCacheResponse("first"));
|
|
543
|
+
client.cache.set(second, createCacheResponse("second"));
|
|
544
|
+
|
|
545
|
+
client.cache.invalidate(first);
|
|
546
|
+
|
|
547
|
+
expect(client.cache.get(scopeKey(first.cacheKey, "user-1"))?.staleTime).toBe(0);
|
|
548
|
+
expect(client.cache.get(scopeKey(second.cacheKey, "user-2"))?.staleTime).toBe(1000);
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
it("should pass only the element of the same scope to the setter function", () => {
|
|
552
|
+
const first = request.setScope("user-1");
|
|
553
|
+
const setter = vi.fn(() => createCacheResponse("second"));
|
|
554
|
+
client.cache.set(first, createCacheResponse("first"));
|
|
555
|
+
|
|
556
|
+
client.cache.set(request.setScope("user-2"), setter);
|
|
557
|
+
|
|
558
|
+
expect(setter).toHaveBeenCalledWith(null);
|
|
559
|
+
});
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
describe("When cache events are emitted", () => {
|
|
563
|
+
it("should store the element before the listeners are notified", () => {
|
|
564
|
+
let readInListener: CacheValueType | undefined;
|
|
565
|
+
client.cache.events.onDataByKey(request.cacheKey, () => {
|
|
566
|
+
readInListener = client.cache.get(request.cacheKey);
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
570
|
+
|
|
571
|
+
expect(readInListener?.data).toStrictEqual({ id: 1 });
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it("should remove the element before the listeners are notified", () => {
|
|
575
|
+
let readInListener: CacheValueType | undefined = undefined as CacheValueType | undefined;
|
|
576
|
+
const spy = vi.fn(() => {
|
|
577
|
+
readInListener = client.cache.get(request.cacheKey);
|
|
578
|
+
});
|
|
579
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
580
|
+
client.cache.events.onDeleteByKey(request.cacheKey, spy);
|
|
581
|
+
|
|
582
|
+
client.cache.delete(request.cacheKey);
|
|
583
|
+
|
|
584
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
585
|
+
expect(readInListener).toBeUndefined();
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
it("should mark the element as stale before the listeners are notified", () => {
|
|
589
|
+
let staleTimeInListener: number | undefined;
|
|
590
|
+
client.cache.set(request.setStaleTime(1000), createCacheResponse({ id: 1 }));
|
|
591
|
+
client.cache.events.onInvalidateByKey(request.cacheKey, () => {
|
|
592
|
+
staleTimeInListener = client.cache.get(request.cacheKey)?.staleTime;
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
client.cache.invalidate(request.cacheKey);
|
|
596
|
+
|
|
597
|
+
expect(staleTimeInListener).toBe(0);
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
it("should notify the general listeners before the listeners of the key", () => {
|
|
601
|
+
const order: string[] = [];
|
|
602
|
+
client.cache.events.onDataByKey(request.cacheKey, () => order.push("key"));
|
|
603
|
+
client.cache.events.onData(() => order.push("general"));
|
|
604
|
+
|
|
605
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
606
|
+
|
|
607
|
+
expect(order).toStrictEqual(["general", "key"]);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it("should trigger the plugins before the listeners", () => {
|
|
611
|
+
const order: string[] = [];
|
|
612
|
+
client.addPlugin(new Plugin({ name: "order" }).onCacheItemChange(() => order.push("plugin")));
|
|
613
|
+
client.cache.events.onData(() => order.push("listener"));
|
|
614
|
+
|
|
615
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
616
|
+
|
|
617
|
+
expect(order).toStrictEqual(["plugin", "listener"]);
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
it("should pass the previous and the new element to the plugins", () => {
|
|
621
|
+
const spy = vi.fn();
|
|
622
|
+
client.addPlugin(new Plugin({ name: "change" }).onCacheItemChange(spy));
|
|
623
|
+
|
|
624
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
625
|
+
const first = client.cache.get(request.cacheKey);
|
|
626
|
+
client.cache.set(request, createCacheResponse({ id: 2 }));
|
|
627
|
+
const second = client.cache.get(request.cacheKey);
|
|
628
|
+
|
|
629
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
630
|
+
expect(spy.mock.calls[0][0]).toStrictEqual({
|
|
631
|
+
cache: client.cache,
|
|
632
|
+
cacheKey: request.cacheKey,
|
|
633
|
+
prevData: null,
|
|
634
|
+
newData: first,
|
|
635
|
+
});
|
|
636
|
+
expect(spy.mock.calls[1][0]).toStrictEqual({
|
|
637
|
+
cache: client.cache,
|
|
638
|
+
cacheKey: request.cacheKey,
|
|
639
|
+
prevData: first,
|
|
640
|
+
newData: second,
|
|
641
|
+
});
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it("should not trigger the change plugins for a response which was not stored", () => {
|
|
645
|
+
const spy = vi.fn();
|
|
646
|
+
client.addPlugin(new Plugin({ name: "change" }).onCacheItemChange(spy));
|
|
647
|
+
|
|
648
|
+
client.cache.set(request.setCache(false), createCacheResponse({ id: 1 }));
|
|
649
|
+
client.cache.set(request, createCacheErrorResponse({ message: "Failed" }));
|
|
650
|
+
|
|
651
|
+
expect(spy).not.toHaveBeenCalled();
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it("should notify the remaining listeners when one of them unsubscribes during the emit", () => {
|
|
655
|
+
const second = vi.fn();
|
|
656
|
+
const unsubscribeFirst = client.cache.events.onData(() => unsubscribeFirst());
|
|
657
|
+
client.cache.events.onData(second);
|
|
658
|
+
|
|
659
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
660
|
+
client.cache.set(request, createCacheResponse({ id: 2 }));
|
|
661
|
+
|
|
662
|
+
expect(second).toHaveBeenCalledTimes(2);
|
|
663
|
+
expect(client.cache.emitter.listenerCount("cache")).toBe(1);
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
it("should notify a listener which gets removed by an earlier listener during the same emit", () => {
|
|
667
|
+
const second = vi.fn();
|
|
668
|
+
let unsubscribeSecond: VoidFunction = () => {};
|
|
669
|
+
client.cache.events.onData(() => unsubscribeSecond());
|
|
670
|
+
unsubscribeSecond = client.cache.events.onData(second);
|
|
671
|
+
|
|
672
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
673
|
+
client.cache.set(request, createCacheResponse({ id: 2 }));
|
|
674
|
+
|
|
675
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
it("should not notify a listener added during the emit about the current change", () => {
|
|
679
|
+
const late = vi.fn();
|
|
680
|
+
const unsubscribe = client.cache.events.onData(() => {
|
|
681
|
+
unsubscribe();
|
|
682
|
+
client.cache.events.onData(late);
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
686
|
+
expect(late).not.toHaveBeenCalled();
|
|
687
|
+
|
|
688
|
+
client.cache.set(request, createCacheResponse({ id: 2 }));
|
|
689
|
+
expect(late).toHaveBeenCalledTimes(1);
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
it("should allow to set another element from the listener", () => {
|
|
693
|
+
const other = request.setCacheKey("other");
|
|
694
|
+
client.cache.events.onDataByKey(request.cacheKey, (data) => {
|
|
695
|
+
client.cache.set(other, createCacheResponse({ copy: data.data }));
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
699
|
+
|
|
700
|
+
expect(other.read()?.data).toStrictEqual({ copy: { id: 1 } });
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
it("should not notify the listeners of other keys", () => {
|
|
704
|
+
const spy = vi.fn();
|
|
705
|
+
client.cache.events.onDataByKey("other", spy);
|
|
706
|
+
client.cache.events.onDeleteByKey("other", spy);
|
|
707
|
+
client.cache.events.onInvalidateByKey("other", spy);
|
|
708
|
+
|
|
709
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
710
|
+
client.cache.invalidate(request.cacheKey);
|
|
711
|
+
client.cache.delete(request.cacheKey);
|
|
712
|
+
|
|
713
|
+
expect(spy).not.toHaveBeenCalled();
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
it("should stop notifying the listener after it unsubscribes", () => {
|
|
717
|
+
const spy = vi.fn();
|
|
718
|
+
const unsubscribe = client.cache.events.onData(spy);
|
|
719
|
+
|
|
720
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
721
|
+
unsubscribe();
|
|
722
|
+
unsubscribe();
|
|
723
|
+
client.cache.set(request, createCacheResponse({ id: 2 }));
|
|
724
|
+
|
|
725
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
it("should leave no listeners behind after every subscription is removed", () => {
|
|
729
|
+
const unsubscribes = [
|
|
730
|
+
client.cache.events.onData(() => {}),
|
|
731
|
+
client.cache.events.onDataByKey(request.cacheKey, () => {}),
|
|
732
|
+
client.cache.events.onInvalidate(() => {}),
|
|
733
|
+
client.cache.events.onInvalidateByKey(request.cacheKey, () => {}),
|
|
734
|
+
client.cache.events.onDelete(() => {}),
|
|
735
|
+
client.cache.events.onDeleteByKey(request.cacheKey, () => {}),
|
|
736
|
+
];
|
|
737
|
+
const listenersCount = () =>
|
|
738
|
+
client.cache.emitter.eventNames().reduce((sum, name) => sum + client.cache.emitter.listenerCount(name), 0);
|
|
739
|
+
|
|
740
|
+
expect(listenersCount()).toBe(6);
|
|
741
|
+
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
|
742
|
+
|
|
743
|
+
expect(listenersCount()).toBe(0);
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
it("should pass the external trigger flag to the emit observers", () => {
|
|
747
|
+
const spy = vi.fn();
|
|
748
|
+
client.cache.emitter.onEmit(spy);
|
|
749
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
750
|
+
const element = client.cache.get(request.cacheKey) as CacheValueType;
|
|
751
|
+
spy.mockClear();
|
|
752
|
+
|
|
753
|
+
client.cache.events.emitCacheData(element, true);
|
|
754
|
+
client.cache.events.emitInvalidation(request.cacheKey, true);
|
|
755
|
+
client.cache.events.emitDelete(request.cacheKey, true);
|
|
756
|
+
|
|
757
|
+
expect(spy.mock.calls.every((call) => call[2] === true)).toBeTrue();
|
|
758
|
+
expect(spy).toHaveBeenCalledTimes(6);
|
|
759
|
+
});
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
describe("When cache gets cleared", () => {
|
|
763
|
+
it("should remove every element from the storage", async () => {
|
|
764
|
+
client.cache.set(request.setCacheKey("first"), createCacheResponse(1));
|
|
765
|
+
client.cache.set(request.setCacheKey("second"), createCacheResponse(2));
|
|
766
|
+
|
|
767
|
+
await client.cache.clear();
|
|
768
|
+
|
|
769
|
+
expect(client.cache.keys()).toHaveLength(0);
|
|
770
|
+
expect(client.cache.get("first")).toBeUndefined();
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
it("should keep working after it was cleared", async () => {
|
|
774
|
+
const spy = vi.fn();
|
|
775
|
+
client.cache.events.onData(spy);
|
|
776
|
+
client.cache.set(request, createCacheResponse(1));
|
|
777
|
+
|
|
778
|
+
await client.cache.clear();
|
|
779
|
+
client.cache.set(request, createCacheResponse(2));
|
|
780
|
+
|
|
781
|
+
expect(request.read()?.data).toBe(2);
|
|
782
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
783
|
+
});
|
|
784
|
+
});
|
|
785
|
+
});
|