@khanacademy/wonder-blocks-testing 7.0.5 → 7.1.2

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.
@@ -1,460 +0,0 @@
1
- // @flow
2
- import {RespondWith, makeMockResponse} from "../make-mock-response.js";
3
-
4
- describe("RespondWith", () => {
5
- describe("#text", () => {
6
- it("should have type text", () => {
7
- // Arrange
8
-
9
- // Act
10
- const result = RespondWith.text("SOME TEXT");
11
-
12
- // Assert
13
- expect(result).toHaveProperty("type", "text");
14
- });
15
-
16
- it("should provide the given text", () => {
17
- // Arrange
18
-
19
- // Act
20
- const mockResponse = RespondWith.text("SOME TEXT");
21
- // $FlowIgnore[incompatible-use]
22
- const result = mockResponse.text;
23
-
24
- // Assert
25
- expect(result).toEqual("SOME TEXT");
26
- });
27
- });
28
-
29
- describe("#json", () => {
30
- it("should have type text", () => {
31
- // Arrange
32
-
33
- // Act
34
- const result = RespondWith.json({});
35
-
36
- // Assert
37
- expect(result).toHaveProperty("type", "text");
38
- });
39
-
40
- it("should provide the given data in the text", () => {
41
- // Arrange
42
- const json = {
43
- foo: "bar",
44
- };
45
-
46
- // Act
47
- const mockResponse = RespondWith.json(json);
48
- // $FlowIgnore[incompatible-use]
49
- const result = mockResponse.text();
50
-
51
- // Assert
52
- expect(result).toEqual(JSON.stringify(json));
53
- });
54
- });
55
-
56
- describe("#graphQLData", () => {
57
- it("should have type text", () => {
58
- // Arrange
59
-
60
- // Act
61
- const result = RespondWith.graphQLData({});
62
-
63
- // Assert
64
- expect(result).toHaveProperty("type", "text");
65
- });
66
-
67
- it("should provide the given data in the text", () => {
68
- // Arrange
69
- const data = {
70
- foo: "bar",
71
- };
72
-
73
- // Act
74
- const mockResponse = RespondWith.graphQLData(data);
75
- // $FlowIgnore[incompatible-use]
76
- const result = mockResponse.text();
77
-
78
- // Assert
79
- expect(result).toEqual(JSON.stringify({data}));
80
- });
81
- });
82
-
83
- describe("#unparseableBody", () => {
84
- it("should have type text", () => {
85
- // Arrange
86
-
87
- // Act
88
- const result = RespondWith.unparseableBody();
89
-
90
- // Assert
91
- expect(result).toHaveProperty("type", "text");
92
- });
93
-
94
- it("should have text that is unparseable json", () => {
95
- // Arrange
96
-
97
- // Act
98
- const mockResponse = RespondWith.unparseableBody();
99
- // $FlowIgnore[incompatible-use]
100
- const underTest = () => JSON.parse(mockResponse.text);
101
-
102
- // Assert
103
- expect(underTest).toThrowErrorMatchingInlineSnapshot(
104
- `"Unexpected token I in JSON at position 0"`,
105
- );
106
- });
107
- });
108
-
109
- describe("#abortedRequest", () => {
110
- it("should have type reject", () => {
111
- // Arrange
112
-
113
- // Act
114
- const result = RespondWith.abortedRequest();
115
-
116
- // Assert
117
- expect(result).toHaveProperty("type", "reject");
118
- });
119
-
120
- it("should provide AbortError", () => {
121
- // Arrange
122
-
123
- // Act
124
- const mockResponse = RespondWith.abortedRequest();
125
- // $FlowIgnore[incompatible-use]
126
- const result = mockResponse.error();
127
-
128
- // Assert
129
- expect(result).toMatchInlineSnapshot(
130
- `[AbortError: Mock request aborted]`,
131
- );
132
- });
133
- });
134
-
135
- describe("#reject", () => {
136
- it("should have type reject", () => {
137
- // Arrange
138
-
139
- // Act
140
- const result = RespondWith.reject(new Error("BOOM!"));
141
-
142
- // Assert
143
- expect(result).toHaveProperty("type", "reject");
144
- });
145
-
146
- it("should have the given error", () => {
147
- // Arrange
148
- const error = new Error("BOOM!");
149
-
150
- // Act
151
- const mockResponse = RespondWith.reject(error);
152
- // $FlowIgnore[incompatible-use]
153
- const result = mockResponse.error;
154
-
155
- // Assert
156
- expect(result).toBe(error);
157
- });
158
- });
159
-
160
- describe("#errorStatusCode", () => {
161
- it("should have type text", () => {
162
- // Arrange
163
-
164
- // Act
165
- const result = RespondWith.errorStatusCode(400);
166
-
167
- // Assert
168
- expect(result).toHaveProperty("type", "text");
169
- });
170
-
171
- it("should include the given status code", () => {
172
- // Arrange
173
-
174
- // Act
175
- const result = RespondWith.errorStatusCode(400);
176
-
177
- // Assert
178
- expect(result).toHaveProperty("statusCode", 400);
179
- });
180
-
181
- it("should throw if the status code represents success", () => {
182
- // Arrange
183
-
184
- // Act
185
- const result = () => RespondWith.errorStatusCode(200);
186
-
187
- // Assert
188
- expect(result).toThrowErrorMatchingInlineSnapshot(
189
- `"200 is not a valid error status code"`,
190
- );
191
- });
192
- });
193
-
194
- describe("#nonGraphQLBody", () => {
195
- it("should have type invalid", () => {
196
- // Arrange
197
-
198
- // Act
199
- const result = RespondWith.nonGraphQLBody();
200
-
201
- // Assert
202
- expect(result).toHaveProperty("type", "text");
203
- });
204
-
205
- it("should have text that is valid json", () => {
206
- // Arrange
207
-
208
- // Act
209
- const mockResponse = RespondWith.nonGraphQLBody();
210
- // $FlowIgnore[incompatible-use]
211
- const underTest = () => JSON.parse(mockResponse.text());
212
-
213
- // Assert
214
- expect(underTest).not.toThrow();
215
- });
216
-
217
- it("should have text that is not a valid GraphQL response", () => {
218
- // Arrange
219
-
220
- // Act
221
- const mockResponse = RespondWith.nonGraphQLBody();
222
- // $FlowIgnore[incompatible-use]
223
- const result = JSON.parse(mockResponse.text());
224
-
225
- // Assert
226
- expect(result).toMatchInlineSnapshot(`
227
- Object {
228
- "that": "is not a valid graphql response",
229
- "valid": "json",
230
- }
231
- `);
232
- });
233
- });
234
-
235
- describe("#graphQLErrors", () => {
236
- it("should have type test", () => {
237
- // Arrange
238
-
239
- // Act
240
- const result = RespondWith.graphQLErrors([]);
241
-
242
- // Assert
243
- expect(result).toHaveProperty("type", "text");
244
- });
245
-
246
- it("should include the given error messages", () => {
247
- // Arrange
248
- const errorMessages = ["foo", "bar"];
249
-
250
- // Act
251
- const mockResponse = RespondWith.graphQLErrors(errorMessages);
252
- // $FlowIgnore[incompatible-use]
253
- const result = JSON.parse(mockResponse.text());
254
-
255
- // Assert
256
- expect(result).toMatchInlineSnapshot(`
257
- Object {
258
- "errors": Array [
259
- Object {
260
- "message": "foo",
261
- },
262
- Object {
263
- "message": "bar",
264
- },
265
- ],
266
- }
267
- `);
268
- });
269
- });
270
- });
271
-
272
- describe("#makeGqlErrorResponse", () => {
273
- it("should throw for unknown response type", () => {
274
- // Arrange
275
-
276
- // Act
277
- const result = () =>
278
- makeMockResponse(({type: "NOT A VALID TYPE"}: any));
279
-
280
- // Assert
281
- expect(result).toThrowErrorMatchingInlineSnapshot(
282
- `"Unknown response type: NOT A VALID TYPE"`,
283
- );
284
- });
285
-
286
- describe("data response", () => {
287
- it("should resolve to have a successful status code", async () => {
288
- // Arrange
289
- const mockResponse = RespondWith.graphQLData({});
290
-
291
- // Act
292
- const result = await makeMockResponse(mockResponse);
293
-
294
- // Assert
295
- expect(result.status).toBe(200);
296
- });
297
-
298
- it("should resolve to response with text() function that resolves to GraphQL data result", async () => {
299
- // Arrange
300
- const data = {
301
- foo: "bar",
302
- };
303
- const mockResponse = RespondWith.graphQLData(data);
304
-
305
- // Act
306
- const response = await makeMockResponse(mockResponse);
307
- const result = await response.text();
308
-
309
- // Assert
310
- expect(result).toEqual(JSON.stringify({data}));
311
- });
312
- });
313
-
314
- describe("unparseable body response", () => {
315
- it("should resolve to have a successful status code", async () => {
316
- // Arrange
317
- const mockResponse = RespondWith.unparseableBody();
318
-
319
- // Act
320
- const result = await makeMockResponse(mockResponse);
321
-
322
- // Assert
323
- expect(result.status).toBe(200);
324
- });
325
-
326
- it("should resolve to response with text() function that resolves to non-JSON text", async () => {
327
- // Arrange
328
- const mockResponse = RespondWith.unparseableBody();
329
-
330
- // Act
331
- const response = await makeMockResponse(mockResponse);
332
- const text = await response.text();
333
- const act = () => JSON.parse(text);
334
-
335
- // Assert
336
- expect(act).toThrowError();
337
- });
338
- });
339
-
340
- describe("aborted request response", () => {
341
- it("should reject with error", async () => {
342
- // Arrange
343
- const mockResponse = RespondWith.abortedRequest();
344
-
345
- // Act
346
- const act = () => makeMockResponse(mockResponse);
347
-
348
- // Assert
349
- await expect(act).rejects.toBeInstanceOf(Error);
350
- });
351
-
352
- it("should reject with an AbortError", async () => {
353
- // Arrange
354
- const mockResponse = RespondWith.abortedRequest();
355
-
356
- // Act
357
- const act = makeMockResponse(mockResponse);
358
-
359
- // Assert
360
- await expect(act).rejects.toHaveProperty("name", "AbortError");
361
- });
362
- });
363
-
364
- describe("rejection", () => {
365
- it("should reject with error", async () => {
366
- // Arrange
367
- const error = new Error("BOOM!");
368
- const mockResponse = RespondWith.reject(error);
369
-
370
- // Act
371
- const act = () => makeMockResponse(mockResponse);
372
-
373
- // Assert
374
- await expect(act).rejects.toBe(error);
375
- });
376
- });
377
-
378
- describe("error status code response", () => {
379
- it("should resolve to have the given status code", async () => {
380
- // Arrange
381
- const mockResponse = RespondWith.errorStatusCode(400);
382
-
383
- // Act
384
- const result = await makeMockResponse(mockResponse);
385
-
386
- // Assert
387
- expect(result.status).toBe(400);
388
- });
389
-
390
- it("should resolve to response with text() function that resolves to some parseable JSON", async () => {
391
- // Arrange
392
- const mockResponse = RespondWith.errorStatusCode(400);
393
-
394
- // Act
395
- const response = await makeMockResponse(mockResponse);
396
- const text = await response.text();
397
- const act = () => JSON.parse(text);
398
-
399
- // Assert
400
- expect(act).not.toThrowError();
401
- });
402
- });
403
-
404
- describe("non-graphql body response", () => {
405
- it("should resolve to have a successful status code", async () => {
406
- // Arrange
407
- const mockResponse = RespondWith.nonGraphQLBody();
408
-
409
- // Act
410
- const result = await makeMockResponse(mockResponse);
411
-
412
- // Assert
413
- expect(result.status).toBe(200);
414
- });
415
-
416
- it("should resolve to response with text() function that resolves to JSON parseable text that is not a valid GraphQL response", async () => {
417
- // Arrange
418
- const mockResponse = RespondWith.nonGraphQLBody();
419
-
420
- // Act
421
- const response = await makeMockResponse(mockResponse);
422
- const text = await response.text();
423
- const result = JSON.parse(text);
424
-
425
- // Assert
426
- expect(result).not.toHaveProperty("data");
427
- expect(result).not.toHaveProperty("errors");
428
- });
429
- });
430
-
431
- describe("graphql error response", () => {
432
- it("should resolve to have a successful status code", async () => {
433
- // Arrange
434
- const mockResponse = RespondWith.graphQLErrors([]);
435
-
436
- // Act
437
- const result = await makeMockResponse(mockResponse);
438
-
439
- // Assert
440
- expect(result.status).toBe(200);
441
- });
442
-
443
- it("should resolve to response with text() function that resolves to GraphQL error result", async () => {
444
- // Arrange
445
- const errorMessages = ["foo", "bar"];
446
- const mockResponse = RespondWith.graphQLErrors(errorMessages);
447
-
448
- // Act
449
- const response = await makeMockResponse(mockResponse);
450
- const text = await response.text();
451
- const result = JSON.parse(text);
452
-
453
- // Assert
454
- expect(result).toHaveProperty("errors", [
455
- {message: "foo"},
456
- {message: "bar"},
457
- ]);
458
- });
459
- });
460
- });
@@ -1,150 +0,0 @@
1
- // @flow
2
- import {ResponseImpl} from "./response-impl.js";
3
- import type {GraphQLJson} from "./types.js";
4
-
5
- /**
6
- * Describes a mock response to a fetch request.
7
- */
8
- export opaque type MockResponse<TJson> =
9
- | {|
10
- type: "text",
11
- text: string | (() => string),
12
- statusCode: number,
13
- |}
14
- | {|
15
- type: "reject",
16
- error: Error | (() => Error),
17
- |};
18
-
19
- /**
20
- * Helper for creating a text-based mock response.
21
- */
22
- const textResponse = <TData>(
23
- text: string | (() => string),
24
- statusCode: number = 200,
25
- ): MockResponse<TData> => ({
26
- type: "text",
27
- text,
28
- statusCode,
29
- });
30
-
31
- /**
32
- * Helper for creating a rejected mock response.
33
- */
34
- const rejectResponse = (error: Error | (() => Error)): MockResponse<empty> => ({
35
- type: "reject",
36
- error,
37
- });
38
-
39
- /**
40
- * Helpers to define mock responses for mocked requests.
41
- */
42
- export const RespondWith = Object.freeze({
43
- /**
44
- * Response with text body and status code.
45
- * Status code defaults to 200.
46
- */
47
- text: <TData = string>(
48
- text: string,
49
- statusCode: number = 200,
50
- ): MockResponse<TData> => textResponse<TData>(text, statusCode),
51
-
52
- /**
53
- * Response with JSON body and status code 200.
54
- */
55
- json: <TJson: {...}>(json: TJson): MockResponse<TJson> =>
56
- textResponse<TJson>(() => JSON.stringify(json)),
57
-
58
- /**
59
- * Response with GraphQL data JSON body and status code 200.
60
- */
61
- graphQLData: <TData: {...}>(
62
- data: TData,
63
- ): MockResponse<GraphQLJson<TData>> =>
64
- textResponse<GraphQLJson<TData>>(() => JSON.stringify({data})),
65
-
66
- /**
67
- * Response with body that will not parse as JSON and status code 200.
68
- */
69
- unparseableBody: (): MockResponse<any> => textResponse("INVALID JSON"),
70
-
71
- /**
72
- * Rejects with an AbortError to simulate an aborted request.
73
- */
74
- abortedRequest: (): MockResponse<any> =>
75
- rejectResponse(() => {
76
- const abortError = new Error("Mock request aborted");
77
- abortError.name = "AbortError";
78
- return abortError;
79
- }),
80
-
81
- /**
82
- * Rejects with the given error.
83
- */
84
- reject: (error: Error): MockResponse<any> => rejectResponse(error),
85
-
86
- /**
87
- * A non-200 status code with empty text body.
88
- * Equivalent to calling `ResponseWith.text("", statusCode)`.
89
- */
90
- errorStatusCode: (statusCode: number): MockResponse<any> => {
91
- if (statusCode < 300) {
92
- throw new Error(`${statusCode} is not a valid error status code`);
93
- }
94
- return textResponse("{}", statusCode);
95
- },
96
-
97
- /**
98
- * Response body that is valid JSON but not a valid GraphQL response.
99
- */
100
- nonGraphQLBody: (): MockResponse<any> =>
101
- textResponse(() =>
102
- JSON.stringify({
103
- valid: "json",
104
- that: "is not a valid graphql response",
105
- }),
106
- ),
107
-
108
- /**
109
- * Response that is a GraphQL errors response with status code 200.
110
- */
111
- graphQLErrors: (
112
- errorMessages: $ReadOnlyArray<string>,
113
- ): MockResponse<GraphQLJson<any>> =>
114
- textResponse<GraphQLJson<any>>(() =>
115
- JSON.stringify({
116
- errors: errorMessages.map((e) => ({
117
- message: e,
118
- })),
119
- }),
120
- ),
121
- });
122
-
123
- /**
124
- * Turns a MockResponse value to an actual Response that represents the mock.
125
- */
126
- export const makeMockResponse = (
127
- response: MockResponse<any>,
128
- ): Promise<Response> => {
129
- switch (response.type) {
130
- case "text":
131
- const text =
132
- typeof response.text === "function"
133
- ? response.text()
134
- : response.text;
135
-
136
- return Promise.resolve(
137
- new ResponseImpl(text, {status: response.statusCode}),
138
- );
139
-
140
- case "reject":
141
- const error =
142
- response.error instanceof Error
143
- ? response.error
144
- : response.error();
145
- return Promise.reject(error);
146
-
147
- default:
148
- throw new Error(`Unknown response type: ${response.type}`);
149
- }
150
- };