@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,565 @@
|
|
|
1
|
+
import { isPlainObject, replaceEqualDeep } from "cache";
|
|
2
|
+
import { Client } from "client";
|
|
3
|
+
|
|
4
|
+
import { createCacheErrorResponse, createCacheResponse } from "../../utils/fixtures.utils";
|
|
5
|
+
|
|
6
|
+
type Nested = { child?: Nested; value?: number };
|
|
7
|
+
|
|
8
|
+
class Model {
|
|
9
|
+
constructor(public id: number) {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const createNested = (depth: number): Nested => {
|
|
13
|
+
const root: Nested = {};
|
|
14
|
+
let current = root;
|
|
15
|
+
for (let i = 0; i < depth; i += 1) {
|
|
16
|
+
current.child = {};
|
|
17
|
+
current = current.child;
|
|
18
|
+
}
|
|
19
|
+
current.value = 1;
|
|
20
|
+
return root;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const getDepth = (value: Nested): number => {
|
|
24
|
+
let depth = 0;
|
|
25
|
+
let current: Nested | undefined = value;
|
|
26
|
+
while (current?.child) {
|
|
27
|
+
depth += 1;
|
|
28
|
+
current = current.child;
|
|
29
|
+
}
|
|
30
|
+
return depth;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
describe("Cache [ Structural Sharing ]", () => {
|
|
34
|
+
describe("When comparing primitives", () => {
|
|
35
|
+
it.each([
|
|
36
|
+
{ name: "numbers", prev: 1, next: 1 },
|
|
37
|
+
{ name: "strings", prev: "value", next: "value" },
|
|
38
|
+
{ name: "booleans", prev: false, next: false },
|
|
39
|
+
{ name: "null", prev: null, next: null },
|
|
40
|
+
{ name: "undefined", prev: undefined, next: undefined },
|
|
41
|
+
{ name: "NaN", prev: NaN, next: NaN },
|
|
42
|
+
])("should return the previous value for equal $name", ({ prev, next }) => {
|
|
43
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it.each([
|
|
47
|
+
{ name: "a different value", prev: 1, next: 2 },
|
|
48
|
+
{ name: "a different type", prev: 1, next: "1" },
|
|
49
|
+
{ name: "null instead of undefined", prev: undefined, next: null },
|
|
50
|
+
{ name: "undefined instead of null", prev: null, next: undefined },
|
|
51
|
+
{ name: "false instead of zero", prev: 0, next: false },
|
|
52
|
+
{ name: "an empty string instead of zero", prev: 0, next: "" },
|
|
53
|
+
])("should return the next value for $name", ({ prev, next }) => {
|
|
54
|
+
expect(replaceEqualDeep(prev, next)).toBe(next);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should tell the negative zero from the positive zero", () => {
|
|
58
|
+
expect(Object.is(replaceEqualDeep(0, -0), -0)).toBeTrue();
|
|
59
|
+
expect(Object.is(replaceEqualDeep(-0, 0), 0)).toBeTrue();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should tell the nested negative zero from the positive zero", () => {
|
|
63
|
+
const prev = { value: 0, list: [0] };
|
|
64
|
+
|
|
65
|
+
const result = replaceEqualDeep(prev, { value: -0, list: [-0] });
|
|
66
|
+
|
|
67
|
+
expect(result).not.toBe(prev);
|
|
68
|
+
expect(Object.is(result.value, -0)).toBeTrue();
|
|
69
|
+
expect(Object.is(result.list[0], -0)).toBeTrue();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should share the values which contain NaN", () => {
|
|
73
|
+
const prev = { value: NaN, list: [NaN, { nested: NaN }] };
|
|
74
|
+
const next = { value: NaN, list: [NaN, { nested: NaN }] };
|
|
75
|
+
|
|
76
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should return the next value when a primitive replaces an object and the other way round", () => {
|
|
80
|
+
const object = { id: 1 };
|
|
81
|
+
|
|
82
|
+
expect(replaceEqualDeep(object, 1)).toBe(1);
|
|
83
|
+
expect(replaceEqualDeep(1, object)).toBe(object);
|
|
84
|
+
expect(replaceEqualDeep(object, null)).toBeNull();
|
|
85
|
+
expect(replaceEqualDeep(null, object)).toBe(object);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("When comparing objects", () => {
|
|
90
|
+
it("should return the previous object when the keys come in a different order", () => {
|
|
91
|
+
const prev = { id: 1, name: "Kate", tags: ["admin"] };
|
|
92
|
+
const next = { tags: ["admin"], name: "Kate", id: 1 };
|
|
93
|
+
|
|
94
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should replace every parent of the changed value and keep the other branches", () => {
|
|
98
|
+
const prev = { user: { profile: { name: "Kate" }, settings: { theme: "dark" } }, posts: [{ id: 1 }] };
|
|
99
|
+
const next = { user: { profile: { name: "Maya" }, settings: { theme: "dark" } }, posts: [{ id: 1 }] };
|
|
100
|
+
|
|
101
|
+
const result = replaceEqualDeep(prev, next);
|
|
102
|
+
|
|
103
|
+
expect(result).toStrictEqual(next);
|
|
104
|
+
expect(result).not.toBe(prev);
|
|
105
|
+
expect(result.user).not.toBe(prev.user);
|
|
106
|
+
expect(result.user.profile).not.toBe(prev.user.profile);
|
|
107
|
+
expect(result.user.settings).toBe(prev.user.settings);
|
|
108
|
+
expect(result.posts).toBe(prev.posts);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should detect the added key", () => {
|
|
112
|
+
const prev = { id: 1 };
|
|
113
|
+
const next = { id: 1, name: "Kate" };
|
|
114
|
+
|
|
115
|
+
const result = replaceEqualDeep(prev, next);
|
|
116
|
+
|
|
117
|
+
expect(result).not.toBe(prev);
|
|
118
|
+
expect(result).toStrictEqual(next);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should detect the removed key", () => {
|
|
122
|
+
const prev = { id: 1, name: "Kate" };
|
|
123
|
+
const next = { id: 1 };
|
|
124
|
+
|
|
125
|
+
const result = replaceEqualDeep(prev, next);
|
|
126
|
+
|
|
127
|
+
expect(result).not.toBe(prev);
|
|
128
|
+
expect(result).toStrictEqual(next);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("should detect the renamed key with the same value", () => {
|
|
132
|
+
const prev = { first: 1 };
|
|
133
|
+
const next = { second: 1 };
|
|
134
|
+
|
|
135
|
+
expect(replaceEqualDeep(prev, next)).toStrictEqual({ second: 1 });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("should handle the same number of keys when one of them holds undefined", () => {
|
|
139
|
+
const prev = { a: 1, b: undefined };
|
|
140
|
+
const next = { a: 1, c: undefined };
|
|
141
|
+
|
|
142
|
+
const result = replaceEqualDeep<Record<string, unknown>>(prev, next);
|
|
143
|
+
|
|
144
|
+
expect(result).not.toBe(prev);
|
|
145
|
+
expect(Object.keys(result)).toStrictEqual(["a", "c"]);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("should share the values which contain undefined", () => {
|
|
149
|
+
const prev = { a: { b: undefined }, c: undefined };
|
|
150
|
+
const next = { a: { b: undefined }, c: undefined };
|
|
151
|
+
|
|
152
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("should treat objects without the prototype like plain objects", () => {
|
|
156
|
+
const prev = Object.assign(Object.create(null), { nested: { id: 1 } });
|
|
157
|
+
const next = Object.assign(Object.create(null), { nested: { id: 1 } });
|
|
158
|
+
|
|
159
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
160
|
+
expect(replaceEqualDeep(prev, { nested: { id: 1 } })).toBe(prev);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("should share the objects with own constructor property", () => {
|
|
164
|
+
const prev = { constructor: "value", id: 1 };
|
|
165
|
+
const next = { constructor: "value", id: 1 };
|
|
166
|
+
|
|
167
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("should not change the compared values", () => {
|
|
171
|
+
const prev = Object.freeze({ list: Object.freeze([Object.freeze({ id: 1 })]), name: "Kate" });
|
|
172
|
+
const next = Object.freeze({ list: Object.freeze([Object.freeze({ id: 1 })]), name: "Maya" });
|
|
173
|
+
|
|
174
|
+
const result = replaceEqualDeep(prev, next);
|
|
175
|
+
|
|
176
|
+
expect(result).toStrictEqual({ list: [{ id: 1 }], name: "Maya" });
|
|
177
|
+
expect(result.list).toBe(prev.list);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("should share the same object placed twice in the next value", () => {
|
|
181
|
+
const shared = { id: 1 };
|
|
182
|
+
const prev = { first: { id: 1 }, second: { id: 1 } };
|
|
183
|
+
const next = { first: shared, second: shared };
|
|
184
|
+
|
|
185
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe("When comparing arrays", () => {
|
|
190
|
+
it("should keep the references of the unchanged items", () => {
|
|
191
|
+
const prev = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|
192
|
+
const next = [{ id: 1 }, { id: 20 }, { id: 3 }];
|
|
193
|
+
|
|
194
|
+
const result = replaceEqualDeep(prev, next);
|
|
195
|
+
|
|
196
|
+
expect(result).not.toBe(prev);
|
|
197
|
+
expect(result).toStrictEqual(next);
|
|
198
|
+
expect(result[0]).toBe(prev[0]);
|
|
199
|
+
expect(result[1]).not.toBe(prev[1]);
|
|
200
|
+
expect(result[2]).toBe(prev[2]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("should return a copy when the next array is longer", () => {
|
|
204
|
+
const prev = [{ id: 1 }];
|
|
205
|
+
const next = [{ id: 1 }, { id: 2 }];
|
|
206
|
+
|
|
207
|
+
const result = replaceEqualDeep(prev, next);
|
|
208
|
+
|
|
209
|
+
expect(result).toStrictEqual(next);
|
|
210
|
+
expect(result).not.toBe(prev);
|
|
211
|
+
expect(result[0]).toBe(prev[0]);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should return a copy when the next array is shorter", () => {
|
|
215
|
+
const prev = [{ id: 1 }, { id: 2 }];
|
|
216
|
+
const next = [{ id: 1 }];
|
|
217
|
+
|
|
218
|
+
const result = replaceEqualDeep(prev, next);
|
|
219
|
+
|
|
220
|
+
expect(result).toStrictEqual(next);
|
|
221
|
+
expect(result).toHaveLength(1);
|
|
222
|
+
expect(result[0]).toBe(prev[0]);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("should compare the items by their position", () => {
|
|
226
|
+
const prev = [{ id: 1 }, { id: 2 }];
|
|
227
|
+
const next = [{ id: 2 }, { id: 1 }];
|
|
228
|
+
|
|
229
|
+
const result = replaceEqualDeep(prev, next);
|
|
230
|
+
|
|
231
|
+
expect(result).toStrictEqual(next);
|
|
232
|
+
expect(result[0]).not.toBe(prev[1]);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("should return the previous array of undefined values", () => {
|
|
236
|
+
const prev = [undefined, undefined];
|
|
237
|
+
const next = [undefined, undefined];
|
|
238
|
+
|
|
239
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("should return the previous array which contains undefined", () => {
|
|
243
|
+
const prev = [1, undefined, { id: 1 }];
|
|
244
|
+
const next = [1, undefined, { id: 1 }];
|
|
245
|
+
|
|
246
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("should return the result as an array", () => {
|
|
250
|
+
const result = replaceEqualDeep([{ id: 1 }], [{ id: 1 }, { id: 2 }]);
|
|
251
|
+
|
|
252
|
+
expect(Array.isArray(result)).toBeTrue();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("should not share the sparse arrays", () => {
|
|
256
|
+
// eslint-disable-next-line no-sparse-arrays
|
|
257
|
+
const prev = [1, , 3];
|
|
258
|
+
// eslint-disable-next-line no-sparse-arrays
|
|
259
|
+
const next = [1, , 3];
|
|
260
|
+
|
|
261
|
+
expect(replaceEqualDeep(prev, next)).toBe(next);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("should not share the arrays with additional properties", () => {
|
|
265
|
+
const prev = Object.assign([1, 2], { total: 2 });
|
|
266
|
+
const next = Object.assign([1, 2], { total: 2 });
|
|
267
|
+
|
|
268
|
+
expect(replaceEqualDeep(prev, next)).toBe(next);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("should not treat the array-like objects as arrays", () => {
|
|
272
|
+
const prev = [1, 2];
|
|
273
|
+
const next = { 0: 1, 1: 2 };
|
|
274
|
+
|
|
275
|
+
expect(replaceEqualDeep<unknown>(prev, next)).toBe(next);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe("When comparing values which are not plain objects", () => {
|
|
280
|
+
it.each([
|
|
281
|
+
{ name: "dates", create: () => new Date("2026-01-01T00:00:00.000Z") },
|
|
282
|
+
{ name: "maps", create: () => new Map([["key", 1]]) },
|
|
283
|
+
{ name: "sets", create: () => new Set([1]) },
|
|
284
|
+
{ name: "expressions", create: () => /value/ },
|
|
285
|
+
{ name: "class instances", create: () => new Model(1) },
|
|
286
|
+
{ name: "typed arrays", create: () => new Uint8Array([1, 2]) },
|
|
287
|
+
{ name: "errors", create: () => new Error("Failed") },
|
|
288
|
+
{ name: "functions", create: () => () => 1 },
|
|
289
|
+
])("should take the equal $name from the next value", ({ create }) => {
|
|
290
|
+
const prev = create();
|
|
291
|
+
const next = create();
|
|
292
|
+
|
|
293
|
+
expect(replaceEqualDeep<unknown>(prev, next)).toBe(next);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("should keep the same instance when it did not change", () => {
|
|
297
|
+
const date = new Date();
|
|
298
|
+
const model = new Model(1);
|
|
299
|
+
const prev = { date, model, nested: { id: 1 } };
|
|
300
|
+
const next = { date, model, nested: { id: 1 } };
|
|
301
|
+
|
|
302
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("should replace the parent of the instance which changed and keep its plain siblings", () => {
|
|
306
|
+
const prev = { createdAt: new Date(1), author: { name: "Kate" } };
|
|
307
|
+
const next = { createdAt: new Date(1), author: { name: "Kate" } };
|
|
308
|
+
|
|
309
|
+
const result = replaceEqualDeep(prev, next);
|
|
310
|
+
|
|
311
|
+
expect(result).not.toBe(prev);
|
|
312
|
+
expect(result.createdAt).toBe(next.createdAt);
|
|
313
|
+
expect(result.author).toBe(prev.author);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("should not look inside of the class instances", () => {
|
|
317
|
+
const prev = { model: Object.assign(new Model(1), { nested: { id: 1 } }) };
|
|
318
|
+
const next = { model: Object.assign(new Model(1), { nested: { id: 1 } }) };
|
|
319
|
+
|
|
320
|
+
const result = replaceEqualDeep(prev, next);
|
|
321
|
+
|
|
322
|
+
expect(result.model).toBe(next.model);
|
|
323
|
+
expect(result.model.nested).toBe(next.model.nested);
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
describe("When comparing circular and deep structures", () => {
|
|
328
|
+
it("should not overflow the stack for the circular structures", () => {
|
|
329
|
+
const prev: Record<string, unknown> = { id: 1 };
|
|
330
|
+
prev.self = prev;
|
|
331
|
+
const next: Record<string, unknown> = { id: 1 };
|
|
332
|
+
next.self = next;
|
|
333
|
+
|
|
334
|
+
const result = replaceEqualDeep(prev, next);
|
|
335
|
+
|
|
336
|
+
expect(result.id).toBe(1);
|
|
337
|
+
expect(result.self).toBe(next);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("should handle the structures which reference each other", () => {
|
|
341
|
+
const createPair = () => {
|
|
342
|
+
const parent: Record<string, unknown> = { name: "parent" };
|
|
343
|
+
const child: Record<string, unknown> = { name: "child", parent };
|
|
344
|
+
parent.child = child;
|
|
345
|
+
return parent;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const result = replaceEqualDeep(createPair(), createPair());
|
|
349
|
+
|
|
350
|
+
expect(result.name).toBe("parent");
|
|
351
|
+
expect((result.child as Record<string, unknown>).name).toBe("child");
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("should handle the circular arrays", () => {
|
|
355
|
+
const prev: unknown[] = [1];
|
|
356
|
+
prev.push(prev);
|
|
357
|
+
const next: unknown[] = [1];
|
|
358
|
+
next.push(next);
|
|
359
|
+
|
|
360
|
+
const result = replaceEqualDeep(prev, next);
|
|
361
|
+
|
|
362
|
+
expect(result[0]).toBe(1);
|
|
363
|
+
expect(result[1]).toBe(next);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it("should handle many circular references in a single structure", () => {
|
|
367
|
+
const createGraph = () => {
|
|
368
|
+
const node: Record<string, unknown> = { id: 1 };
|
|
369
|
+
for (let i = 0; i < 30; i += 1) {
|
|
370
|
+
node[`link${i}`] = node;
|
|
371
|
+
}
|
|
372
|
+
return node;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const result = replaceEqualDeep(createGraph(), createGraph());
|
|
376
|
+
|
|
377
|
+
expect(Object.keys(result)).toHaveLength(31);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("should handle the circular next value compared with the plain previous value", () => {
|
|
381
|
+
const prev = { id: 1, self: { id: 1, self: { id: 1 } } };
|
|
382
|
+
const next: Record<string, unknown> = { id: 1 };
|
|
383
|
+
next.self = next;
|
|
384
|
+
|
|
385
|
+
const result = replaceEqualDeep<Record<string, unknown>>(prev, next);
|
|
386
|
+
|
|
387
|
+
expect(result.id).toBe(1);
|
|
388
|
+
expect(result.self).toBe(next);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("should keep the unchanged branches next to the circular reference", () => {
|
|
392
|
+
const prev: Record<string, unknown> = { list: [{ id: 1 }] };
|
|
393
|
+
prev.self = prev;
|
|
394
|
+
const next: Record<string, unknown> = { list: [{ id: 1 }] };
|
|
395
|
+
next.self = next;
|
|
396
|
+
|
|
397
|
+
const result = replaceEqualDeep(prev, next);
|
|
398
|
+
|
|
399
|
+
expect(result.list).toBe(prev.list);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it("should not overflow the stack for the extremely deep structures", () => {
|
|
403
|
+
const depth = 20_000;
|
|
404
|
+
const prev = createNested(depth);
|
|
405
|
+
const next = createNested(depth);
|
|
406
|
+
|
|
407
|
+
const result = replaceEqualDeep(prev, next);
|
|
408
|
+
|
|
409
|
+
expect(getDepth(result)).toBe(depth);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it("should share the deep structures which fit in the depth limit", () => {
|
|
413
|
+
const prev = createNested(400);
|
|
414
|
+
const next = createNested(400);
|
|
415
|
+
|
|
416
|
+
expect(replaceEqualDeep(prev, next)).toBe(prev);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
describe("When checking plain objects", () => {
|
|
421
|
+
it.each([
|
|
422
|
+
{ name: "object literal", value: {} },
|
|
423
|
+
{ name: "object without the prototype", value: Object.create(null) },
|
|
424
|
+
{ name: "object with own constructor property", value: { constructor: null } },
|
|
425
|
+
])("should recognize the $name", ({ value }) => {
|
|
426
|
+
expect(isPlainObject(value)).toBeTrue();
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
it.each([
|
|
430
|
+
{ name: "null", value: null },
|
|
431
|
+
{ name: "undefined", value: undefined },
|
|
432
|
+
{ name: "array", value: [] },
|
|
433
|
+
{ name: "string", value: "value" },
|
|
434
|
+
{ name: "number", value: 1 },
|
|
435
|
+
{ name: "function", value: () => {} },
|
|
436
|
+
{ name: "date", value: new Date() },
|
|
437
|
+
{ name: "map", value: new Map() },
|
|
438
|
+
{ name: "set", value: new Set() },
|
|
439
|
+
{ name: "expression", value: /value/ },
|
|
440
|
+
{ name: "error", value: new Error("Failed") },
|
|
441
|
+
{ name: "promise", value: Promise.resolve() },
|
|
442
|
+
{ name: "class instance", value: new Model(1) },
|
|
443
|
+
{ name: "object with a custom prototype", value: Object.create({ inherited: true }) },
|
|
444
|
+
{ name: "object with a custom string tag", value: { [Symbol.toStringTag]: "Custom" } },
|
|
445
|
+
])("should reject the $name", ({ value }) => {
|
|
446
|
+
expect(isPlainObject(value)).toBeFalse();
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
describe("When the cache shares the data", () => {
|
|
451
|
+
let client = new Client({ url: "shared-base-url" });
|
|
452
|
+
let request = client.createRequest<{ response: any; error: { message: string } }>()({ endpoint: "/users" });
|
|
453
|
+
|
|
454
|
+
beforeEach(() => {
|
|
455
|
+
client = new Client({ url: "shared-base-url" });
|
|
456
|
+
request = client.createRequest<{ response: any; error: { message: string } }>()({ endpoint: "/users" });
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
afterEach(() => {
|
|
460
|
+
client.cache.clear();
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it("should keep the data reference when the same response arrives again", () => {
|
|
464
|
+
client.cache.set(request, createCacheResponse({ users: [{ id: 1 }], total: 1 }));
|
|
465
|
+
const first = request.read()?.data;
|
|
466
|
+
|
|
467
|
+
client.cache.set(request, createCacheResponse({ users: [{ id: 1 }], total: 1 }));
|
|
468
|
+
|
|
469
|
+
expect(request.read()?.data).toBe(first);
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
it("should emit the shared data to the listeners", () => {
|
|
473
|
+
const spy = vi.fn();
|
|
474
|
+
client.cache.set(request, createCacheResponse({ users: [{ id: 1 }] }));
|
|
475
|
+
const first = request.read()?.data;
|
|
476
|
+
client.cache.events.onDataByKey(request.cacheKey, spy);
|
|
477
|
+
|
|
478
|
+
client.cache.set(request, createCacheResponse({ users: [{ id: 1 }] }));
|
|
479
|
+
|
|
480
|
+
expect(spy.mock.calls[0][0].data).toBe(first);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it("should update the timestamps even when the data is shared", () => {
|
|
484
|
+
const responseTimestamp = Date.now();
|
|
485
|
+
client.cache.set(request, createCacheResponse({ id: 1 }, { responseTimestamp: responseTimestamp - 1000 }));
|
|
486
|
+
const first = client.cache.get(request.cacheKey);
|
|
487
|
+
|
|
488
|
+
client.cache.set(request, createCacheResponse({ id: 1 }, { responseTimestamp }));
|
|
489
|
+
|
|
490
|
+
expect(client.cache.get(request.cacheKey)?.responseTimestamp).toBe(responseTimestamp);
|
|
491
|
+
expect(request.read()?.data).toBe(first?.data);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("should keep the unchanged items of the list", () => {
|
|
495
|
+
client.cache.set(request, createCacheResponse([{ id: 1 }, { id: 2 }]));
|
|
496
|
+
const first = request.read()?.data;
|
|
497
|
+
|
|
498
|
+
client.cache.set(request, createCacheResponse([{ id: 1 }, { id: 2 }, { id: 3 }]));
|
|
499
|
+
const second = request.read()?.data;
|
|
500
|
+
|
|
501
|
+
expect(second).not.toBe(first);
|
|
502
|
+
expect(second[0]).toBe(first[0]);
|
|
503
|
+
expect(second[1]).toBe(first[1]);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("should replace the data when the structural sharing is disabled", () => {
|
|
507
|
+
const withoutSharing = request.setStructuralSharing(false);
|
|
508
|
+
const next = { id: 1 };
|
|
509
|
+
client.cache.set(withoutSharing, createCacheResponse({ id: 1 }));
|
|
510
|
+
|
|
511
|
+
client.cache.set(withoutSharing, createCacheResponse(next));
|
|
512
|
+
|
|
513
|
+
expect(request.read()?.data).toBe(next);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it("should share the data of the updates", () => {
|
|
517
|
+
client.cache.set(request, createCacheResponse({ users: [{ id: 1 }] }));
|
|
518
|
+
const first = request.read()?.data;
|
|
519
|
+
|
|
520
|
+
client.cache.update(request, { data: { users: [{ id: 1 }] } });
|
|
521
|
+
|
|
522
|
+
expect(request.read()?.data).toBe(first);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it("should share the data when a falsy value is replaced with the equal one", () => {
|
|
526
|
+
client.cache.set(request, createCacheResponse(0));
|
|
527
|
+
client.cache.set(request, createCacheResponse(0));
|
|
528
|
+
|
|
529
|
+
expect(request.read()?.data).toBe(0);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it("should emit the previous data reference together with the error", () => {
|
|
533
|
+
const spy = vi.fn();
|
|
534
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
535
|
+
const first = request.read()?.data;
|
|
536
|
+
client.cache.events.onDataByKey(request.cacheKey, spy);
|
|
537
|
+
|
|
538
|
+
client.cache.set(request, createCacheErrorResponse({ message: "Failed" }));
|
|
539
|
+
|
|
540
|
+
expect(spy.mock.calls[0][0].data).toBe(first);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it("should store the circular data without failing", () => {
|
|
544
|
+
const createCircular = () => {
|
|
545
|
+
const data: Record<string, unknown> = { id: 1 };
|
|
546
|
+
data.self = data;
|
|
547
|
+
return data;
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
client.cache.set(request, createCacheResponse(createCircular()));
|
|
551
|
+
client.cache.set(request, createCacheResponse(createCircular()));
|
|
552
|
+
|
|
553
|
+
expect(request.read()?.data.id).toBe(1);
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
it("should not share the data between different cache keys", () => {
|
|
557
|
+
const other = request.setCacheKey("other");
|
|
558
|
+
client.cache.set(request, createCacheResponse({ id: 1 }));
|
|
559
|
+
|
|
560
|
+
client.cache.set(other, createCacheResponse({ id: 1 }));
|
|
561
|
+
|
|
562
|
+
expect(other.read()?.data).not.toBe(request.read()?.data);
|
|
563
|
+
});
|
|
564
|
+
});
|
|
565
|
+
});
|