@khanacademy/wonder-blocks-data 10.0.5 → 10.1.1
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 +38 -0
- package/dist/components/data.d.ts +52 -0
- package/dist/components/data.js.flow +63 -0
- package/dist/components/gql-router.d.ts +24 -0
- package/dist/components/gql-router.js.flow +33 -0
- package/dist/components/intercept-context.d.ts +10 -0
- package/dist/components/intercept-context.js.flow +19 -0
- package/dist/components/intercept-requests.d.ts +42 -0
- package/dist/components/intercept-requests.js.flow +51 -0
- package/dist/components/track-data.d.ts +11 -0
- package/dist/components/track-data.js.flow +18 -0
- package/dist/es/index.js +196 -214
- package/dist/hooks/use-cached-effect.d.ts +70 -0
- package/dist/hooks/use-cached-effect.js.flow +85 -0
- package/dist/hooks/use-gql-router-context.d.ts +5 -0
- package/dist/hooks/use-gql-router-context.js.flow +15 -0
- package/dist/hooks/use-gql.d.ts +12 -0
- package/dist/hooks/use-gql.js.flow +29 -0
- package/dist/hooks/use-hydratable-effect.d.ts +102 -0
- package/dist/hooks/use-hydratable-effect.js.flow +125 -0
- package/dist/hooks/use-request-interception.d.ts +14 -0
- package/dist/hooks/use-request-interception.js.flow +25 -0
- package/dist/hooks/use-server-effect.d.ts +39 -0
- package/dist/hooks/use-server-effect.js.flow +51 -0
- package/dist/hooks/use-shared-cache.d.ts +32 -0
- package/dist/hooks/use-shared-cache.js.flow +43 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +198 -219
- package/dist/index.js.flow +48 -2
- package/dist/util/data-error.d.ts +45 -0
- package/dist/util/data-error.js.flow +64 -0
- package/dist/util/get-gql-data-from-response.d.ts +4 -0
- package/dist/util/get-gql-data-from-response.js.flow +13 -0
- package/dist/util/get-gql-request-id.d.ts +5 -0
- package/dist/util/get-gql-request-id.js.flow +20 -0
- package/dist/util/gql-error.d.ts +28 -0
- package/dist/util/gql-error.js.flow +43 -0
- package/dist/util/gql-router-context.d.ts +3 -0
- package/dist/util/gql-router-context.js.flow +10 -0
- package/dist/util/gql-types.d.ts +34 -0
- package/dist/util/gql-types.js.flow +53 -0
- package/dist/util/graphql-document-node-parser.d.ts +18 -0
- package/dist/util/graphql-document-node-parser.js.flow +31 -0
- package/dist/util/graphql-types.d.ts +19 -0
- package/dist/util/graphql-types.js.flow +30 -0
- package/dist/util/hydration-cache-api.d.ts +17 -0
- package/dist/util/hydration-cache-api.js.flow +30 -0
- package/dist/util/merge-gql-context.d.ts +8 -0
- package/dist/util/merge-gql-context.js.flow +19 -0
- package/dist/util/purge-caches.d.ts +8 -0
- package/dist/util/purge-caches.js.flow +15 -0
- package/dist/util/request-api.d.ts +28 -0
- package/dist/util/request-api.js.flow +34 -0
- package/dist/util/request-fulfillment.d.ts +37 -0
- package/dist/util/request-fulfillment.js.flow +50 -0
- package/dist/util/request-tracking.d.ts +62 -0
- package/dist/util/request-tracking.js.flow +81 -0
- package/dist/util/result-from-cache-response.d.ts +5 -0
- package/dist/util/result-from-cache-response.js.flow +15 -0
- package/dist/util/scoped-in-memory-cache.d.ts +38 -0
- package/dist/util/scoped-in-memory-cache.js.flow +57 -0
- package/dist/util/serializable-in-memory-cache.d.ts +16 -0
- package/dist/util/serializable-in-memory-cache.js.flow +26 -0
- package/dist/util/ssr-cache.d.ts +51 -0
- package/dist/util/ssr-cache.js.flow +87 -0
- package/dist/util/status.d.ts +10 -0
- package/dist/util/status.js.flow +19 -0
- package/dist/util/to-gql-operation.d.ts +32 -0
- package/dist/util/to-gql-operation.js.flow +45 -0
- package/dist/util/types.d.ts +111 -0
- package/dist/util/types.js.flow +151 -0
- package/package.json +5 -6
- package/src/components/__tests__/{data.test.js → data.test.tsx} +50 -16
- package/src/components/__tests__/{gql-router.test.js → gql-router.test.tsx} +6 -7
- package/src/components/__tests__/{intercept-requests.test.js → intercept-requests.test.tsx} +4 -5
- package/src/components/__tests__/{track-data.test.js → track-data.test.tsx} +4 -5
- package/src/components/{data.js → data.ts} +13 -21
- package/src/components/{gql-router.js → gql-router.tsx} +14 -16
- package/src/components/{intercept-context.js → intercept-context.ts} +5 -4
- package/src/components/{intercept-requests.js → intercept-requests.tsx} +9 -10
- package/src/components/{track-data.js → track-data.tsx} +5 -6
- package/src/hooks/__tests__/{use-cached-effect.test.js → use-cached-effect.test.tsx} +65 -63
- package/src/hooks/__tests__/{use-gql-router-context.test.js → use-gql-router-context.test.tsx} +9 -9
- package/src/hooks/__tests__/{use-gql.test.js → use-gql.test.tsx} +23 -24
- package/src/hooks/__tests__/{use-hydratable-effect.test.js → use-hydratable-effect.test.ts} +52 -54
- package/src/hooks/__tests__/{use-request-interception.test.js → use-request-interception.test.tsx} +7 -5
- package/src/hooks/__tests__/{use-server-effect.test.js → use-server-effect.test.ts} +16 -10
- package/src/hooks/__tests__/{use-shared-cache.test.js → use-shared-cache.test.ts} +13 -13
- package/src/hooks/{use-cached-effect.js → use-cached-effect.ts} +34 -31
- package/src/hooks/{use-gql-router-context.js → use-gql-router-context.ts} +6 -7
- package/src/hooks/{use-gql.js → use-gql.ts} +9 -9
- package/src/hooks/{use-hydratable-effect.js → use-hydratable-effect.ts} +60 -67
- package/src/hooks/{use-request-interception.js → use-request-interception.ts} +6 -6
- package/src/hooks/{use-server-effect.js → use-server-effect.ts} +12 -14
- package/src/hooks/{use-shared-cache.js → use-shared-cache.ts} +16 -11
- package/src/index.ts +46 -0
- package/src/util/__tests__/{get-gql-data-from-response.test.js → get-gql-data-from-response.test.ts} +1 -2
- package/src/util/__tests__/{get-gql-request-id.test.js → get-gql-request-id.test.ts} +10 -12
- package/src/util/__tests__/{graphql-document-node-parser.test.js → graphql-document-node-parser.test.ts} +12 -13
- package/src/util/__tests__/{hydration-cache-api.test.js → hydration-cache-api.test.ts} +3 -4
- package/src/util/__tests__/{merge-gql-context.test.js → merge-gql-context.test.ts} +5 -6
- package/src/util/__tests__/{purge-caches.test.js → purge-caches.test.ts} +3 -4
- package/src/util/__tests__/{request-api.test.js → request-api.test.ts} +5 -5
- package/src/util/__tests__/{request-fulfillment.test.js → request-fulfillment.test.ts} +2 -3
- package/src/util/__tests__/{request-tracking.test.js → request-tracking.test.tsx} +15 -8
- package/src/util/__tests__/{result-from-cache-response.test.js → result-from-cache-response.test.ts} +3 -5
- package/src/util/__tests__/{scoped-in-memory-cache.test.js → scoped-in-memory-cache.test.ts} +5 -6
- package/src/util/__tests__/{serializable-in-memory-cache.test.js → serializable-in-memory-cache.test.ts} +8 -8
- package/src/util/__tests__/{ssr-cache.test.js → ssr-cache.test.ts} +5 -4
- package/src/util/__tests__/{to-gql-operation.test.js → to-gql-operation.test.ts} +5 -4
- package/src/util/{data-error.js → data-error.ts} +3 -4
- package/src/util/{get-gql-data-from-response.js → get-gql-data-from-response.ts} +3 -8
- package/src/util/{get-gql-request-id.js → get-gql-request-id.ts} +13 -17
- package/src/util/{gql-error.js → gql-error.ts} +3 -4
- package/src/util/gql-router-context.ts +6 -0
- package/src/util/{gql-types.js → gql-types.ts} +27 -23
- package/src/util/{graphql-document-node-parser.js → graphql-document-node-parser.ts} +8 -9
- package/src/util/graphql-types.ts +27 -0
- package/src/util/{hydration-cache-api.js → hydration-cache-api.ts} +6 -4
- package/src/util/{merge-gql-context.js → merge-gql-context.ts} +3 -3
- package/src/util/{purge-caches.js → purge-caches.ts} +2 -3
- package/src/util/{request-api.js → request-api.ts} +4 -5
- package/src/util/{request-fulfillment.js → request-fulfillment.ts} +15 -14
- package/src/util/{request-tracking.js → request-tracking.ts} +15 -16
- package/src/util/{result-from-cache-response.js → result-from-cache-response.ts} +6 -7
- package/src/util/{scoped-in-memory-cache.js → scoped-in-memory-cache.ts} +3 -4
- package/src/util/{serializable-in-memory-cache.js → serializable-in-memory-cache.ts} +5 -6
- package/src/util/{ssr-cache.js → ssr-cache.ts} +21 -20
- package/src/util/{status.js → status.ts} +5 -6
- package/src/util/{to-gql-operation.js → to-gql-operation.ts} +4 -5
- package/src/util/{types.js → types.ts} +41 -49
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/src/__docs__/_overview_.stories.mdx +0 -18
- package/src/__docs__/_overview_graphql.stories.mdx +0 -35
- package/src/__docs__/_overview_ssr_.stories.mdx +0 -185
- package/src/__docs__/_overview_testing_.stories.mdx +0 -123
- package/src/__docs__/exports.abort-inflight-requests.stories.mdx +0 -20
- package/src/__docs__/exports.data-error.stories.mdx +0 -23
- package/src/__docs__/exports.data-errors.stories.mdx +0 -23
- package/src/__docs__/exports.data.stories.mdx +0 -146
- package/src/__docs__/exports.fetch-tracked-requests.stories.mdx +0 -24
- package/src/__docs__/exports.get-gql-request-id.stories.mdx +0 -24
- package/src/__docs__/exports.gql-error.stories.mdx +0 -23
- package/src/__docs__/exports.gql-errors.stories.mdx +0 -20
- package/src/__docs__/exports.gql-router.stories.mdx +0 -29
- package/src/__docs__/exports.has-tracked-requests-to-be-fetched.stories.mdx +0 -20
- package/src/__docs__/exports.intercept-requests.stories.mdx +0 -69
- package/src/__docs__/exports.intialize-hydration-cache.stories.mdx +0 -29
- package/src/__docs__/exports.purge-caches.stories.mdx +0 -23
- package/src/__docs__/exports.purge-hydration-cache.stories.mdx +0 -24
- package/src/__docs__/exports.scoped-in-memory-cache.stories.mdx +0 -92
- package/src/__docs__/exports.serializable-in-memory-cache.stories.mdx +0 -112
- package/src/__docs__/exports.shared-cache.stories.mdx +0 -16
- package/src/__docs__/exports.status.stories.mdx +0 -31
- package/src/__docs__/exports.track-data.stories.mdx +0 -209
- package/src/__docs__/exports.use-cached-effect.stories.mdx +0 -44
- package/src/__docs__/exports.use-gql.stories.mdx +0 -41
- package/src/__docs__/exports.use-hydratable-effect.stories.mdx +0 -43
- package/src/__docs__/exports.use-server-effect.stories.mdx +0 -50
- package/src/__docs__/exports.use-shared-cache.stories.mdx +0 -30
- package/src/__docs__/exports.when-client-side.stories.mdx +0 -33
- package/src/__docs__/types.cached-response.stories.mdx +0 -29
- package/src/__docs__/types.error-options.stories.mdx +0 -21
- package/src/__docs__/types.fetch-policy.stories.mdx +0 -44
- package/src/__docs__/types.gql-context.stories.mdx +0 -20
- package/src/__docs__/types.gql-fetch-fn.stories.mdx +0 -24
- package/src/__docs__/types.gql-fetch-options.stories.mdx +0 -24
- package/src/__docs__/types.gql-operation-type.stories.mdx +0 -24
- package/src/__docs__/types.gql-operation.stories.mdx +0 -67
- package/src/__docs__/types.raw-scoped-cache.stories.mdx +0 -27
- package/src/__docs__/types.response-cache.stories.mdx +0 -33
- package/src/__docs__/types.result.stories.mdx +0 -39
- package/src/__docs__/types.scoped-cache.stories.mdx +0 -114
- package/src/__docs__/types.valid-cache-data.stories.mdx +0 -23
- package/src/index.js +0 -55
- package/src/util/gql-router-context.js +0 -6
- package/src/util/graphql-types.js +0 -30
- /package/src/hooks/__tests__/__snapshots__/{use-shared-cache.test.js.snap → use-shared-cache.test.ts.snap} +0 -0
- /package/src/util/__tests__/__snapshots__/{scoped-in-memory-cache.test.js.snap → scoped-in-memory-cache.test.ts.snap} +0 -0
- /package/src/util/__tests__/__snapshots__/{serializable-in-memory-cache.test.js.snap → serializable-in-memory-cache.test.ts.snap} +0 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {
|
|
4
3
|
renderHook as clientRenderHook,
|
|
@@ -7,25 +6,18 @@ import {
|
|
|
7
6
|
import {renderHook as serverRenderHook} from "@testing-library/react-hooks/server";
|
|
8
7
|
|
|
9
8
|
import {Server} from "@khanacademy/wonder-blocks-core";
|
|
10
|
-
import {Status} from "../../util/status
|
|
9
|
+
import {Status} from "../../util/status";
|
|
11
10
|
|
|
12
|
-
import {RequestFulfillment} from "../../util/request-fulfillment
|
|
13
|
-
import * as UseRequestInterception from "../use-request-interception
|
|
14
|
-
import * as UseServerEffect from "../use-server-effect
|
|
15
|
-
import * as UseSharedCache from "../use-shared-cache
|
|
11
|
+
import {RequestFulfillment} from "../../util/request-fulfillment";
|
|
12
|
+
import * as UseRequestInterception from "../use-request-interception";
|
|
13
|
+
import * as UseServerEffect from "../use-server-effect";
|
|
14
|
+
import * as UseSharedCache from "../use-shared-cache";
|
|
16
15
|
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// eslint-disable-next-line import/named
|
|
23
|
-
WhenClientSide,
|
|
24
|
-
} from "../use-hydratable-effect.js";
|
|
25
|
-
|
|
26
|
-
jest.mock("../use-request-interception.js");
|
|
27
|
-
jest.mock("../use-server-effect.js");
|
|
28
|
-
jest.mock("../use-shared-cache.js");
|
|
16
|
+
import {useHydratableEffect, WhenClientSide} from "../use-hydratable-effect";
|
|
17
|
+
|
|
18
|
+
jest.mock("../use-request-interception");
|
|
19
|
+
jest.mock("../use-server-effect");
|
|
20
|
+
jest.mock("../use-shared-cache");
|
|
29
21
|
|
|
30
22
|
describe("#useHydratableEffect", () => {
|
|
31
23
|
beforeEach(() => {
|
|
@@ -39,14 +31,14 @@ describe("#useHydratableEffect", () => {
|
|
|
39
31
|
jest.spyOn(
|
|
40
32
|
UseRequestInterception,
|
|
41
33
|
"useRequestInterception",
|
|
42
|
-
).mockImplementation((_, handler) => handler);
|
|
34
|
+
).mockImplementation((_: any, handler: any) => handler);
|
|
43
35
|
|
|
44
36
|
// We need the cache to work a little so that we get our result.
|
|
45
|
-
const cache = {};
|
|
37
|
+
const cache: Record<string, any> = {};
|
|
46
38
|
jest.spyOn(UseSharedCache, "useSharedCache").mockImplementation(
|
|
47
|
-
(id, _, defaultValue) => {
|
|
39
|
+
(id: any, _: any, defaultValue: any) => {
|
|
48
40
|
const setCache = React.useCallback(
|
|
49
|
-
(v) => (cache[id] = v),
|
|
41
|
+
(v: any) => (cache[id] = v),
|
|
50
42
|
[id],
|
|
51
43
|
);
|
|
52
44
|
const currentValue =
|
|
@@ -91,7 +83,7 @@ describe("#useHydratableEffect", () => {
|
|
|
91
83
|
${undefined /*default*/} | ${true}
|
|
92
84
|
`(
|
|
93
85
|
"should call useServerEffect with the handler and hydrate=$hydrate for $clientBehavior",
|
|
94
|
-
({hydrate, clientBehavior}) => {
|
|
86
|
+
({hydrate, clientBehavior}: any) => {
|
|
95
87
|
// Arrange
|
|
96
88
|
const useServerEffectSpy = jest
|
|
97
89
|
.spyOn(UseServerEffect, "useServerEffect")
|
|
@@ -120,7 +112,7 @@ describe("#useHydratableEffect", () => {
|
|
|
120
112
|
${"foo"} | ${"foo"}
|
|
121
113
|
`(
|
|
122
114
|
"should call useSharedCache with id, scope=$scope, and a function to set the default",
|
|
123
|
-
({scope, expectedScope}) => {
|
|
115
|
+
({scope, expectedScope}: any) => {
|
|
124
116
|
const fakeHandler = jest.fn();
|
|
125
117
|
jest.spyOn(UseServerEffect, "useServerEffect").mockReturnValue(
|
|
126
118
|
Status.success({thisIs: "some data"}),
|
|
@@ -208,7 +200,7 @@ describe("#useHydratableEffect", () => {
|
|
|
208
200
|
${undefined /*default*/} | ${true}
|
|
209
201
|
`(
|
|
210
202
|
"should call useServerEffect with the handler and hydrate=$hydrate for $clientBehavior",
|
|
211
|
-
({hydrate, clientBehavior}) => {
|
|
203
|
+
({hydrate, clientBehavior}: any) => {
|
|
212
204
|
// Arrange
|
|
213
205
|
const useServerEffectSpy = jest
|
|
214
206
|
.spyOn(UseServerEffect, "useServerEffect")
|
|
@@ -247,7 +239,7 @@ describe("#useHydratableEffect", () => {
|
|
|
247
239
|
|
|
248
240
|
it("should share inflight requests for the same requestId", () => {
|
|
249
241
|
// Arrange
|
|
250
|
-
const pending = new Promise((resolve, reject) => {
|
|
242
|
+
const pending = new Promise((resolve: any, reject: any) => {
|
|
251
243
|
/*pending*/
|
|
252
244
|
});
|
|
253
245
|
const fakeHandler = jest.fn().mockReturnValue(pending);
|
|
@@ -266,7 +258,7 @@ describe("#useHydratableEffect", () => {
|
|
|
266
258
|
${Status.error(new Error("some error"))}
|
|
267
259
|
`(
|
|
268
260
|
"should fulfill request when server value is $serverResult and clientBehavior is ExecuteWhenNoSuccessResult",
|
|
269
|
-
({serverResult}) => {
|
|
261
|
+
({serverResult}: any) => {
|
|
270
262
|
// Arrange
|
|
271
263
|
const fakeHandler = jest.fn();
|
|
272
264
|
jest.spyOn(UseServerEffect, "useServerEffect").mockReturnValue(
|
|
@@ -293,7 +285,7 @@ describe("#useHydratableEffect", () => {
|
|
|
293
285
|
${Status.success("data")}
|
|
294
286
|
`(
|
|
295
287
|
"should fulfill request when server value is $serveResult and clientBehavior is AlwaysExecute",
|
|
296
|
-
({serverResult}) => {
|
|
288
|
+
({serverResult}: any) => {
|
|
297
289
|
// Arrange
|
|
298
290
|
const fakeHandler = jest.fn();
|
|
299
291
|
jest.spyOn(UseServerEffect, "useServerEffect").mockReturnValue(
|
|
@@ -336,7 +328,7 @@ describe("#useHydratableEffect", () => {
|
|
|
336
328
|
${Status.success("data")}
|
|
337
329
|
`(
|
|
338
330
|
"should not fulfill request when server value is $serverResult and clientBehavior is ExecuteWhenNoResult",
|
|
339
|
-
({serverResult}) => {
|
|
331
|
+
({serverResult}: any) => {
|
|
340
332
|
const fakeHandler = jest.fn();
|
|
341
333
|
jest.spyOn(UseServerEffect, "useServerEffect").mockReturnValue(
|
|
342
334
|
serverResult,
|
|
@@ -380,7 +372,8 @@ describe("#useHydratableEffect", () => {
|
|
|
380
372
|
|
|
381
373
|
// Act
|
|
382
374
|
const {rerender, waitForNextUpdate} = clientRenderHook(
|
|
383
|
-
({requestId}) =>
|
|
375
|
+
({requestId}: any) =>
|
|
376
|
+
useHydratableEffect(requestId, fakeHandler),
|
|
384
377
|
{
|
|
385
378
|
initialProps: {requestId: "ID"},
|
|
386
379
|
},
|
|
@@ -405,7 +398,8 @@ describe("#useHydratableEffect", () => {
|
|
|
405
398
|
|
|
406
399
|
// Act
|
|
407
400
|
const {rerender, result} = clientRenderHook(
|
|
408
|
-
({requestId}) =>
|
|
401
|
+
({requestId}: any) =>
|
|
402
|
+
useHydratableEffect(requestId, fakeHandler),
|
|
409
403
|
{
|
|
410
404
|
initialProps: {requestId: "ID"},
|
|
411
405
|
},
|
|
@@ -449,15 +443,14 @@ describe("#useHydratableEffect", () => {
|
|
|
449
443
|
|
|
450
444
|
// Act
|
|
451
445
|
const {rerender, result} = clientRenderHook(
|
|
452
|
-
({requestId}) =>
|
|
446
|
+
({requestId}: any) =>
|
|
447
|
+
useHydratableEffect(requestId, fakeHandler),
|
|
453
448
|
{
|
|
454
449
|
initialProps: {requestId: "ID"},
|
|
455
450
|
},
|
|
456
451
|
);
|
|
457
452
|
rerender({requestId: "ID2"});
|
|
458
|
-
await act((): Promise<
|
|
459
|
-
Promise.all([response1, response2]),
|
|
460
|
-
);
|
|
453
|
+
await act((): Promise<any> => Promise.all([response1, response2]));
|
|
461
454
|
|
|
462
455
|
// Assert
|
|
463
456
|
expect(result.all).not.toContainEqual(Status.success("DATA1"));
|
|
@@ -477,15 +470,14 @@ describe("#useHydratableEffect", () => {
|
|
|
477
470
|
|
|
478
471
|
// Act
|
|
479
472
|
const {rerender, result} = clientRenderHook(
|
|
480
|
-
({requestId}) =>
|
|
473
|
+
({requestId}: any) =>
|
|
474
|
+
useHydratableEffect(requestId, fakeHandler),
|
|
481
475
|
{
|
|
482
476
|
initialProps: {requestId: "ID"},
|
|
483
477
|
},
|
|
484
478
|
);
|
|
485
479
|
rerender({requestId: "ID2"});
|
|
486
|
-
await act((): Promise<
|
|
487
|
-
Promise.all([response1, response2]),
|
|
488
|
-
);
|
|
480
|
+
await act((): Promise<any> => Promise.all([response1, response2]));
|
|
489
481
|
|
|
490
482
|
// Assert
|
|
491
483
|
expect(result.current).toStrictEqual(Status.success("DATA2"));
|
|
@@ -517,13 +509,14 @@ describe("#useHydratableEffect", () => {
|
|
|
517
509
|
|
|
518
510
|
// Act
|
|
519
511
|
const {rerender, result} = clientRenderHook(
|
|
520
|
-
({skip}) => useHydratableEffect("ID", fakeHandler, {skip}),
|
|
512
|
+
({skip}: any) => useHydratableEffect("ID", fakeHandler, {skip}),
|
|
521
513
|
{
|
|
522
514
|
initialProps: {skip: false},
|
|
523
515
|
},
|
|
524
516
|
);
|
|
525
517
|
rerender({skip: true});
|
|
526
|
-
|
|
518
|
+
|
|
519
|
+
await act((): Promise<any> => response1);
|
|
527
520
|
|
|
528
521
|
// Assert
|
|
529
522
|
expect(result.all).not.toContainEqual(Status.success("DATA1"));
|
|
@@ -541,15 +534,13 @@ describe("#useHydratableEffect", () => {
|
|
|
541
534
|
|
|
542
535
|
// Act
|
|
543
536
|
const {rerender, result} = clientRenderHook(
|
|
544
|
-
({handler}) => useHydratableEffect("ID", handler),
|
|
537
|
+
({handler}: any) => useHydratableEffect("ID", handler),
|
|
545
538
|
{
|
|
546
539
|
initialProps: {handler: fakeHandler1},
|
|
547
540
|
},
|
|
548
541
|
);
|
|
549
542
|
rerender({handler: fakeHandler2});
|
|
550
|
-
await act((): Promise<
|
|
551
|
-
Promise.all([response1, response2]),
|
|
552
|
-
);
|
|
543
|
+
await act((): Promise<any> => Promise.all([response1, response2]));
|
|
553
544
|
|
|
554
545
|
// Assert
|
|
555
546
|
expect(result.current).toStrictEqual(Status.success("DATA1"));
|
|
@@ -565,17 +556,19 @@ describe("#useHydratableEffect", () => {
|
|
|
565
556
|
|
|
566
557
|
// Act
|
|
567
558
|
const {rerender, result} = clientRenderHook(
|
|
568
|
-
({options}) => useHydratableEffect("ID", fakeHandler),
|
|
559
|
+
({options}: any) => useHydratableEffect("ID", fakeHandler),
|
|
569
560
|
{
|
|
570
561
|
initialProps: {options: undefined},
|
|
571
562
|
},
|
|
572
563
|
);
|
|
573
564
|
rerender({
|
|
565
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type '{ scope: string; }' is not assignable to type 'undefined'.
|
|
574
566
|
options: {
|
|
575
567
|
scope: "BLAH!",
|
|
576
568
|
},
|
|
577
569
|
});
|
|
578
|
-
|
|
570
|
+
|
|
571
|
+
await act((): Promise<any> => response1);
|
|
579
572
|
|
|
580
573
|
// Assert
|
|
581
574
|
expect(result.current).toStrictEqual(Status.success("DATA1"));
|
|
@@ -599,7 +592,7 @@ describe("#useHydratableEffect", () => {
|
|
|
599
592
|
result: hookResult,
|
|
600
593
|
waitForNextUpdate,
|
|
601
594
|
} = clientRenderHook(
|
|
602
|
-
({requestId}) =>
|
|
595
|
+
({requestId}: any) =>
|
|
603
596
|
useHydratableEffect(requestId, fakeHandler, {
|
|
604
597
|
retainResultOnChange: true,
|
|
605
598
|
}),
|
|
@@ -607,7 +600,8 @@ describe("#useHydratableEffect", () => {
|
|
|
607
600
|
initialProps: {requestId: "ID"},
|
|
608
601
|
},
|
|
609
602
|
);
|
|
610
|
-
|
|
603
|
+
|
|
604
|
+
await act((): Promise<any> => response1);
|
|
611
605
|
rerender({requestId: "ID2"});
|
|
612
606
|
const result = hookResult.current;
|
|
613
607
|
await waitForNextUpdate();
|
|
@@ -632,7 +626,7 @@ describe("#useHydratableEffect", () => {
|
|
|
632
626
|
|
|
633
627
|
// Act
|
|
634
628
|
const {rerender, result} = clientRenderHook(
|
|
635
|
-
({requestId}) =>
|
|
629
|
+
({requestId}: any) =>
|
|
636
630
|
useHydratableEffect(requestId, fakeHandler, {
|
|
637
631
|
retainResultOnChange: false,
|
|
638
632
|
}),
|
|
@@ -640,7 +634,8 @@ describe("#useHydratableEffect", () => {
|
|
|
640
634
|
initialProps: {requestId: "ID"},
|
|
641
635
|
},
|
|
642
636
|
);
|
|
643
|
-
|
|
637
|
+
|
|
638
|
+
await act((): Promise<any> => response1);
|
|
644
639
|
rerender({requestId: "ID2"});
|
|
645
640
|
|
|
646
641
|
// Assert
|
|
@@ -659,7 +654,8 @@ describe("#useHydratableEffect", () => {
|
|
|
659
654
|
const {result} = clientRenderHook(() =>
|
|
660
655
|
useHydratableEffect("ID", fakeHandler),
|
|
661
656
|
);
|
|
662
|
-
|
|
657
|
+
|
|
658
|
+
await act((): Promise<any> => response);
|
|
663
659
|
|
|
664
660
|
// Assert
|
|
665
661
|
expect(result.current).toStrictEqual(Status.success("DATA"));
|
|
@@ -679,7 +675,8 @@ describe("#useHydratableEffect", () => {
|
|
|
679
675
|
onResultChanged: () => {},
|
|
680
676
|
}),
|
|
681
677
|
);
|
|
682
|
-
|
|
678
|
+
|
|
679
|
+
await act((): Promise<any> => response);
|
|
683
680
|
|
|
684
681
|
// Assert
|
|
685
682
|
expect(result.current).toStrictEqual(Status.loading());
|
|
@@ -700,7 +697,8 @@ describe("#useHydratableEffect", () => {
|
|
|
700
697
|
onResultChanged,
|
|
701
698
|
}),
|
|
702
699
|
);
|
|
703
|
-
|
|
700
|
+
|
|
701
|
+
await act((): Promise<any> => response);
|
|
704
702
|
|
|
705
703
|
// Assert
|
|
706
704
|
expect(onResultChanged).toHaveBeenCalledWith(
|
package/src/hooks/__tests__/{use-request-interception.test.js → use-request-interception.test.tsx}
RENAMED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {renderHook} from "@testing-library/react-hooks";
|
|
4
|
-
import InterceptRequests from "../../components/intercept-requests
|
|
5
|
-
import {useRequestInterception} from "../use-request-interception
|
|
3
|
+
import InterceptRequests from "../../components/intercept-requests";
|
|
4
|
+
import {useRequestInterception} from "../use-request-interception";
|
|
6
5
|
|
|
7
6
|
describe("#useRequestInterception", () => {
|
|
8
7
|
it("should return a function", () => {
|
|
@@ -37,7 +36,7 @@ describe("#useRequestInterception", () => {
|
|
|
37
36
|
|
|
38
37
|
// Act
|
|
39
38
|
const wrapper = renderHook(
|
|
40
|
-
({requestId}) => useRequestInterception(requestId, handler),
|
|
39
|
+
({requestId}: any) => useRequestInterception(requestId, handler),
|
|
41
40
|
{initialProps: {requestId: "ID"}},
|
|
42
41
|
);
|
|
43
42
|
const result1 = wrapper.result.current;
|
|
@@ -53,7 +52,7 @@ describe("#useRequestInterception", () => {
|
|
|
53
52
|
|
|
54
53
|
// Act
|
|
55
54
|
const wrapper = renderHook(
|
|
56
|
-
({handler}) => useRequestInterception("ID", handler),
|
|
55
|
+
({handler}: any) => useRequestInterception("ID", handler),
|
|
57
56
|
{initialProps: {handler: jest.fn()}},
|
|
58
57
|
);
|
|
59
58
|
const result1 = wrapper.result.current;
|
|
@@ -81,6 +80,7 @@ describe("#useRequestInterception", () => {
|
|
|
81
80
|
{wrapper: Wrapper, initialProps: {interceptor: interceptor1}},
|
|
82
81
|
);
|
|
83
82
|
const result1 = wrapper.result.current;
|
|
83
|
+
// @ts-expect-error [FEI-5019] - TS2345 - Argument of type '{ wrapper: ({ children, interceptor, }: any) => JSX.Element; interceptor: jest.Mock<any, any, any>; }' is not assignable to parameter of type '{ interceptor: jest.Mock<any, any, any>; }'.
|
|
84
84
|
wrapper.rerender({wrapper: Wrapper, interceptor: interceptor2});
|
|
85
85
|
const result2 = wrapper.result.current;
|
|
86
86
|
|
|
@@ -126,6 +126,7 @@ describe("#useRequestInterception", () => {
|
|
|
126
126
|
interceptedHandler();
|
|
127
127
|
|
|
128
128
|
// Assert
|
|
129
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'toHaveBeenCalledBefore' does not exist on type 'JestMatchers<Mock<null, [], any>>'.
|
|
129
130
|
expect(interceptorNearest).toHaveBeenCalledBefore(
|
|
130
131
|
interceptorFurthest,
|
|
131
132
|
);
|
|
@@ -153,6 +154,7 @@ describe("#useRequestInterception", () => {
|
|
|
153
154
|
interceptedHandler();
|
|
154
155
|
|
|
155
156
|
// Assert
|
|
157
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'toHaveBeenCalledBefore' does not exist on type 'JestMatchers<Mock<null, [], any>>'.
|
|
156
158
|
expect(interceptorFurthest).toHaveBeenCalledBefore(handler);
|
|
157
159
|
});
|
|
158
160
|
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {renderHook as clientRenderHook} from "@testing-library/react-hooks";
|
|
3
2
|
import {renderHook as serverRenderHook} from "@testing-library/react-hooks/server";
|
|
4
3
|
|
|
5
4
|
import {Server} from "@khanacademy/wonder-blocks-core";
|
|
6
5
|
|
|
7
|
-
import TrackData from "../../components/track-data
|
|
8
|
-
import {RequestFulfillment} from "../../util/request-fulfillment
|
|
9
|
-
import {SsrCache} from "../../util/ssr-cache
|
|
10
|
-
import {RequestTracker} from "../../util/request-tracking
|
|
11
|
-
import {DataError} from "../../util/data-error
|
|
12
|
-
import * as UseRequestInterception from "../use-request-interception
|
|
6
|
+
import TrackData from "../../components/track-data";
|
|
7
|
+
import {RequestFulfillment} from "../../util/request-fulfillment";
|
|
8
|
+
import {SsrCache} from "../../util/ssr-cache";
|
|
9
|
+
import {RequestTracker} from "../../util/request-tracking";
|
|
10
|
+
import {DataError} from "../../util/data-error";
|
|
11
|
+
import * as UseRequestInterception from "../use-request-interception";
|
|
13
12
|
|
|
14
|
-
import {useServerEffect} from "../use-server-effect
|
|
13
|
+
import {useServerEffect} from "../use-server-effect";
|
|
15
14
|
|
|
16
|
-
jest.mock("../use-request-interception
|
|
15
|
+
jest.mock("../use-request-interception");
|
|
17
16
|
|
|
18
17
|
describe("#useServerEffect", () => {
|
|
19
18
|
beforeEach(() => {
|
|
@@ -33,7 +32,7 @@ describe("#useServerEffect", () => {
|
|
|
33
32
|
jest.spyOn(
|
|
34
33
|
UseRequestInterception,
|
|
35
34
|
"useRequestInterception",
|
|
36
|
-
).mockImplementation((_, handler) => handler);
|
|
35
|
+
).mockImplementation((_: any, handler: any) => handler);
|
|
37
36
|
});
|
|
38
37
|
|
|
39
38
|
it("should call useRequestInterception", () => {
|
|
@@ -140,6 +139,7 @@ describe("#useServerEffect", () => {
|
|
|
140
139
|
const interceptedHandler = jest.fn();
|
|
141
140
|
jest.spyOn(SsrCache.Default, "getEntry").mockReturnValueOnce({
|
|
142
141
|
data: "DATA",
|
|
142
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'null' is not assignable to type 'undefined'.
|
|
143
143
|
error: null,
|
|
144
144
|
});
|
|
145
145
|
jest.spyOn(
|
|
@@ -165,6 +165,7 @@ describe("#useServerEffect", () => {
|
|
|
165
165
|
const fakeHandler = jest.fn();
|
|
166
166
|
jest.spyOn(SsrCache.Default, "getEntry").mockReturnValueOnce({
|
|
167
167
|
data: "DATA",
|
|
168
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'null' is not assignable to type 'undefined'.
|
|
168
169
|
error: null,
|
|
169
170
|
});
|
|
170
171
|
|
|
@@ -181,7 +182,9 @@ describe("#useServerEffect", () => {
|
|
|
181
182
|
// Arrange
|
|
182
183
|
const fakeHandler = jest.fn();
|
|
183
184
|
jest.spyOn(SsrCache.Default, "getEntry").mockReturnValueOnce({
|
|
185
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'null' is not assignable to type 'ValidCacheData'.
|
|
184
186
|
data: null,
|
|
187
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'string' is not assignable to type 'undefined'.
|
|
185
188
|
error: "ERROR",
|
|
186
189
|
});
|
|
187
190
|
|
|
@@ -221,6 +224,7 @@ describe("#useServerEffect", () => {
|
|
|
221
224
|
const fakeHandler = jest.fn();
|
|
222
225
|
jest.spyOn(SsrCache.Default, "getEntry").mockReturnValueOnce({
|
|
223
226
|
data: "DATA",
|
|
227
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'null' is not assignable to type 'undefined'.
|
|
224
228
|
error: null,
|
|
225
229
|
});
|
|
226
230
|
|
|
@@ -237,7 +241,9 @@ describe("#useServerEffect", () => {
|
|
|
237
241
|
// Arrange
|
|
238
242
|
const fakeHandler = jest.fn();
|
|
239
243
|
jest.spyOn(SsrCache.Default, "getEntry").mockReturnValueOnce({
|
|
244
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'null' is not assignable to type 'ValidCacheData'.
|
|
240
245
|
data: null,
|
|
246
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'string' is not assignable to type 'undefined'.
|
|
241
247
|
error: "ERROR",
|
|
242
248
|
});
|
|
243
249
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import {renderHook as clientRenderHook} from "@testing-library/react-hooks";
|
|
3
2
|
|
|
4
|
-
import {useSharedCache, SharedCache} from "../use-shared-cache
|
|
3
|
+
import {useSharedCache, SharedCache} from "../use-shared-cache";
|
|
5
4
|
|
|
6
5
|
describe("#useSharedCache", () => {
|
|
7
6
|
beforeEach(() => {
|
|
@@ -14,7 +13,7 @@ describe("#useSharedCache", () => {
|
|
|
14
13
|
${""}
|
|
15
14
|
${5}
|
|
16
15
|
${() => "BOO"}
|
|
17
|
-
`("should throw if the id is $id", ({id}) => {
|
|
16
|
+
`("should throw if the id is $id", ({id}: any) => {
|
|
18
17
|
// Arrange
|
|
19
18
|
|
|
20
19
|
// Act
|
|
@@ -30,7 +29,7 @@ describe("#useSharedCache", () => {
|
|
|
30
29
|
${""}
|
|
31
30
|
${5}
|
|
32
31
|
${() => "BOO"}
|
|
33
|
-
`("should throw if the scope is $scope", ({scope}) => {
|
|
32
|
+
`("should throw if the scope is $scope", ({scope}: any) => {
|
|
34
33
|
// Arrange
|
|
35
34
|
|
|
36
35
|
// Act
|
|
@@ -49,6 +48,7 @@ describe("#useSharedCache", () => {
|
|
|
49
48
|
} = clientRenderHook(() => useSharedCache("id", "scope"));
|
|
50
49
|
|
|
51
50
|
// Assert
|
|
51
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'toBeArrayOfSize' does not exist on type 'JestMatchers<[ValidCacheData | null | undefined, CacheValueFn<ValidCacheData>]>'.
|
|
52
52
|
expect(result).toBeArrayOfSize(2);
|
|
53
53
|
});
|
|
54
54
|
|
|
@@ -110,7 +110,7 @@ describe("#useSharedCache", () => {
|
|
|
110
110
|
it("should be the same function if the id and scope remain the same", () => {
|
|
111
111
|
// Arrange
|
|
112
112
|
const wrapper = clientRenderHook(
|
|
113
|
-
({id, scope}) => useSharedCache(id, scope),
|
|
113
|
+
({id, scope}: any) => useSharedCache(id, scope),
|
|
114
114
|
{initialProps: {id: "id", scope: "scope"}},
|
|
115
115
|
);
|
|
116
116
|
|
|
@@ -123,14 +123,14 @@ describe("#useSharedCache", () => {
|
|
|
123
123
|
const result2 = wrapper.result.current;
|
|
124
124
|
|
|
125
125
|
// Assert
|
|
126
|
-
//
|
|
126
|
+
// @ts-expect-error [FEI-5019] - TS7053 - Element implicitly has an 'any' type because expression of type '1' can't be used to index type 'Error | [ValidCacheData | null | undefined, CacheValueFn<ValidCacheData>]'.
|
|
127
127
|
expect(result1[1]).toBe(result2[1]);
|
|
128
128
|
});
|
|
129
129
|
|
|
130
130
|
it("should be a new function if the id changes", () => {
|
|
131
131
|
// Arrange
|
|
132
132
|
const wrapper = clientRenderHook(
|
|
133
|
-
({id}) => useSharedCache(id, "scope"),
|
|
133
|
+
({id}: any) => useSharedCache(id, "scope"),
|
|
134
134
|
{
|
|
135
135
|
initialProps: {id: "id"},
|
|
136
136
|
},
|
|
@@ -142,14 +142,14 @@ describe("#useSharedCache", () => {
|
|
|
142
142
|
const result2 = wrapper.result.current;
|
|
143
143
|
|
|
144
144
|
// Assert
|
|
145
|
-
//
|
|
145
|
+
// @ts-expect-error [FEI-5019] - TS7053 - Element implicitly has an 'any' type because expression of type '1' can't be used to index type 'Error | [ValidCacheData | null | undefined, CacheValueFn<ValidCacheData>]'.
|
|
146
146
|
expect(result1[1]).not.toBe(result2[1]);
|
|
147
147
|
});
|
|
148
148
|
|
|
149
149
|
it("should be a new function if the scope changes", () => {
|
|
150
150
|
// Arrange
|
|
151
151
|
const wrapper = clientRenderHook(
|
|
152
|
-
({scope}) => useSharedCache("id", scope),
|
|
152
|
+
({scope}: any) => useSharedCache("id", scope),
|
|
153
153
|
{
|
|
154
154
|
initialProps: {scope: "scope"},
|
|
155
155
|
},
|
|
@@ -161,7 +161,7 @@ describe("#useSharedCache", () => {
|
|
|
161
161
|
const result2 = wrapper.result.current;
|
|
162
162
|
|
|
163
163
|
// Assert
|
|
164
|
-
//
|
|
164
|
+
// @ts-expect-error [FEI-5019] - TS7053 - Element implicitly has an 'any' type because expression of type '1' can't be used to index type 'Error | [ValidCacheData | null | undefined, CacheValueFn<ValidCacheData>]'.
|
|
165
165
|
expect(result1[1]).not.toBe(result2[1]);
|
|
166
166
|
});
|
|
167
167
|
|
|
@@ -186,7 +186,7 @@ describe("#useSharedCache", () => {
|
|
|
186
186
|
value
|
|
187
187
|
${undefined}
|
|
188
188
|
${null}
|
|
189
|
-
`("should purge the value from the cache if $value", ({value}) => {
|
|
189
|
+
`("should purge the value from the cache if $value", ({value}: any) => {
|
|
190
190
|
// Arrange
|
|
191
191
|
const wrapper = clientRenderHook(() =>
|
|
192
192
|
useSharedCache("id", "scope"),
|
|
@@ -225,7 +225,7 @@ describe("#useSharedCache", () => {
|
|
|
225
225
|
id
|
|
226
226
|
${"id1"}
|
|
227
227
|
${"id2"}
|
|
228
|
-
`("should not share cache if scope is different", ({id}) => {
|
|
228
|
+
`("should not share cache if scope is different", ({id}: any) => {
|
|
229
229
|
// Arrange
|
|
230
230
|
const hook1 = clientRenderHook(() => useSharedCache("id1", "scope1"));
|
|
231
231
|
const hook2 = clientRenderHook(() => useSharedCache(id, "scope2"));
|
|
@@ -243,7 +243,7 @@ describe("#useSharedCache", () => {
|
|
|
243
243
|
scope
|
|
244
244
|
${"scope1"}
|
|
245
245
|
${"scope2"}
|
|
246
|
-
`("should not share cache if id is different", ({scope}) => {
|
|
246
|
+
`("should not share cache if id is different", ({scope}: any) => {
|
|
247
247
|
// Arrange
|
|
248
248
|
const hook1 = clientRenderHook(() => useSharedCache("id1", "scope1"));
|
|
249
249
|
const hook2 = clientRenderHook(() => useSharedCache("id2", scope));
|