@khanacademy/wonder-blocks-data 13.0.10 → 13.0.12
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/CHANGELOG.md +15 -0
- package/package.json +3 -3
- package/src/components/__tests__/data.test.tsx +0 -832
- package/src/components/__tests__/gql-router.test.tsx +0 -63
- package/src/components/__tests__/intercept-requests.test.tsx +0 -57
- package/src/components/__tests__/track-data.test.tsx +0 -56
- package/src/components/data.ts +0 -73
- package/src/components/gql-router.tsx +0 -63
- package/src/components/intercept-context.ts +0 -19
- package/src/components/intercept-requests.tsx +0 -67
- package/src/components/track-data.tsx +0 -28
- package/src/hooks/__tests__/__snapshots__/use-shared-cache.test.ts.snap +0 -17
- package/src/hooks/__tests__/use-cached-effect.test.tsx +0 -789
- package/src/hooks/__tests__/use-gql-router-context.test.tsx +0 -132
- package/src/hooks/__tests__/use-gql.test.tsx +0 -204
- package/src/hooks/__tests__/use-hydratable-effect.test.ts +0 -708
- package/src/hooks/__tests__/use-request-interception.test.tsx +0 -254
- package/src/hooks/__tests__/use-server-effect.test.ts +0 -293
- package/src/hooks/__tests__/use-shared-cache.test.ts +0 -263
- package/src/hooks/use-cached-effect.ts +0 -297
- package/src/hooks/use-gql-router-context.ts +0 -49
- package/src/hooks/use-gql.ts +0 -58
- package/src/hooks/use-hydratable-effect.ts +0 -201
- package/src/hooks/use-request-interception.ts +0 -53
- package/src/hooks/use-server-effect.ts +0 -75
- package/src/hooks/use-shared-cache.ts +0 -107
- package/src/index.ts +0 -46
- package/src/util/__tests__/__snapshots__/scoped-in-memory-cache.test.ts.snap +0 -19
- package/src/util/__tests__/__snapshots__/serializable-in-memory-cache.test.ts.snap +0 -19
- package/src/util/__tests__/get-gql-data-from-response.test.ts +0 -186
- package/src/util/__tests__/get-gql-request-id.test.ts +0 -132
- package/src/util/__tests__/graphql-document-node-parser.test.ts +0 -535
- package/src/util/__tests__/hydration-cache-api.test.ts +0 -34
- package/src/util/__tests__/merge-gql-context.test.ts +0 -73
- package/src/util/__tests__/purge-caches.test.ts +0 -28
- package/src/util/__tests__/request-api.test.ts +0 -176
- package/src/util/__tests__/request-fulfillment.test.ts +0 -146
- package/src/util/__tests__/request-tracking.test.tsx +0 -321
- package/src/util/__tests__/result-from-cache-response.test.ts +0 -79
- package/src/util/__tests__/scoped-in-memory-cache.test.ts +0 -316
- package/src/util/__tests__/serializable-in-memory-cache.test.ts +0 -397
- package/src/util/__tests__/ssr-cache.test.ts +0 -636
- package/src/util/__tests__/to-gql-operation.test.ts +0 -41
- package/src/util/data-error.ts +0 -63
- package/src/util/get-gql-data-from-response.ts +0 -65
- package/src/util/get-gql-request-id.ts +0 -106
- package/src/util/gql-error.ts +0 -43
- package/src/util/gql-router-context.ts +0 -9
- package/src/util/gql-types.ts +0 -64
- package/src/util/graphql-document-node-parser.ts +0 -132
- package/src/util/graphql-types.ts +0 -28
- package/src/util/hydration-cache-api.ts +0 -30
- package/src/util/merge-gql-context.ts +0 -35
- package/src/util/purge-caches.ts +0 -14
- package/src/util/request-api.ts +0 -65
- package/src/util/request-fulfillment.ts +0 -121
- package/src/util/request-tracking.ts +0 -211
- package/src/util/result-from-cache-response.ts +0 -30
- package/src/util/scoped-in-memory-cache.ts +0 -121
- package/src/util/serializable-in-memory-cache.ts +0 -44
- package/src/util/ssr-cache.ts +0 -193
- package/src/util/status.ts +0 -35
- package/src/util/to-gql-operation.ts +0 -43
- package/src/util/types.ts +0 -145
- package/tsconfig-build.json +0 -12
- package/tsconfig-build.tsbuildinfo +0 -1
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {resultFromCachedResponse} from "../result-from-cache-response";
|
|
2
|
-
|
|
3
|
-
describe("#resultFromCachedResponse", () => {
|
|
4
|
-
it("should return null cache entry is null", () => {
|
|
5
|
-
// Arrange
|
|
6
|
-
const cacheEntry = null;
|
|
7
|
-
|
|
8
|
-
// Act
|
|
9
|
-
const result = resultFromCachedResponse(cacheEntry);
|
|
10
|
-
|
|
11
|
-
// Assert
|
|
12
|
-
expect(result).toBeNull();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it("should return success status if cache entry has data", () => {
|
|
16
|
-
// Arrange
|
|
17
|
-
const cacheEntry = {
|
|
18
|
-
data: "DATA",
|
|
19
|
-
error: undefined,
|
|
20
|
-
} as const;
|
|
21
|
-
|
|
22
|
-
// Act
|
|
23
|
-
const result = resultFromCachedResponse(cacheEntry);
|
|
24
|
-
|
|
25
|
-
// Assert
|
|
26
|
-
expect(result).toStrictEqual({
|
|
27
|
-
status: "success",
|
|
28
|
-
data: "DATA",
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("should return aborted status if cache entry has no data and no error", () => {
|
|
33
|
-
// Arrange
|
|
34
|
-
const cacheEntry: any = {
|
|
35
|
-
data: null,
|
|
36
|
-
error: null,
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// Act
|
|
40
|
-
const result = resultFromCachedResponse(cacheEntry);
|
|
41
|
-
|
|
42
|
-
// Assert
|
|
43
|
-
expect(result).toStrictEqual({
|
|
44
|
-
status: "aborted",
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("should return error status if cache entry has error", () => {
|
|
49
|
-
// Arrange
|
|
50
|
-
const cacheEntry: any = {
|
|
51
|
-
data: null,
|
|
52
|
-
error: "ERROR",
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
// Act
|
|
56
|
-
const result = resultFromCachedResponse(cacheEntry);
|
|
57
|
-
|
|
58
|
-
// Assert
|
|
59
|
-
expect(result).toStrictEqual({
|
|
60
|
-
status: "error",
|
|
61
|
-
error: expect.any(Error),
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("should hydrate the error into an error object", () => {
|
|
66
|
-
// Arrange
|
|
67
|
-
const cacheEntry: any = {
|
|
68
|
-
data: null,
|
|
69
|
-
error: "ERROR",
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
// Act
|
|
73
|
-
const cacheResult = resultFromCachedResponse(cacheEntry);
|
|
74
|
-
const error = cacheResult?.status === "error" && cacheResult.error;
|
|
75
|
-
|
|
76
|
-
// Assert
|
|
77
|
-
expect(error).toMatchInlineSnapshot(`[HydratedDataError: ERROR]`);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import {ScopedInMemoryCache} from "../scoped-in-memory-cache";
|
|
2
|
-
|
|
3
|
-
describe("ScopedInMemoryCache", () => {
|
|
4
|
-
describe("#set", () => {
|
|
5
|
-
it.each`
|
|
6
|
-
id
|
|
7
|
-
${null}
|
|
8
|
-
${""}
|
|
9
|
-
${5}
|
|
10
|
-
${() => "BOO"}
|
|
11
|
-
`("should throw if the id is $id", ({id}: any) => {
|
|
12
|
-
// Arrange
|
|
13
|
-
const cache = new ScopedInMemoryCache();
|
|
14
|
-
|
|
15
|
-
// Act
|
|
16
|
-
const underTest = () => cache.set("scope", id, "value");
|
|
17
|
-
|
|
18
|
-
// Assert
|
|
19
|
-
expect(underTest).toThrowErrorMatchingSnapshot();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it.each`
|
|
23
|
-
scope
|
|
24
|
-
${null}
|
|
25
|
-
${""}
|
|
26
|
-
${5}
|
|
27
|
-
${() => "BOO"}
|
|
28
|
-
`("should throw if the scope is $scope", ({scope}: any) => {
|
|
29
|
-
// Arrange
|
|
30
|
-
const cache = new ScopedInMemoryCache();
|
|
31
|
-
|
|
32
|
-
// Act
|
|
33
|
-
const underTest = () => cache.set(scope, "key", "value");
|
|
34
|
-
|
|
35
|
-
// Assert
|
|
36
|
-
expect(underTest).toThrowErrorMatchingSnapshot();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("should throw if the value is a function", () => {
|
|
40
|
-
// Arrange
|
|
41
|
-
const cache = new ScopedInMemoryCache();
|
|
42
|
-
|
|
43
|
-
// Act
|
|
44
|
-
const underTest = () => cache.set("scope", "key", () => "value");
|
|
45
|
-
|
|
46
|
-
// Assert
|
|
47
|
-
expect(underTest).toThrowErrorMatchingSnapshot();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("should store the entry in the cache", () => {
|
|
51
|
-
// Arrange
|
|
52
|
-
const cache = new ScopedInMemoryCache();
|
|
53
|
-
|
|
54
|
-
// Act
|
|
55
|
-
cache.set("scope", "key", "data");
|
|
56
|
-
const result = cache.get("scope", "key");
|
|
57
|
-
|
|
58
|
-
// Assert
|
|
59
|
-
expect(result).toStrictEqual("data");
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it("should replace the entry in the cache", () => {
|
|
63
|
-
// Arrange
|
|
64
|
-
const cache = new ScopedInMemoryCache({
|
|
65
|
-
scope: {
|
|
66
|
-
key: "data",
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// Act
|
|
71
|
-
cache.set("scope", "key", "other_data");
|
|
72
|
-
const result = cache.get("scope", "key");
|
|
73
|
-
|
|
74
|
-
// Assert
|
|
75
|
-
expect(result).toStrictEqual("other_data");
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
describe("#retrieve", () => {
|
|
80
|
-
it("should return null if the scope is not cached", () => {
|
|
81
|
-
// Arrange
|
|
82
|
-
const cache = new ScopedInMemoryCache();
|
|
83
|
-
|
|
84
|
-
// Act
|
|
85
|
-
const result = cache.get("scope", "key");
|
|
86
|
-
|
|
87
|
-
// Assert
|
|
88
|
-
expect(result).toBeNull();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it("should return null if the value is not in the cache scope", () => {
|
|
92
|
-
// Arrange
|
|
93
|
-
const cache = new ScopedInMemoryCache({
|
|
94
|
-
scope: {
|
|
95
|
-
key1: "data",
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// Act
|
|
100
|
-
const result = cache.get("scope", "key2");
|
|
101
|
-
|
|
102
|
-
// Assert
|
|
103
|
-
expect(result).toBeNull();
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("should return the entry if it exists", () => {
|
|
107
|
-
// Arrange
|
|
108
|
-
const cache = new ScopedInMemoryCache({
|
|
109
|
-
scope: {key: "value"},
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// Act
|
|
113
|
-
const result = cache.get("scope", "key");
|
|
114
|
-
|
|
115
|
-
// Assert
|
|
116
|
-
expect(result).toStrictEqual("value");
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
describe("#purge", () => {
|
|
121
|
-
it("should remove the entry", () => {
|
|
122
|
-
// Arrange
|
|
123
|
-
const cache = new ScopedInMemoryCache({
|
|
124
|
-
scope1: {
|
|
125
|
-
key1: "data1",
|
|
126
|
-
key2: "data2",
|
|
127
|
-
},
|
|
128
|
-
scope2: {
|
|
129
|
-
key1: "data1",
|
|
130
|
-
key2: "data2",
|
|
131
|
-
},
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// Act
|
|
135
|
-
cache.purge("scope1", "key2");
|
|
136
|
-
const result = cache._cache;
|
|
137
|
-
|
|
138
|
-
// Assert
|
|
139
|
-
expect(result).toStrictEqual({
|
|
140
|
-
scope1: {
|
|
141
|
-
key1: "data1",
|
|
142
|
-
},
|
|
143
|
-
scope2: {
|
|
144
|
-
key1: "data1",
|
|
145
|
-
key2: "data2",
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it("should remove the entire scope if the purged item is the last item in the scope", () => {
|
|
151
|
-
const cache = new ScopedInMemoryCache({
|
|
152
|
-
scope1: {
|
|
153
|
-
key2: "data2",
|
|
154
|
-
},
|
|
155
|
-
scope2: {
|
|
156
|
-
key1: "data1",
|
|
157
|
-
key2: "data2",
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
// Act
|
|
162
|
-
cache.purge("scope1", "key2");
|
|
163
|
-
const result = cache._cache;
|
|
164
|
-
|
|
165
|
-
// Assert
|
|
166
|
-
expect(result).toStrictEqual({
|
|
167
|
-
scope2: {
|
|
168
|
-
key1: "data1",
|
|
169
|
-
key2: "data2",
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
describe("#purgeScope", () => {
|
|
176
|
-
it("should remove matching entries only", () => {
|
|
177
|
-
// Arrange
|
|
178
|
-
const cache = new ScopedInMemoryCache({
|
|
179
|
-
scope1: {
|
|
180
|
-
key1: "a",
|
|
181
|
-
key2: "b",
|
|
182
|
-
key3: "a",
|
|
183
|
-
},
|
|
184
|
-
scope2: {
|
|
185
|
-
key1: "a",
|
|
186
|
-
key2: "b",
|
|
187
|
-
key3: "a",
|
|
188
|
-
},
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
// Act
|
|
192
|
-
cache.purgeScope("scope1", (id: any, value: any) => value === "a");
|
|
193
|
-
const result = cache._cache;
|
|
194
|
-
|
|
195
|
-
// Assert
|
|
196
|
-
expect(result).toStrictEqual({
|
|
197
|
-
scope1: {
|
|
198
|
-
key2: "b",
|
|
199
|
-
},
|
|
200
|
-
scope2: {
|
|
201
|
-
key1: "a",
|
|
202
|
-
key2: "b",
|
|
203
|
-
key3: "a",
|
|
204
|
-
},
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it("should remove the entire scope when there is no predicate", () => {
|
|
209
|
-
// Arrange
|
|
210
|
-
const cache = new ScopedInMemoryCache({
|
|
211
|
-
scope1: {
|
|
212
|
-
key1: "data1",
|
|
213
|
-
key2: "data2",
|
|
214
|
-
},
|
|
215
|
-
scope2: {
|
|
216
|
-
key1: "data1",
|
|
217
|
-
key2: "data2",
|
|
218
|
-
},
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
// Act
|
|
222
|
-
cache.purgeScope("scope1");
|
|
223
|
-
const result = cache._cache;
|
|
224
|
-
|
|
225
|
-
// Assert
|
|
226
|
-
expect(result).toStrictEqual({
|
|
227
|
-
scope2: {
|
|
228
|
-
key1: "data1",
|
|
229
|
-
key2: "data2",
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
it("should not throw if the scope does not exist", () => {
|
|
235
|
-
// Arrange
|
|
236
|
-
const cache = new ScopedInMemoryCache({
|
|
237
|
-
scope1: {
|
|
238
|
-
key1: "data1",
|
|
239
|
-
},
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
// Act
|
|
243
|
-
const act = () => cache.purgeScope("scope2");
|
|
244
|
-
|
|
245
|
-
// Arrange
|
|
246
|
-
expect(act).not.toThrow();
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
describe("#purgeAll", () => {
|
|
251
|
-
it("should remove matching entries only", () => {
|
|
252
|
-
const cache = new ScopedInMemoryCache({
|
|
253
|
-
scope1: {key: "2"},
|
|
254
|
-
scope2: {key: "1"},
|
|
255
|
-
scope3: {key: "2"},
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
// Act
|
|
259
|
-
cache.purgeAll((scope: any, id: any, value: any) => value === "2");
|
|
260
|
-
const result = cache._cache;
|
|
261
|
-
|
|
262
|
-
// Assert
|
|
263
|
-
expect(result).toStrictEqual({
|
|
264
|
-
scope2: {key: "1"},
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
it("should remove the all items if there is no predicate", () => {
|
|
269
|
-
const cache = new ScopedInMemoryCache({
|
|
270
|
-
scope1: {key: "2"},
|
|
271
|
-
scope2: {key: "1"},
|
|
272
|
-
scope3: {key: "2"},
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
// Act
|
|
276
|
-
cache.purgeAll();
|
|
277
|
-
const result = cache._cache;
|
|
278
|
-
|
|
279
|
-
// Assert
|
|
280
|
-
expect(result).toStrictEqual({});
|
|
281
|
-
});
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
describe("@inUse", () => {
|
|
285
|
-
it("should return true if the cache contains data", () => {
|
|
286
|
-
// Arrange
|
|
287
|
-
const cache = new ScopedInMemoryCache({
|
|
288
|
-
scope1: {key: "2"},
|
|
289
|
-
scope2: {key: "1"},
|
|
290
|
-
scope3: {key: "2"},
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
// Act
|
|
294
|
-
const result = cache.inUse;
|
|
295
|
-
|
|
296
|
-
// Assert
|
|
297
|
-
expect(result).toBeTruthy();
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
it("should return false if the cache is empty", () => {
|
|
301
|
-
// Arrange
|
|
302
|
-
const cache = new ScopedInMemoryCache({
|
|
303
|
-
scope1: {key: "2"},
|
|
304
|
-
scope2: {key: "1"},
|
|
305
|
-
scope3: {key: "2"},
|
|
306
|
-
});
|
|
307
|
-
cache.purgeAll();
|
|
308
|
-
|
|
309
|
-
// Act
|
|
310
|
-
const result = cache.inUse;
|
|
311
|
-
|
|
312
|
-
// Assert
|
|
313
|
-
expect(result).toBeFalsy();
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
});
|