@khanacademy/wonder-blocks-data 13.0.11 → 14.0.0
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 +19 -0
- package/package.json +5 -5
- 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,132 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import {renderHook} from "@testing-library/react-hooks";
|
|
3
|
-
|
|
4
|
-
import {GqlRouterContext} from "../../util/gql-router-context";
|
|
5
|
-
import {useGqlRouterContext} from "../use-gql-router-context";
|
|
6
|
-
|
|
7
|
-
describe("#useGqlRouterContext", () => {
|
|
8
|
-
it("should throw if there is no GqlRouterContext", () => {
|
|
9
|
-
// Arrange
|
|
10
|
-
|
|
11
|
-
// Act
|
|
12
|
-
const {
|
|
13
|
-
result: {error: result},
|
|
14
|
-
} = renderHook(() => useGqlRouterContext());
|
|
15
|
-
|
|
16
|
-
// Assert
|
|
17
|
-
expect(result).toMatchInlineSnapshot(
|
|
18
|
-
`[InternalGqlError: No GqlRouter]`,
|
|
19
|
-
);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("should return an equivalent to the GqlRouterContext if no overrides given", () => {
|
|
23
|
-
// Arrange
|
|
24
|
-
const baseContext = {
|
|
25
|
-
fetch: jest.fn(),
|
|
26
|
-
defaultContext: {
|
|
27
|
-
foo: "bar",
|
|
28
|
-
},
|
|
29
|
-
} as const;
|
|
30
|
-
const Wrapper = ({children}: any) => (
|
|
31
|
-
<GqlRouterContext.Provider value={baseContext}>
|
|
32
|
-
{children}
|
|
33
|
-
</GqlRouterContext.Provider>
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
// Act
|
|
37
|
-
const {
|
|
38
|
-
result: {current: result},
|
|
39
|
-
} = renderHook(() => useGqlRouterContext(), {wrapper: Wrapper});
|
|
40
|
-
|
|
41
|
-
// Assert
|
|
42
|
-
expect(result).toStrictEqual(baseContext);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("should return the same object if nothing has changed", () => {
|
|
46
|
-
// Arrange
|
|
47
|
-
const baseContext = {
|
|
48
|
-
fetch: jest.fn(),
|
|
49
|
-
defaultContext: {
|
|
50
|
-
foo: "bar",
|
|
51
|
-
},
|
|
52
|
-
} as const;
|
|
53
|
-
const Wrapper = ({children}: any) => (
|
|
54
|
-
<GqlRouterContext.Provider value={baseContext}>
|
|
55
|
-
{children}
|
|
56
|
-
</GqlRouterContext.Provider>
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
// Act
|
|
60
|
-
const wrapper = renderHook(() => useGqlRouterContext(), {
|
|
61
|
-
wrapper: Wrapper,
|
|
62
|
-
});
|
|
63
|
-
const result1 = wrapper.result.current;
|
|
64
|
-
wrapper.rerender();
|
|
65
|
-
const result2 = wrapper.result.current;
|
|
66
|
-
|
|
67
|
-
// Assert
|
|
68
|
-
expect(result1).toBe(result2);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("should return the same object if the object adds overrides that don't change the merged context", () => {
|
|
72
|
-
// Arrange
|
|
73
|
-
const baseContext = {
|
|
74
|
-
fetch: jest.fn(),
|
|
75
|
-
defaultContext: {
|
|
76
|
-
foo: "bar",
|
|
77
|
-
},
|
|
78
|
-
} as const;
|
|
79
|
-
const Wrapper = ({children}: any) => (
|
|
80
|
-
<GqlRouterContext.Provider value={baseContext}>
|
|
81
|
-
{children}
|
|
82
|
-
</GqlRouterContext.Provider>
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
// Act
|
|
86
|
-
const wrapper = renderHook(
|
|
87
|
-
({overrides}: any) => useGqlRouterContext(overrides),
|
|
88
|
-
{
|
|
89
|
-
wrapper: Wrapper,
|
|
90
|
-
initialProps: {},
|
|
91
|
-
},
|
|
92
|
-
);
|
|
93
|
-
const result1 = wrapper.result.current;
|
|
94
|
-
wrapper.rerender({overrides: {foo: "bar"}});
|
|
95
|
-
const result2 = wrapper.result.current;
|
|
96
|
-
|
|
97
|
-
// Assert
|
|
98
|
-
expect(result1).toBe(result2);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("should return an updated object if the object adds overrides that change the merged context", () => {
|
|
102
|
-
// Arrange
|
|
103
|
-
const baseContext = {
|
|
104
|
-
fetch: jest.fn(),
|
|
105
|
-
defaultContext: {
|
|
106
|
-
foo: "bar",
|
|
107
|
-
},
|
|
108
|
-
} as const;
|
|
109
|
-
const Wrapper = ({children}: any) => (
|
|
110
|
-
<GqlRouterContext.Provider value={baseContext}>
|
|
111
|
-
{children}
|
|
112
|
-
</GqlRouterContext.Provider>
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
// Act
|
|
116
|
-
const wrapper = renderHook(
|
|
117
|
-
({overrides}: any) => useGqlRouterContext(overrides),
|
|
118
|
-
{
|
|
119
|
-
wrapper: Wrapper,
|
|
120
|
-
initialProps: {
|
|
121
|
-
overrides: {fiz: "baz"},
|
|
122
|
-
},
|
|
123
|
-
},
|
|
124
|
-
);
|
|
125
|
-
const result1 = wrapper.result.current;
|
|
126
|
-
wrapper.rerender({overrides: {} as any});
|
|
127
|
-
const result2 = wrapper.result.current;
|
|
128
|
-
|
|
129
|
-
// Assert
|
|
130
|
-
expect(result1).not.toBe(result2);
|
|
131
|
-
});
|
|
132
|
-
});
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import {renderHook} from "@testing-library/react-hooks";
|
|
3
|
-
|
|
4
|
-
import * as GetGqlDataFromResponse from "../../util/get-gql-data-from-response";
|
|
5
|
-
import {GqlRouterContext} from "../../util/gql-router-context";
|
|
6
|
-
import {useGql} from "../use-gql";
|
|
7
|
-
|
|
8
|
-
describe("#useGql", () => {
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
jest.resetAllMocks();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it("should throw if there is no GqlRouterContext available", () => {
|
|
14
|
-
// Arrange
|
|
15
|
-
|
|
16
|
-
// Act
|
|
17
|
-
const {
|
|
18
|
-
result: {error: result},
|
|
19
|
-
} = renderHook(() => useGql());
|
|
20
|
-
|
|
21
|
-
// Assert
|
|
22
|
-
expect(result).toMatchInlineSnapshot(
|
|
23
|
-
`[InternalGqlError: No GqlRouter]`,
|
|
24
|
-
);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("should return a function", () => {
|
|
28
|
-
// Arrange
|
|
29
|
-
const gqlRouterContext = {
|
|
30
|
-
fetch: jest.fn(),
|
|
31
|
-
defaultContext: {},
|
|
32
|
-
} as const;
|
|
33
|
-
|
|
34
|
-
// Act
|
|
35
|
-
const {
|
|
36
|
-
result: {current: result},
|
|
37
|
-
} = renderHook(() => useGql(), {
|
|
38
|
-
wrapper: ({children}: any) => (
|
|
39
|
-
<GqlRouterContext.Provider value={gqlRouterContext}>
|
|
40
|
-
{children}
|
|
41
|
-
</GqlRouterContext.Provider>
|
|
42
|
-
),
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Assert
|
|
46
|
-
expect(result).toBeInstanceOf(Function);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
describe("returned gqlFetch", () => {
|
|
50
|
-
it("should fetch the operation with combined context", async () => {
|
|
51
|
-
// Arrange
|
|
52
|
-
jest.spyOn(
|
|
53
|
-
GetGqlDataFromResponse,
|
|
54
|
-
"getGqlDataFromResponse",
|
|
55
|
-
).mockResolvedValue({
|
|
56
|
-
some: "data",
|
|
57
|
-
});
|
|
58
|
-
const fetchFake = jest
|
|
59
|
-
.fn()
|
|
60
|
-
.mockResolvedValue("FAKE_RESPONSE" as any);
|
|
61
|
-
const gqlRouterContext = {
|
|
62
|
-
fetch: fetchFake,
|
|
63
|
-
defaultContext: {
|
|
64
|
-
a: "defaultA",
|
|
65
|
-
b: "defaultB",
|
|
66
|
-
},
|
|
67
|
-
} as const;
|
|
68
|
-
const {
|
|
69
|
-
result: {current: gqlFetch},
|
|
70
|
-
} = renderHook(() => useGql(), {
|
|
71
|
-
wrapper: ({children}: any) => (
|
|
72
|
-
<GqlRouterContext.Provider value={gqlRouterContext}>
|
|
73
|
-
{children}
|
|
74
|
-
</GqlRouterContext.Provider>
|
|
75
|
-
),
|
|
76
|
-
});
|
|
77
|
-
const gqlOp = {
|
|
78
|
-
type: "query",
|
|
79
|
-
id: "MyQuery",
|
|
80
|
-
} as const;
|
|
81
|
-
const gqlOpContext = {
|
|
82
|
-
a: undefined, // This should not get included.
|
|
83
|
-
b: "overrideB",
|
|
84
|
-
} as const;
|
|
85
|
-
const gqlOpVariables = {
|
|
86
|
-
var1: "val1",
|
|
87
|
-
} as const;
|
|
88
|
-
|
|
89
|
-
// Act
|
|
90
|
-
await gqlFetch(gqlOp, {
|
|
91
|
-
context: gqlOpContext,
|
|
92
|
-
variables: gqlOpVariables,
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// Assert
|
|
96
|
-
expect(fetchFake).toHaveBeenCalledWith(gqlOp, gqlOpVariables, {
|
|
97
|
-
a: "defaultA",
|
|
98
|
-
b: "overrideB",
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it("should parse the response", async () => {
|
|
103
|
-
// Arrange
|
|
104
|
-
const getGqlDataFromResponseSpy = jest
|
|
105
|
-
.spyOn(GetGqlDataFromResponse, "getGqlDataFromResponse")
|
|
106
|
-
.mockResolvedValue({
|
|
107
|
-
some: "data",
|
|
108
|
-
});
|
|
109
|
-
const gqlRouterContext = {
|
|
110
|
-
fetch: jest.fn().mockResolvedValue("FAKE_RESPONSE" as any),
|
|
111
|
-
defaultContext: {},
|
|
112
|
-
} as const;
|
|
113
|
-
const {
|
|
114
|
-
result: {current: gqlFetch},
|
|
115
|
-
} = renderHook(() => useGql(), {
|
|
116
|
-
wrapper: ({children}: any) => (
|
|
117
|
-
<GqlRouterContext.Provider value={gqlRouterContext}>
|
|
118
|
-
{children}
|
|
119
|
-
</GqlRouterContext.Provider>
|
|
120
|
-
),
|
|
121
|
-
});
|
|
122
|
-
const gqlOp = {
|
|
123
|
-
type: "query",
|
|
124
|
-
id: "MyQuery",
|
|
125
|
-
} as const;
|
|
126
|
-
|
|
127
|
-
// Act
|
|
128
|
-
await gqlFetch(gqlOp);
|
|
129
|
-
|
|
130
|
-
// Assert
|
|
131
|
-
expect(getGqlDataFromResponseSpy).toHaveBeenCalledWith(
|
|
132
|
-
"FAKE_RESPONSE",
|
|
133
|
-
);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("should reject if the response parse rejects", async () => {
|
|
137
|
-
// Arrange
|
|
138
|
-
jest.spyOn(
|
|
139
|
-
GetGqlDataFromResponse,
|
|
140
|
-
"getGqlDataFromResponse",
|
|
141
|
-
).mockRejectedValue(new Error("FAKE_ERROR"));
|
|
142
|
-
const gqlRouterContext = {
|
|
143
|
-
fetch: jest.fn().mockResolvedValue("FAKE_RESPONSE" as any),
|
|
144
|
-
defaultContext: {},
|
|
145
|
-
} as const;
|
|
146
|
-
const {
|
|
147
|
-
result: {current: gqlFetch},
|
|
148
|
-
} = renderHook(() => useGql(), {
|
|
149
|
-
wrapper: ({children}: any) => (
|
|
150
|
-
<GqlRouterContext.Provider value={gqlRouterContext}>
|
|
151
|
-
{children}
|
|
152
|
-
</GqlRouterContext.Provider>
|
|
153
|
-
),
|
|
154
|
-
});
|
|
155
|
-
const gqlOp = {
|
|
156
|
-
type: "query",
|
|
157
|
-
id: "MyQuery",
|
|
158
|
-
} as const;
|
|
159
|
-
|
|
160
|
-
// Act
|
|
161
|
-
const act = gqlFetch(gqlOp);
|
|
162
|
-
|
|
163
|
-
// Assert
|
|
164
|
-
await expect(act).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
165
|
-
`"FAKE_ERROR"`,
|
|
166
|
-
);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it("should resolve to the response data", async () => {
|
|
170
|
-
// Arrange
|
|
171
|
-
jest.spyOn(
|
|
172
|
-
GetGqlDataFromResponse,
|
|
173
|
-
"getGqlDataFromResponse",
|
|
174
|
-
).mockResolvedValue({
|
|
175
|
-
some: "data",
|
|
176
|
-
});
|
|
177
|
-
const gqlRouterContext = {
|
|
178
|
-
fetch: jest.fn().mockResolvedValue("FAKE_RESPONSE" as any),
|
|
179
|
-
defaultContext: {},
|
|
180
|
-
} as const;
|
|
181
|
-
const {
|
|
182
|
-
result: {current: gqlFetch},
|
|
183
|
-
} = renderHook(() => useGql(), {
|
|
184
|
-
wrapper: ({children}: any) => (
|
|
185
|
-
<GqlRouterContext.Provider value={gqlRouterContext}>
|
|
186
|
-
{children}
|
|
187
|
-
</GqlRouterContext.Provider>
|
|
188
|
-
),
|
|
189
|
-
});
|
|
190
|
-
const gqlOp = {
|
|
191
|
-
type: "mutation",
|
|
192
|
-
id: "MyMutation",
|
|
193
|
-
} as const;
|
|
194
|
-
|
|
195
|
-
// Act
|
|
196
|
-
const result = await gqlFetch(gqlOp);
|
|
197
|
-
|
|
198
|
-
// Assert
|
|
199
|
-
expect(result).toEqual({
|
|
200
|
-
some: "data",
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
});
|