@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,186 +0,0 @@
|
|
|
1
|
-
import {getGqlDataFromResponse} from "../get-gql-data-from-response";
|
|
2
|
-
|
|
3
|
-
describe("#getGqlDataFromReponse", () => {
|
|
4
|
-
it("should throw if the response cannot be parsed", async () => {
|
|
5
|
-
// Arrange
|
|
6
|
-
const response: any = {
|
|
7
|
-
status: 200,
|
|
8
|
-
text: jest.fn(() => Promise.resolve("BAD JSON")),
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
// Act
|
|
12
|
-
const result = getGqlDataFromResponse(response);
|
|
13
|
-
|
|
14
|
-
// Assert
|
|
15
|
-
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
|
|
16
|
-
"Failed to parse response
|
|
17
|
-
caused by
|
|
18
|
-
SyntaxError: Unexpected token 'B', "BAD JSON" is not valid JSON"
|
|
19
|
-
`);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("should include status code and body text in parse error metadata", async () => {
|
|
23
|
-
// Arrange
|
|
24
|
-
const response: any = {
|
|
25
|
-
status: 200,
|
|
26
|
-
text: jest.fn(() => Promise.resolve("BAD JSON")),
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// Act
|
|
30
|
-
const result = getGqlDataFromResponse(response);
|
|
31
|
-
|
|
32
|
-
// Assert
|
|
33
|
-
await expect(result).rejects.toHaveProperty("metadata", {
|
|
34
|
-
statusCode: 200,
|
|
35
|
-
bodyText: "BAD JSON",
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("should throw if the status code is not <300", async () => {
|
|
40
|
-
// Arrange
|
|
41
|
-
const response: any = {
|
|
42
|
-
status: 400,
|
|
43
|
-
text: jest.fn(() => Promise.resolve("{}")),
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// Act
|
|
47
|
-
const result = getGqlDataFromResponse(response);
|
|
48
|
-
|
|
49
|
-
// Assert
|
|
50
|
-
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
51
|
-
`"Response unsuccessful"`,
|
|
52
|
-
);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it("should include status code and result in response error metadata", async () => {
|
|
56
|
-
// Arrange
|
|
57
|
-
const response: any = {
|
|
58
|
-
status: 400,
|
|
59
|
-
text: jest.fn(() =>
|
|
60
|
-
Promise.resolve(JSON.stringify({data: "DATA"})),
|
|
61
|
-
),
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// Act
|
|
65
|
-
const result = getGqlDataFromResponse(response);
|
|
66
|
-
|
|
67
|
-
// Assert
|
|
68
|
-
await expect(result).rejects.toHaveProperty("metadata", {
|
|
69
|
-
statusCode: 400,
|
|
70
|
-
result: {
|
|
71
|
-
data: "DATA",
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("should throw if the response is malformed", async () => {
|
|
77
|
-
// Arrange
|
|
78
|
-
const response: any = {
|
|
79
|
-
status: 200,
|
|
80
|
-
text: jest.fn(() => Promise.resolve("{}")),
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
// Act
|
|
84
|
-
const result = getGqlDataFromResponse(response);
|
|
85
|
-
|
|
86
|
-
// Assert
|
|
87
|
-
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
88
|
-
`"Server response missing"`,
|
|
89
|
-
);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it("should include the status code and the result in the malformed response error", async () => {
|
|
93
|
-
// Arrange
|
|
94
|
-
const response: any = {
|
|
95
|
-
status: 200,
|
|
96
|
-
text: jest.fn(() =>
|
|
97
|
-
Promise.resolve(JSON.stringify({malformed: "response"})),
|
|
98
|
-
),
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
// Act
|
|
102
|
-
const result = getGqlDataFromResponse(response);
|
|
103
|
-
|
|
104
|
-
// Assert
|
|
105
|
-
await expect(result).rejects.toHaveProperty("metadata", {
|
|
106
|
-
statusCode: 200,
|
|
107
|
-
result: {
|
|
108
|
-
malformed: "response",
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it("should throw if the response has GraphQL errors", async () => {
|
|
114
|
-
// Arrange
|
|
115
|
-
const response: any = {
|
|
116
|
-
status: 200,
|
|
117
|
-
text: jest.fn(() =>
|
|
118
|
-
Promise.resolve(
|
|
119
|
-
JSON.stringify({
|
|
120
|
-
data: {},
|
|
121
|
-
errors: [{message: "GraphQL error"}],
|
|
122
|
-
}),
|
|
123
|
-
),
|
|
124
|
-
),
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
// Act
|
|
128
|
-
const result = getGqlDataFromResponse(response);
|
|
129
|
-
|
|
130
|
-
// Assert
|
|
131
|
-
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
132
|
-
`"GraphQL errors"`,
|
|
133
|
-
);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("should include the status code and result in the metadata", async () => {
|
|
137
|
-
// Arrange
|
|
138
|
-
const response: any = {
|
|
139
|
-
status: 200,
|
|
140
|
-
text: jest.fn(() =>
|
|
141
|
-
Promise.resolve(
|
|
142
|
-
JSON.stringify({
|
|
143
|
-
data: {},
|
|
144
|
-
errors: [{message: "GraphQL error"}],
|
|
145
|
-
}),
|
|
146
|
-
),
|
|
147
|
-
),
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
// Act
|
|
151
|
-
const result = getGqlDataFromResponse(response);
|
|
152
|
-
|
|
153
|
-
// Assert
|
|
154
|
-
await expect(result).rejects.toHaveProperty("metadata", {
|
|
155
|
-
statusCode: 200,
|
|
156
|
-
result: {
|
|
157
|
-
data: {},
|
|
158
|
-
errors: [{message: "GraphQL error"}],
|
|
159
|
-
},
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it("should resolve to the response data", async () => {
|
|
164
|
-
// Arrange
|
|
165
|
-
const response: any = {
|
|
166
|
-
status: 200,
|
|
167
|
-
text: jest.fn(() =>
|
|
168
|
-
Promise.resolve(
|
|
169
|
-
JSON.stringify({
|
|
170
|
-
data: {
|
|
171
|
-
test: "test",
|
|
172
|
-
},
|
|
173
|
-
}),
|
|
174
|
-
),
|
|
175
|
-
),
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// Act
|
|
179
|
-
const result = getGqlDataFromResponse(response);
|
|
180
|
-
|
|
181
|
-
// Assert
|
|
182
|
-
await expect(result).resolves.toEqual({
|
|
183
|
-
test: "test",
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
});
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import {getGqlRequestId} from "../get-gql-request-id";
|
|
2
|
-
|
|
3
|
-
describe("#getGqlRequestId", () => {
|
|
4
|
-
it("should include the id of the query", () => {
|
|
5
|
-
// Arrange
|
|
6
|
-
const operation = {
|
|
7
|
-
type: "query",
|
|
8
|
-
id: "myQuery",
|
|
9
|
-
} as const;
|
|
10
|
-
|
|
11
|
-
// Act
|
|
12
|
-
const requestId = getGqlRequestId(operation, null, {
|
|
13
|
-
module: "MODULE",
|
|
14
|
-
curriculum: "CURRICULUM",
|
|
15
|
-
targetLocale: "LOCALE",
|
|
16
|
-
});
|
|
17
|
-
const result = new Set(requestId.split("|"));
|
|
18
|
-
|
|
19
|
-
// Assert
|
|
20
|
-
expect(result).toContain("myQuery");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("should include the context values sorted by key", () => {
|
|
24
|
-
// Arrange
|
|
25
|
-
const operation = {
|
|
26
|
-
type: "query",
|
|
27
|
-
id: "myQuery",
|
|
28
|
-
} as const;
|
|
29
|
-
const context = {
|
|
30
|
-
context3: "value3",
|
|
31
|
-
context2: "value2",
|
|
32
|
-
context1: "value1",
|
|
33
|
-
} as const;
|
|
34
|
-
|
|
35
|
-
// Act
|
|
36
|
-
const requestId = getGqlRequestId(operation, null, context);
|
|
37
|
-
const result = new Set(requestId.split("|"));
|
|
38
|
-
|
|
39
|
-
// Assert
|
|
40
|
-
expect(result).toContain(
|
|
41
|
-
`context1=value1&context2=value2&context3=value3`,
|
|
42
|
-
);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("should include the variables, sorted by key, if present", () => {
|
|
46
|
-
// Arrange
|
|
47
|
-
const operation = {
|
|
48
|
-
type: "query",
|
|
49
|
-
id: "myQuery",
|
|
50
|
-
} as const;
|
|
51
|
-
const variables = {
|
|
52
|
-
variable4: null,
|
|
53
|
-
variable2: 42,
|
|
54
|
-
variable1: "value1",
|
|
55
|
-
variable5: true,
|
|
56
|
-
variable3: undefined,
|
|
57
|
-
} as const;
|
|
58
|
-
|
|
59
|
-
// Act
|
|
60
|
-
const requestId = getGqlRequestId(operation, variables, {
|
|
61
|
-
module: "MODULE",
|
|
62
|
-
curriculum: "CURRICULUM",
|
|
63
|
-
targetLocale: "LOCALE",
|
|
64
|
-
});
|
|
65
|
-
const result = new Set(requestId.split("|"));
|
|
66
|
-
|
|
67
|
-
// Assert
|
|
68
|
-
expect(result).toContain(
|
|
69
|
-
`variable1=value1&variable2=42&variable3=&variable4=null&variable5=true`,
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("should sort nested variable properties", () => {
|
|
74
|
-
// Arrange
|
|
75
|
-
const operation = {
|
|
76
|
-
type: "query",
|
|
77
|
-
id: "myQuery",
|
|
78
|
-
} as const;
|
|
79
|
-
const variables = {
|
|
80
|
-
variable4: null,
|
|
81
|
-
variable2: 42,
|
|
82
|
-
variable1: "value1",
|
|
83
|
-
variable5: true,
|
|
84
|
-
variable3: undefined,
|
|
85
|
-
variable6: {
|
|
86
|
-
nested2: "nested2",
|
|
87
|
-
nested1: "nested1",
|
|
88
|
-
},
|
|
89
|
-
variable7: [1, 2, 3],
|
|
90
|
-
} as const;
|
|
91
|
-
|
|
92
|
-
// Act
|
|
93
|
-
const requestId = getGqlRequestId(operation, variables, {
|
|
94
|
-
module: "MODULE",
|
|
95
|
-
curriculum: "CURRICULUM",
|
|
96
|
-
targetLocale: "LOCALE",
|
|
97
|
-
});
|
|
98
|
-
const result = new Set(requestId.split("|"));
|
|
99
|
-
|
|
100
|
-
// Assert
|
|
101
|
-
expect(result).toContain(
|
|
102
|
-
`variable1=value1&variable2=42&variable3=&variable4=null&variable5=true&variable6.nested1=nested1&variable6.nested2=nested2&variable7.0=1&variable7.1=2&variable7.2=3`,
|
|
103
|
-
);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("should handle non-primitive values in variables", () => {
|
|
107
|
-
// Arrange
|
|
108
|
-
const operation = {
|
|
109
|
-
type: "query",
|
|
110
|
-
id: "myQuery",
|
|
111
|
-
} as const;
|
|
112
|
-
const variables = {
|
|
113
|
-
variable1: {
|
|
114
|
-
date: new Date("2020-01-01"),
|
|
115
|
-
error: new Error("BOOM!"),
|
|
116
|
-
},
|
|
117
|
-
} as const;
|
|
118
|
-
|
|
119
|
-
// Act
|
|
120
|
-
const requestId = getGqlRequestId(operation, variables, {
|
|
121
|
-
module: "MODULE",
|
|
122
|
-
curriculum: "CURRICULUM",
|
|
123
|
-
targetLocale: "LOCALE",
|
|
124
|
-
});
|
|
125
|
-
const result = new Set(requestId.split("|"));
|
|
126
|
-
|
|
127
|
-
// Assert
|
|
128
|
-
expect(result).toContain(
|
|
129
|
-
`variable1.date=2020-01-01T00%3A00%3A00.000Z&variable1.error=Error%3A+BOOM%21`,
|
|
130
|
-
);
|
|
131
|
-
});
|
|
132
|
-
});
|