@khanacademy/wonder-blocks-data 13.0.11 → 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 +8 -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,636 +0,0 @@
|
|
|
1
|
-
import {Server} from "@khanacademy/wonder-blocks-core";
|
|
2
|
-
import {SsrCache} from "../ssr-cache";
|
|
3
|
-
import {SerializableInMemoryCache} from "../serializable-in-memory-cache";
|
|
4
|
-
|
|
5
|
-
describe("../ssr-cache.js", () => {
|
|
6
|
-
afterEach(() => {
|
|
7
|
-
/**
|
|
8
|
-
* This is needed or the JSON.stringify mocks need to be
|
|
9
|
-
* mockImplementationOnce. This is because if the snapshots need
|
|
10
|
-
* to update, they write the inline snapshot and that appears to invoke
|
|
11
|
-
* prettier which in turn, calls JSON.stringify. And if that mock
|
|
12
|
-
* throws, then boom. No snapshot update and a big old confusing test
|
|
13
|
-
* failure.
|
|
14
|
-
*/
|
|
15
|
-
jest.restoreAllMocks();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe("#constructor", () => {
|
|
19
|
-
it("should default the ssr-only cache to a cache instance", () => {
|
|
20
|
-
// Arrange
|
|
21
|
-
|
|
22
|
-
// Act
|
|
23
|
-
const cache = new SsrCache();
|
|
24
|
-
|
|
25
|
-
// Assert
|
|
26
|
-
expect(cache._ssrOnlyCache).toBeInstanceOf(
|
|
27
|
-
SerializableInMemoryCache,
|
|
28
|
-
);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("should set the hydration cache to the passed instance if there is one", () => {
|
|
32
|
-
// Arrange
|
|
33
|
-
const passedInstance = new SerializableInMemoryCache();
|
|
34
|
-
|
|
35
|
-
// Act
|
|
36
|
-
const cache = new SsrCache(null, passedInstance);
|
|
37
|
-
|
|
38
|
-
// Assert
|
|
39
|
-
expect(cache._ssrOnlyCache).toBe(passedInstance);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("should default the hydration cache to a cache instance", () => {
|
|
43
|
-
// Arrange
|
|
44
|
-
|
|
45
|
-
// Act
|
|
46
|
-
const cache = new SsrCache();
|
|
47
|
-
|
|
48
|
-
// Assert
|
|
49
|
-
expect(cache._hydrationCache).toBeInstanceOf(
|
|
50
|
-
SerializableInMemoryCache,
|
|
51
|
-
);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("should set the hydration cache to the passed instance if there is one", () => {
|
|
55
|
-
// Arrange
|
|
56
|
-
const passedInstance = new SerializableInMemoryCache();
|
|
57
|
-
|
|
58
|
-
// Act
|
|
59
|
-
const cache = new SsrCache(passedInstance);
|
|
60
|
-
|
|
61
|
-
// Assert
|
|
62
|
-
expect(cache._hydrationCache).toBe(passedInstance);
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
describe("@Default", () => {
|
|
67
|
-
it("should return an instance of SsrCache", () => {
|
|
68
|
-
// Arrange
|
|
69
|
-
|
|
70
|
-
// Act
|
|
71
|
-
const result = SsrCache.Default;
|
|
72
|
-
|
|
73
|
-
// Assert
|
|
74
|
-
expect(result).toBeInstanceOf(SsrCache);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("should return the same instance on each call", () => {
|
|
78
|
-
// Arrange
|
|
79
|
-
|
|
80
|
-
// Act
|
|
81
|
-
const result1 = SsrCache.Default;
|
|
82
|
-
const result2 = SsrCache.Default;
|
|
83
|
-
|
|
84
|
-
// Assert
|
|
85
|
-
expect(result1).toBe(result2);
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
describe("#initialize", () => {
|
|
90
|
-
it("should initialize the cache with the given data", () => {
|
|
91
|
-
// Arrange
|
|
92
|
-
const cache = new SsrCache();
|
|
93
|
-
|
|
94
|
-
// Act
|
|
95
|
-
cache.initialize({
|
|
96
|
-
MY_KEY: {data: "THE_DATA"},
|
|
97
|
-
});
|
|
98
|
-
const result = cache.getEntry("MY_KEY");
|
|
99
|
-
|
|
100
|
-
// Assert
|
|
101
|
-
expect(result).toStrictEqual({data: "THE_DATA"});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("should throw if the cache is already intialized", () => {
|
|
105
|
-
// Arrange
|
|
106
|
-
const internalCache = new SerializableInMemoryCache({
|
|
107
|
-
MY_KEY: {data: "THE_DATA"},
|
|
108
|
-
});
|
|
109
|
-
const cache = new SsrCache(internalCache);
|
|
110
|
-
|
|
111
|
-
// Act
|
|
112
|
-
const underTest = () =>
|
|
113
|
-
cache.initialize({
|
|
114
|
-
MY_OTHER_KEY: {data: "MORE_DATA"},
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
// Assert
|
|
118
|
-
expect(underTest).toThrowErrorMatchingInlineSnapshot(
|
|
119
|
-
`"Cannot initialize data response cache more than once"`,
|
|
120
|
-
);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("should deep clone the cached data", () => {
|
|
124
|
-
// Arrange
|
|
125
|
-
const cache = new SsrCache();
|
|
126
|
-
const sourceData = {
|
|
127
|
-
MY_KEY: {data: "THE_DATA"},
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
// Act
|
|
131
|
-
cache.initialize(sourceData);
|
|
132
|
-
// Try to mutate the cache.
|
|
133
|
-
sourceData["MY_KEY"] = {data: "SOME_NEW_DATA"};
|
|
134
|
-
const result = cache.getEntry("MY_KEY");
|
|
135
|
-
|
|
136
|
-
// Assert
|
|
137
|
-
expect(result).toStrictEqual({data: "THE_DATA"});
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
describe("#cacheData", () => {
|
|
142
|
-
describe("when client-side", () => {
|
|
143
|
-
it("should not store the entry in the hydration cache", () => {
|
|
144
|
-
// Arrange
|
|
145
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
146
|
-
const cache = new SsrCache(hydrationCache);
|
|
147
|
-
const hydrationStoreSpy = jest.spyOn(hydrationCache, "set");
|
|
148
|
-
|
|
149
|
-
// Act
|
|
150
|
-
cache.cacheData("MY_KEY", "data", true);
|
|
151
|
-
|
|
152
|
-
// Assert
|
|
153
|
-
expect(hydrationStoreSpy).not.toHaveBeenCalled();
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
it("should not store the entry in the ssrOnly cache", () => {
|
|
157
|
-
// Arrange
|
|
158
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
159
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
160
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
161
|
-
const ssrOnlyStoreSpy = jest.spyOn(ssrOnlyCache, "set");
|
|
162
|
-
|
|
163
|
-
// Act
|
|
164
|
-
cache.cacheData("MY_KEY", "data", false);
|
|
165
|
-
|
|
166
|
-
// Assert
|
|
167
|
-
expect(ssrOnlyStoreSpy).not.toHaveBeenCalled();
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
describe("when server-side", () => {
|
|
172
|
-
beforeEach(() => {
|
|
173
|
-
jest.spyOn(Server, "isServerSide").mockReturnValue(true);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
describe("when hydrate is true", () => {
|
|
177
|
-
it("should store the entry in the hydration cache", () => {
|
|
178
|
-
// Arrange
|
|
179
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
180
|
-
const cache = new SsrCache(hydrationCache);
|
|
181
|
-
const hydrationStoreSpy = jest.spyOn(hydrationCache, "set");
|
|
182
|
-
|
|
183
|
-
// Act
|
|
184
|
-
cache.cacheData("MY_KEY", "data", true);
|
|
185
|
-
|
|
186
|
-
// Assert
|
|
187
|
-
expect(hydrationStoreSpy).toHaveBeenCalledWith(
|
|
188
|
-
"default",
|
|
189
|
-
"MY_KEY",
|
|
190
|
-
{
|
|
191
|
-
data: "data",
|
|
192
|
-
},
|
|
193
|
-
);
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
it("should not store the entry in the ssrOnly cache", () => {
|
|
197
|
-
// Arrange
|
|
198
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
199
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
200
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
201
|
-
const ssrOnlyStoreSpy = jest.spyOn(ssrOnlyCache, "set");
|
|
202
|
-
|
|
203
|
-
// Act
|
|
204
|
-
cache.cacheData("MY_KEY", "data", true);
|
|
205
|
-
|
|
206
|
-
// Assert
|
|
207
|
-
expect(ssrOnlyStoreSpy).not.toHaveBeenCalled();
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
describe("when hydrate is false", () => {
|
|
212
|
-
it("should store the entry in the ssr-only cache", () => {
|
|
213
|
-
// Arrange
|
|
214
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
215
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
216
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
217
|
-
const ssrOnlyStoreSpy = jest.spyOn(ssrOnlyCache, "set");
|
|
218
|
-
|
|
219
|
-
// Act
|
|
220
|
-
cache.cacheData("MY_KEY", "data", false);
|
|
221
|
-
|
|
222
|
-
// Assert
|
|
223
|
-
expect(ssrOnlyStoreSpy).toHaveBeenCalledWith(
|
|
224
|
-
"default",
|
|
225
|
-
"MY_KEY",
|
|
226
|
-
{
|
|
227
|
-
data: "data",
|
|
228
|
-
},
|
|
229
|
-
);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it("should not store the entry in the hydration cache", () => {
|
|
233
|
-
// Arrange
|
|
234
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
235
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
236
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
237
|
-
const hydrationStoreSpy = jest.spyOn(hydrationCache, "set");
|
|
238
|
-
|
|
239
|
-
// Act
|
|
240
|
-
cache.cacheData("MY_KEY", "data", false);
|
|
241
|
-
|
|
242
|
-
// Assert
|
|
243
|
-
expect(hydrationStoreSpy).not.toHaveBeenCalled();
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
describe("#cacheError", () => {
|
|
250
|
-
describe("when client-side", () => {
|
|
251
|
-
it("should not store the entry in the hydration cache", () => {
|
|
252
|
-
// Arrange
|
|
253
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
254
|
-
const cache = new SsrCache(hydrationCache);
|
|
255
|
-
const hydrationStoreSpy = jest.spyOn(hydrationCache, "set");
|
|
256
|
-
|
|
257
|
-
// Act
|
|
258
|
-
cache.cacheError("MY_KEY", new Error("Ooops!"), true);
|
|
259
|
-
|
|
260
|
-
// Assert
|
|
261
|
-
expect(hydrationStoreSpy).not.toHaveBeenCalled();
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
it("should not store the entry in the ssrOnly cache", () => {
|
|
265
|
-
// Arrange
|
|
266
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
267
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
268
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
269
|
-
const ssrOnlyStoreSpy = jest.spyOn(ssrOnlyCache, "set");
|
|
270
|
-
|
|
271
|
-
// Act
|
|
272
|
-
cache.cacheError("MY_KEY", "Ooops!", false);
|
|
273
|
-
|
|
274
|
-
// Assert
|
|
275
|
-
expect(ssrOnlyStoreSpy).not.toHaveBeenCalled();
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
describe("when server-side", () => {
|
|
280
|
-
beforeEach(() => {
|
|
281
|
-
jest.spyOn(Server, "isServerSide").mockReturnValue(true);
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
describe("when hydrate is true", () => {
|
|
285
|
-
it("should store the entry in the hydration cache", () => {
|
|
286
|
-
// Arrange
|
|
287
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
288
|
-
const cache = new SsrCache(hydrationCache);
|
|
289
|
-
const hydrationStoreSpy = jest.spyOn(hydrationCache, "set");
|
|
290
|
-
|
|
291
|
-
// Act
|
|
292
|
-
cache.cacheError("MY_KEY", new Error("Ooops!"), true);
|
|
293
|
-
|
|
294
|
-
// Assert
|
|
295
|
-
expect(hydrationStoreSpy).toHaveBeenCalledWith(
|
|
296
|
-
"default",
|
|
297
|
-
"MY_KEY",
|
|
298
|
-
{
|
|
299
|
-
error: "Ooops!",
|
|
300
|
-
},
|
|
301
|
-
);
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
it("should not store the entry in the ssrOnly cache", () => {
|
|
305
|
-
// Arrange
|
|
306
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
307
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
308
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
309
|
-
const ssrOnlyStoreSpy = jest.spyOn(ssrOnlyCache, "set");
|
|
310
|
-
|
|
311
|
-
// Act
|
|
312
|
-
cache.cacheError("MY_KEY", new Error("Ooops!"), true);
|
|
313
|
-
|
|
314
|
-
// Assert
|
|
315
|
-
expect(ssrOnlyStoreSpy).not.toHaveBeenCalled();
|
|
316
|
-
});
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
describe("when hydrate is false", () => {
|
|
320
|
-
it("should store the entry in the ssr-only cache", () => {
|
|
321
|
-
// Arrange
|
|
322
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
323
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
324
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
325
|
-
const ssrOnlyStoreSpy = jest.spyOn(ssrOnlyCache, "set");
|
|
326
|
-
|
|
327
|
-
// Act
|
|
328
|
-
cache.cacheError("MY_KEY", new Error("Ooops!"), false);
|
|
329
|
-
|
|
330
|
-
// Assert
|
|
331
|
-
expect(ssrOnlyStoreSpy).toHaveBeenCalledWith(
|
|
332
|
-
"default",
|
|
333
|
-
"MY_KEY",
|
|
334
|
-
{
|
|
335
|
-
error: "Ooops!",
|
|
336
|
-
},
|
|
337
|
-
);
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
it("should not store the entry in the hydration cache", () => {
|
|
341
|
-
// Arrange
|
|
342
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
343
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
344
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
345
|
-
const hydrationStoreSpy = jest.spyOn(hydrationCache, "set");
|
|
346
|
-
|
|
347
|
-
// Act
|
|
348
|
-
cache.cacheError("MY_KEY", new Error("Ooops!"), false);
|
|
349
|
-
|
|
350
|
-
// Assert
|
|
351
|
-
expect(hydrationStoreSpy).not.toHaveBeenCalled();
|
|
352
|
-
});
|
|
353
|
-
});
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
describe("#getEntry", () => {
|
|
358
|
-
describe("when client-side", () => {
|
|
359
|
-
beforeEach(() => {
|
|
360
|
-
jest.spyOn(Server, "isServerSide").mockReturnValue(false);
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
it("should return null if not in the hydration cache", () => {
|
|
364
|
-
// Arrange
|
|
365
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
366
|
-
jest.spyOn(hydrationCache, "get").mockReturnValue(null);
|
|
367
|
-
const cache = new SsrCache(hydrationCache);
|
|
368
|
-
|
|
369
|
-
// Act
|
|
370
|
-
const result = cache.getEntry("MY_KEY");
|
|
371
|
-
|
|
372
|
-
// Assert
|
|
373
|
-
expect(result).toBeNull();
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
it("should return the cached entry if in the hydration cache", () => {
|
|
377
|
-
// Arrange
|
|
378
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
379
|
-
jest.spyOn(hydrationCache, "get").mockReturnValue({
|
|
380
|
-
data: "data!",
|
|
381
|
-
});
|
|
382
|
-
const cache = new SsrCache(hydrationCache);
|
|
383
|
-
|
|
384
|
-
// Act
|
|
385
|
-
const result = cache.getEntry("MY_KEY");
|
|
386
|
-
|
|
387
|
-
// Assert
|
|
388
|
-
expect(result).toStrictEqual({data: "data!"});
|
|
389
|
-
});
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
describe("when server-side", () => {
|
|
393
|
-
beforeEach(() => {
|
|
394
|
-
jest.spyOn(Server, "isServerSide").mockReturnValue(true);
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
it("should return null in any cache", () => {
|
|
398
|
-
// Arrange
|
|
399
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
400
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
401
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
402
|
-
|
|
403
|
-
// Act
|
|
404
|
-
const result = cache.getEntry("MY_KEY");
|
|
405
|
-
|
|
406
|
-
// Assert
|
|
407
|
-
expect(result).toBeNull();
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
it("should return the cached entry if in the hydration cache", () => {
|
|
411
|
-
// Arrange
|
|
412
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
413
|
-
jest.spyOn(hydrationCache, "get").mockReturnValue({
|
|
414
|
-
data: "data!",
|
|
415
|
-
});
|
|
416
|
-
const cache = new SsrCache(hydrationCache);
|
|
417
|
-
|
|
418
|
-
// Act
|
|
419
|
-
const result = cache.getEntry("MY_KEY");
|
|
420
|
-
|
|
421
|
-
// Assert
|
|
422
|
-
expect(result).toStrictEqual({data: "data!"});
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
it("should return the cached entry if in the ssr-only cache", () => {
|
|
426
|
-
// Arrange
|
|
427
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
428
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
429
|
-
jest.spyOn(ssrOnlyCache, "get").mockReturnValue({
|
|
430
|
-
data: "data!",
|
|
431
|
-
});
|
|
432
|
-
const cache = new SsrCache(hydrationCache, ssrOnlyCache);
|
|
433
|
-
|
|
434
|
-
// Act
|
|
435
|
-
const result = cache.getEntry("MY_KEY");
|
|
436
|
-
|
|
437
|
-
// Assert
|
|
438
|
-
expect(result).toStrictEqual({data: "data!"});
|
|
439
|
-
});
|
|
440
|
-
});
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
describe("#cloneHydratableData", () => {
|
|
444
|
-
it("should clone the hydration cache", () => {
|
|
445
|
-
// Arrange
|
|
446
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
447
|
-
const cloneSpy = jest
|
|
448
|
-
.spyOn(hydrationCache, "clone")
|
|
449
|
-
.mockReturnValue({
|
|
450
|
-
default: "CLONE!" as any,
|
|
451
|
-
});
|
|
452
|
-
const cache = new SsrCache(hydrationCache);
|
|
453
|
-
// Let's add to the initialized state to check that everything
|
|
454
|
-
// is cloning as we expect.
|
|
455
|
-
cache.cacheData("KEY1", "DATA", true);
|
|
456
|
-
cache.cacheError("KEY2", new Error("OH NO!"), true);
|
|
457
|
-
|
|
458
|
-
// Act
|
|
459
|
-
const result = cache.cloneHydratableData();
|
|
460
|
-
|
|
461
|
-
// Assert
|
|
462
|
-
expect(cloneSpy).toHaveBeenCalled();
|
|
463
|
-
expect(result).toBe("CLONE!");
|
|
464
|
-
});
|
|
465
|
-
});
|
|
466
|
-
|
|
467
|
-
describe("#purgeData", () => {
|
|
468
|
-
describe("when client-side", () => {
|
|
469
|
-
beforeEach(() => {
|
|
470
|
-
jest.spyOn(Server, "isServerSide").mockReturnValue(false);
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
it("should remove all entries from the hydration cache when client-side without predicate", () => {
|
|
474
|
-
// Arrange
|
|
475
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
476
|
-
const purgeAllSpy = jest.spyOn(hydrationCache, "purgeAll");
|
|
477
|
-
const cache = new SsrCache(hydrationCache);
|
|
478
|
-
|
|
479
|
-
// Act
|
|
480
|
-
cache.purgeData();
|
|
481
|
-
|
|
482
|
-
// Assert
|
|
483
|
-
expect(purgeAllSpy).toHaveBeenCalledWith(undefined);
|
|
484
|
-
});
|
|
485
|
-
|
|
486
|
-
it("should pass a predicate to hydration cache purge if a predicate is passed", () => {
|
|
487
|
-
// Arrange
|
|
488
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
489
|
-
const purgeAllSpy = jest.spyOn(hydrationCache, "purgeAll");
|
|
490
|
-
const cache = new SsrCache(
|
|
491
|
-
hydrationCache,
|
|
492
|
-
new SerializableInMemoryCache(),
|
|
493
|
-
);
|
|
494
|
-
|
|
495
|
-
// Act
|
|
496
|
-
cache.purgeData(() => true);
|
|
497
|
-
|
|
498
|
-
// Assert
|
|
499
|
-
expect(purgeAllSpy).toHaveBeenCalledWith(expect.any(Function));
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
it("should pass a predicate to the hydration cache that calls the predicate it was given", () => {
|
|
503
|
-
// Arrange
|
|
504
|
-
const hydrationCache = new SerializableInMemoryCache({
|
|
505
|
-
default: {
|
|
506
|
-
KEY1: {
|
|
507
|
-
data: "DATA",
|
|
508
|
-
},
|
|
509
|
-
},
|
|
510
|
-
});
|
|
511
|
-
const cache = new SsrCache(
|
|
512
|
-
hydrationCache,
|
|
513
|
-
new SerializableInMemoryCache(),
|
|
514
|
-
);
|
|
515
|
-
const predicate = jest.fn().mockReturnValue(false);
|
|
516
|
-
|
|
517
|
-
// Act
|
|
518
|
-
cache.purgeData(predicate);
|
|
519
|
-
|
|
520
|
-
// Assert
|
|
521
|
-
expect(predicate).toHaveBeenCalledWith("KEY1", {data: "DATA"});
|
|
522
|
-
});
|
|
523
|
-
});
|
|
524
|
-
|
|
525
|
-
describe("when server-side", () => {
|
|
526
|
-
beforeEach(() => {
|
|
527
|
-
jest.spyOn(Server, "isServerSide").mockReturnValue(true);
|
|
528
|
-
});
|
|
529
|
-
|
|
530
|
-
it("should remove all entries from hydration cache when server-side without predicate", () => {
|
|
531
|
-
// Arrange
|
|
532
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
533
|
-
const hydrationPurgeAllSpy = jest.spyOn(
|
|
534
|
-
hydrationCache,
|
|
535
|
-
"purgeAll",
|
|
536
|
-
);
|
|
537
|
-
const cache = new SsrCache(
|
|
538
|
-
hydrationCache,
|
|
539
|
-
new SerializableInMemoryCache(),
|
|
540
|
-
);
|
|
541
|
-
|
|
542
|
-
// Act
|
|
543
|
-
cache.purgeData();
|
|
544
|
-
|
|
545
|
-
// Assert
|
|
546
|
-
expect(hydrationPurgeAllSpy).toHaveBeenCalledWith(undefined);
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
it("should remove all entries from ssr cache when server-side without predicate", () => {
|
|
550
|
-
// Arrange
|
|
551
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
552
|
-
const ssrPurgeAllSpy = jest.spyOn(ssrOnlyCache, "purgeAll");
|
|
553
|
-
const cache = new SsrCache(
|
|
554
|
-
new SerializableInMemoryCache(),
|
|
555
|
-
ssrOnlyCache,
|
|
556
|
-
);
|
|
557
|
-
|
|
558
|
-
// Act
|
|
559
|
-
cache.purgeData();
|
|
560
|
-
|
|
561
|
-
// Assert
|
|
562
|
-
expect(ssrPurgeAllSpy).toHaveBeenCalledWith(undefined);
|
|
563
|
-
});
|
|
564
|
-
|
|
565
|
-
it("should pass a predicate to hydration cache purge if a predicate is passed", () => {
|
|
566
|
-
// Arrange
|
|
567
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
568
|
-
const purgeAllSpy = jest.spyOn(hydrationCache, "purgeAll");
|
|
569
|
-
const cache = new SsrCache(
|
|
570
|
-
hydrationCache,
|
|
571
|
-
new SerializableInMemoryCache(),
|
|
572
|
-
);
|
|
573
|
-
|
|
574
|
-
// Act
|
|
575
|
-
cache.purgeData(() => true);
|
|
576
|
-
|
|
577
|
-
// Assert
|
|
578
|
-
expect(purgeAllSpy).toHaveBeenCalledWith(expect.any(Function));
|
|
579
|
-
});
|
|
580
|
-
|
|
581
|
-
it("should pass a predicate to srr cache purge if a predicate is passed", () => {
|
|
582
|
-
// Arrange
|
|
583
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
584
|
-
const purgeAllSpy = jest.spyOn(ssrOnlyCache, "purgeAll");
|
|
585
|
-
const cache = new SsrCache(
|
|
586
|
-
new SerializableInMemoryCache(),
|
|
587
|
-
ssrOnlyCache,
|
|
588
|
-
);
|
|
589
|
-
|
|
590
|
-
// Act
|
|
591
|
-
cache.purgeData(() => true);
|
|
592
|
-
|
|
593
|
-
// Assert
|
|
594
|
-
expect(purgeAllSpy).toHaveBeenCalledWith(expect.any(Function));
|
|
595
|
-
});
|
|
596
|
-
|
|
597
|
-
it("should pass a predicate to the hydration cache that calls the predicate it was given", () => {
|
|
598
|
-
// Arrange
|
|
599
|
-
const hydrationCache = new SerializableInMemoryCache();
|
|
600
|
-
const cache = new SsrCache(
|
|
601
|
-
hydrationCache,
|
|
602
|
-
new SerializableInMemoryCache(),
|
|
603
|
-
);
|
|
604
|
-
cache.cacheData("KEY1", "DATA", true);
|
|
605
|
-
const predicate = jest.fn().mockReturnValue(false);
|
|
606
|
-
|
|
607
|
-
// Act
|
|
608
|
-
cache.purgeData(predicate);
|
|
609
|
-
|
|
610
|
-
// Assert
|
|
611
|
-
expect(predicate).toHaveBeenCalledWith("KEY1", {data: "DATA"});
|
|
612
|
-
});
|
|
613
|
-
|
|
614
|
-
it("should pass a predicate to the ssr cache that calls the predicate it was given", () => {
|
|
615
|
-
// Arrange
|
|
616
|
-
const ssrOnlyCache = new SerializableInMemoryCache();
|
|
617
|
-
const cache = new SsrCache(
|
|
618
|
-
new SerializableInMemoryCache(),
|
|
619
|
-
ssrOnlyCache,
|
|
620
|
-
);
|
|
621
|
-
cache.cacheData(
|
|
622
|
-
"KEY1",
|
|
623
|
-
"DATA",
|
|
624
|
-
false /*false so that the data goes into the ssr only cache*/,
|
|
625
|
-
);
|
|
626
|
-
const predicate = jest.fn().mockReturnValue(false);
|
|
627
|
-
|
|
628
|
-
// Act
|
|
629
|
-
cache.purgeData(predicate);
|
|
630
|
-
|
|
631
|
-
// Assert
|
|
632
|
-
expect(predicate).toHaveBeenCalledWith("KEY1", {data: "DATA"});
|
|
633
|
-
});
|
|
634
|
-
});
|
|
635
|
-
});
|
|
636
|
-
});
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {toGqlOperation} from "../to-gql-operation";
|
|
2
|
-
import * as GDNP from "../graphql-document-node-parser";
|
|
3
|
-
|
|
4
|
-
jest.mock("../graphql-document-node-parser");
|
|
5
|
-
|
|
6
|
-
describe("#toGqlOperation", () => {
|
|
7
|
-
it("should parse the document node", () => {
|
|
8
|
-
// Arrange
|
|
9
|
-
const documentNode: any = {};
|
|
10
|
-
const parserSpy = jest
|
|
11
|
-
.spyOn(GDNP, "graphQLDocumentNodeParser")
|
|
12
|
-
.mockReturnValue({
|
|
13
|
-
name: "operationName",
|
|
14
|
-
type: "query",
|
|
15
|
-
} as any);
|
|
16
|
-
|
|
17
|
-
// Act
|
|
18
|
-
toGqlOperation(documentNode);
|
|
19
|
-
|
|
20
|
-
// Assert
|
|
21
|
-
expect(parserSpy).toHaveBeenCalledWith(documentNode);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it("should return the Wonder Blocks Data representation of the given document node", () => {
|
|
25
|
-
// Arrange
|
|
26
|
-
const documentNode: any = {};
|
|
27
|
-
jest.spyOn(GDNP, "graphQLDocumentNodeParser").mockReturnValue({
|
|
28
|
-
name: "operationName",
|
|
29
|
-
type: "mutation",
|
|
30
|
-
} as any);
|
|
31
|
-
|
|
32
|
-
// Act
|
|
33
|
-
const result = toGqlOperation(documentNode);
|
|
34
|
-
|
|
35
|
-
// Assert
|
|
36
|
-
expect(result).toStrictEqual({
|
|
37
|
-
id: "operationName",
|
|
38
|
-
type: "mutation",
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
});
|