@khanacademy/wonder-blocks-testing 3.0.1 → 4.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 +6 -0
- package/dist/es/index.js +212 -135
- package/dist/index.js +259 -138
- package/package.json +2 -1
- package/src/{gql/__tests__/make-gql-mock-response.test.js → __tests__/make-mock-response.test.js} +196 -34
- package/src/__tests__/mock-requester.test.js +213 -0
- package/src/__tests__/response-impl.test.js +47 -0
- package/src/fetch/__tests__/__snapshots__/mock-fetch.test.js.snap +29 -0
- package/src/fetch/__tests__/fetch-request-matches-mock.test.js +99 -0
- package/src/fetch/__tests__/mock-fetch.test.js +84 -0
- package/src/fetch/fetch-request-matches-mock.js +43 -0
- package/src/fetch/mock-fetch.js +19 -0
- package/src/fetch/types.js +18 -0
- package/src/gql/__tests__/mock-gql-fetch.test.js +24 -15
- package/src/gql/__tests__/wb-data-integration.test.js +7 -4
- package/src/gql/mock-gql-fetch.js +9 -80
- package/src/gql/types.js +11 -10
- package/src/index.js +9 -3
- package/src/make-mock-response.js +150 -0
- package/src/mock-requester.js +75 -0
- package/src/response-impl.js +9 -0
- package/src/types.js +39 -0
- package/src/gql/make-gql-mock-response.js +0 -124
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @khanacademy/wonder-blocks-testing
|
|
2
2
|
|
|
3
|
+
## 4.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- fce91b39: Introduced `mockFetch` and expanded `RespondWith` options. `RespondWith` responses will now be real `Response` instances (needs node-fetch peer dependency if no other implementation exists). Breaking changes: `RespondWith.data` is now `RespondWith.graphQLData`.
|
|
8
|
+
|
|
3
9
|
## 3.0.1
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/es/index.js
CHANGED
|
@@ -357,6 +357,215 @@ const fixtures = (componentOrOptions, fn) => {
|
|
|
357
357
|
return group.closeGroup(combinedAdapterOptions);
|
|
358
358
|
};
|
|
359
359
|
|
|
360
|
+
// We need a version of Response. When we're in Jest JSDOM environment or a
|
|
361
|
+
// version of Node that supports the fetch API (17 and up, possibly with
|
|
362
|
+
// --experimental-fetch flag), then we're good, but otherwise we need an
|
|
363
|
+
// implementation, so this uses node-fetch as a peer dependency and uses that
|
|
364
|
+
// to provide the implementation if we don't already have one.
|
|
365
|
+
const ResponseImpl = typeof Response === "undefined" ? require("node-fetch").Response : Response;
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Helper for creating a text-based mock response.
|
|
369
|
+
*/
|
|
370
|
+
const textResponse = (text, statusCode = 200) => ({
|
|
371
|
+
type: "text",
|
|
372
|
+
text,
|
|
373
|
+
statusCode
|
|
374
|
+
});
|
|
375
|
+
/**
|
|
376
|
+
* Helper for creating a rejected mock response.
|
|
377
|
+
*/
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
const rejectResponse = error => ({
|
|
381
|
+
type: "reject",
|
|
382
|
+
error
|
|
383
|
+
});
|
|
384
|
+
/**
|
|
385
|
+
* Helpers to define mock responses for mocked requests.
|
|
386
|
+
*/
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
const RespondWith = Object.freeze({
|
|
390
|
+
/**
|
|
391
|
+
* Response with text body and status code.
|
|
392
|
+
* Status code defaults to 200.
|
|
393
|
+
*/
|
|
394
|
+
text: (text, statusCode = 200) => textResponse(text, statusCode),
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Response with JSON body and status code 200.
|
|
398
|
+
*/
|
|
399
|
+
json: json => textResponse(() => JSON.stringify(json)),
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Response with GraphQL data JSON body and status code 200.
|
|
403
|
+
*/
|
|
404
|
+
graphQLData: data => textResponse(() => JSON.stringify({
|
|
405
|
+
data
|
|
406
|
+
})),
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Response with body that will not parse as JSON and status code 200.
|
|
410
|
+
*/
|
|
411
|
+
unparseableBody: () => textResponse("INVALID JSON"),
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Rejects with an AbortError to simulate an aborted request.
|
|
415
|
+
*/
|
|
416
|
+
abortedRequest: () => rejectResponse(() => {
|
|
417
|
+
const abortError = new Error("Mock request aborted");
|
|
418
|
+
abortError.name = "AbortError";
|
|
419
|
+
return abortError;
|
|
420
|
+
}),
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Rejects with the given error.
|
|
424
|
+
*/
|
|
425
|
+
reject: error => rejectResponse(error),
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* A non-200 status code with empty text body.
|
|
429
|
+
* Equivalent to calling `ResponseWith.text("", statusCode)`.
|
|
430
|
+
*/
|
|
431
|
+
errorStatusCode: statusCode => {
|
|
432
|
+
if (statusCode < 300) {
|
|
433
|
+
throw new Error(`${statusCode} is not a valid error status code`);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return textResponse("{}", statusCode);
|
|
437
|
+
},
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Response body that is valid JSON but not a valid GraphQL response.
|
|
441
|
+
*/
|
|
442
|
+
nonGraphQLBody: () => textResponse(() => JSON.stringify({
|
|
443
|
+
valid: "json",
|
|
444
|
+
that: "is not a valid graphql response"
|
|
445
|
+
})),
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Response that is a GraphQL errors response with status code 200.
|
|
449
|
+
*/
|
|
450
|
+
graphQLErrors: errorMessages => textResponse(() => JSON.stringify({
|
|
451
|
+
errors: errorMessages.map(e => ({
|
|
452
|
+
message: e
|
|
453
|
+
}))
|
|
454
|
+
}))
|
|
455
|
+
});
|
|
456
|
+
/**
|
|
457
|
+
* Turns a MockResponse value to an actual Response that represents the mock.
|
|
458
|
+
*/
|
|
459
|
+
|
|
460
|
+
const makeMockResponse = response => {
|
|
461
|
+
switch (response.type) {
|
|
462
|
+
case "text":
|
|
463
|
+
const text = typeof response.text === "function" ? response.text() : response.text;
|
|
464
|
+
return Promise.resolve(new ResponseImpl(text, {
|
|
465
|
+
status: response.statusCode
|
|
466
|
+
}));
|
|
467
|
+
|
|
468
|
+
case "reject":
|
|
469
|
+
const error = response.error instanceof Error ? response.error : response.error();
|
|
470
|
+
return Promise.reject(error);
|
|
471
|
+
|
|
472
|
+
default:
|
|
473
|
+
throw new Error(`Unknown response type: ${response.type}`);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Get the URL from the given RequestInfo.
|
|
479
|
+
*
|
|
480
|
+
* Since we could be running in Node or in JSDOM, we don't check instance
|
|
481
|
+
* types, but just use a heuristic so that this works without knowing what
|
|
482
|
+
* was polyfilling things.
|
|
483
|
+
*/
|
|
484
|
+
const getHref = input => {
|
|
485
|
+
if (typeof input === "string") {
|
|
486
|
+
return input;
|
|
487
|
+
} else if (typeof input.url === "string") {
|
|
488
|
+
return input.url;
|
|
489
|
+
} else if (typeof input.href === "string") {
|
|
490
|
+
return input.href;
|
|
491
|
+
} else {
|
|
492
|
+
throw new Error(`Unsupported input type`);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
/**
|
|
496
|
+
* Determines if a given fetch invocation matches the given mock.
|
|
497
|
+
*/
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
const fetchRequestMatchesMock = (mock, input, init) => {
|
|
501
|
+
// Currently, we only match on the input portion.
|
|
502
|
+
// This can be a Request, a URL, or a string.
|
|
503
|
+
const href = getHref(input); // Our mock operation is either a string for an exact match, or a regex.
|
|
504
|
+
|
|
505
|
+
if (typeof mock === "string") {
|
|
506
|
+
return href === mock;
|
|
507
|
+
} else if (mock instanceof RegExp) {
|
|
508
|
+
return mock.test(href);
|
|
509
|
+
} else {
|
|
510
|
+
throw new Error(`Unsupported mock operation: ${JSON.stringify(mock)}`);
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* A generic mock request function for using when mocking fetch or gqlFetch.
|
|
516
|
+
*/
|
|
517
|
+
const mockRequester = (operationMatcher, operationToString) => {
|
|
518
|
+
// We want this to work in jest and in fixtures to make life easy for folks.
|
|
519
|
+
// This is the array of mocked operations that we will traverse and
|
|
520
|
+
// manipulate.
|
|
521
|
+
const mocks = []; // What we return has to be a drop in for the fetch function that is
|
|
522
|
+
// provided to `GqlRouter` which is how folks will then use this mock.
|
|
523
|
+
|
|
524
|
+
const mockFn = (...args) => {
|
|
525
|
+
// Iterate our mocked operations and find the first one that matches.
|
|
526
|
+
for (const mock of mocks) {
|
|
527
|
+
if (mock.onceOnly && mock.used) {
|
|
528
|
+
// This is a once-only mock and it has been used, so skip it.
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (operationMatcher.apply(void 0, [mock.operation].concat(args))) {
|
|
533
|
+
mock.used = true;
|
|
534
|
+
return mock.response();
|
|
535
|
+
}
|
|
536
|
+
} // Default is to reject with some helpful info on what request
|
|
537
|
+
// we rejected.
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
return Promise.reject(new Error(`No matching mock response found for request:
|
|
541
|
+
${operationToString.apply(void 0, args)}`));
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
const addMockedOperation = (operation, response, onceOnly) => {
|
|
545
|
+
const mockResponse = () => makeMockResponse(response);
|
|
546
|
+
|
|
547
|
+
mocks.push({
|
|
548
|
+
operation,
|
|
549
|
+
response: mockResponse,
|
|
550
|
+
onceOnly,
|
|
551
|
+
used: false
|
|
552
|
+
});
|
|
553
|
+
return mockFn;
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
mockFn.mockOperation = (operation, response) => addMockedOperation(operation, response, false);
|
|
557
|
+
|
|
558
|
+
mockFn.mockOperationOnce = (operation, response) => addMockedOperation(operation, response, true);
|
|
559
|
+
|
|
560
|
+
return mockFn;
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* A mock for the fetch function passed to GqlRouter.
|
|
565
|
+
*/
|
|
566
|
+
const mockFetch = () => mockRequester(fetchRequestMatchesMock, (input, init) => `Input: ${typeof input === "string" ? input : JSON.stringify(input, null, 2)}
|
|
567
|
+
Options: ${init == null ? "None" : JSON.stringify(init, null, 2)}`);
|
|
568
|
+
|
|
360
569
|
const safeHasOwnProperty = (obj, prop) => // Flow really shouldn't be raising this error here.
|
|
361
570
|
// $FlowFixMe[method-unbinding]
|
|
362
571
|
Object.prototype.hasOwnProperty.call(obj, prop); // TODO(somewhatabstract, FEI-4268): use a third-party library to do this and
|
|
@@ -426,143 +635,11 @@ const gqlRequestMatchesMock = (mock, operation, variables, context) => {
|
|
|
426
635
|
return true;
|
|
427
636
|
};
|
|
428
637
|
|
|
429
|
-
/**
|
|
430
|
-
* Helpers to define rejection states for mocking GQL requests.
|
|
431
|
-
*/
|
|
432
|
-
const RespondWith = Object.freeze({
|
|
433
|
-
data: data => ({
|
|
434
|
-
type: "data",
|
|
435
|
-
data
|
|
436
|
-
}),
|
|
437
|
-
unparseableBody: () => ({
|
|
438
|
-
type: "parse"
|
|
439
|
-
}),
|
|
440
|
-
abortedRequest: () => ({
|
|
441
|
-
type: "abort"
|
|
442
|
-
}),
|
|
443
|
-
errorStatusCode: statusCode => {
|
|
444
|
-
if (statusCode < 300) {
|
|
445
|
-
throw new Error(`${statusCode} is not a valid error status code`);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
return {
|
|
449
|
-
type: "status",
|
|
450
|
-
statusCode
|
|
451
|
-
};
|
|
452
|
-
},
|
|
453
|
-
nonGraphQLBody: () => ({
|
|
454
|
-
type: "invalid"
|
|
455
|
-
}),
|
|
456
|
-
graphQLErrors: errorMessages => ({
|
|
457
|
-
type: "graphql",
|
|
458
|
-
errors: errorMessages
|
|
459
|
-
})
|
|
460
|
-
});
|
|
461
|
-
/**
|
|
462
|
-
* Turns an ErrorResponse value in an actual Response that will invoke
|
|
463
|
-
* that error.
|
|
464
|
-
*/
|
|
465
|
-
|
|
466
|
-
const makeGqlMockResponse = response => {
|
|
467
|
-
switch (response.type) {
|
|
468
|
-
case "data":
|
|
469
|
-
return Promise.resolve({
|
|
470
|
-
status: 200,
|
|
471
|
-
text: () => Promise.resolve(JSON.stringify({
|
|
472
|
-
data: response.data
|
|
473
|
-
}))
|
|
474
|
-
});
|
|
475
|
-
|
|
476
|
-
case "parse":
|
|
477
|
-
return Promise.resolve({
|
|
478
|
-
status: 200,
|
|
479
|
-
text: () => Promise.resolve("INVALID JSON")
|
|
480
|
-
});
|
|
481
|
-
|
|
482
|
-
case "abort":
|
|
483
|
-
const abortError = new Error("Mock request aborted");
|
|
484
|
-
abortError.name = "AbortError";
|
|
485
|
-
return Promise.reject(abortError);
|
|
486
|
-
|
|
487
|
-
case "status":
|
|
488
|
-
return Promise.resolve({
|
|
489
|
-
status: response.statusCode,
|
|
490
|
-
text: () => Promise.resolve(JSON.stringify({}))
|
|
491
|
-
});
|
|
492
|
-
|
|
493
|
-
case "invalid":
|
|
494
|
-
return Promise.resolve({
|
|
495
|
-
status: 200,
|
|
496
|
-
text: () => Promise.resolve(JSON.stringify({
|
|
497
|
-
valid: "json",
|
|
498
|
-
that: "is not a valid graphql response"
|
|
499
|
-
}))
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
case "graphql":
|
|
503
|
-
return Promise.resolve({
|
|
504
|
-
status: 200,
|
|
505
|
-
text: () => Promise.resolve(JSON.stringify({
|
|
506
|
-
errors: response.errors.map(e => ({
|
|
507
|
-
message: e
|
|
508
|
-
}))
|
|
509
|
-
}))
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
default:
|
|
513
|
-
throw new Error(`Unknown response type: ${response.type}`);
|
|
514
|
-
}
|
|
515
|
-
};
|
|
516
|
-
|
|
517
638
|
/**
|
|
518
639
|
* A mock for the fetch function passed to GqlRouter.
|
|
519
640
|
*/
|
|
520
|
-
const mockGqlFetch = () => {
|
|
521
|
-
// We want this to work in jest and in fixtures to make life easy for folks.
|
|
522
|
-
// This is the array of mocked operations that we will traverse and
|
|
523
|
-
// manipulate.
|
|
524
|
-
const mocks = []; // What we return has to be a drop in for the fetch function that is
|
|
525
|
-
// provided to `GqlRouter` which is how folks will then use this mock.
|
|
526
|
-
|
|
527
|
-
const gqlFetchMock = (operation, variables, context) => {
|
|
528
|
-
// Iterate our mocked operations and find the first one that matches.
|
|
529
|
-
for (const mock of mocks) {
|
|
530
|
-
if (mock.onceOnly && mock.used) {
|
|
531
|
-
// This is a once-only mock and it has been used, so skip it.
|
|
532
|
-
continue;
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
if (gqlRequestMatchesMock(mock.operation, operation, variables, context)) {
|
|
536
|
-
mock.used = true;
|
|
537
|
-
return mock.response();
|
|
538
|
-
}
|
|
539
|
-
} // Default is to reject with some helpful info on what request
|
|
540
|
-
// we rejected.
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
return Promise.reject(new Error(`No matching GraphQL mock response found for request:
|
|
544
|
-
Operation: ${operation.type} ${operation.id}
|
|
641
|
+
const mockGqlFetch = () => mockRequester(gqlRequestMatchesMock, (operation, variables, context) => `Operation: ${operation.type} ${operation.id}
|
|
545
642
|
Variables: ${variables == null ? "None" : JSON.stringify(variables, null, 2)}
|
|
546
|
-
Context: ${JSON.stringify(context, null, 2)}`)
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
const addMockedOperation = (operation, response, onceOnly) => {
|
|
550
|
-
const mockResponse = () => makeGqlMockResponse(response);
|
|
551
|
-
|
|
552
|
-
mocks.push({
|
|
553
|
-
operation,
|
|
554
|
-
response: mockResponse,
|
|
555
|
-
onceOnly,
|
|
556
|
-
used: false
|
|
557
|
-
});
|
|
558
|
-
return gqlFetchMock;
|
|
559
|
-
};
|
|
560
|
-
|
|
561
|
-
gqlFetchMock.mockOperation = (operation, response) => addMockedOperation(operation, response, false);
|
|
562
|
-
|
|
563
|
-
gqlFetchMock.mockOperationOnce = (operation, response) => addMockedOperation(operation, response, true);
|
|
564
|
-
|
|
565
|
-
return gqlFetchMock;
|
|
566
|
-
};
|
|
643
|
+
Context: ${JSON.stringify(context, null, 2)}`);
|
|
567
644
|
|
|
568
|
-
export { RespondWith, adapters, fixtures, mockGqlFetch, setup as setupFixtures };
|
|
645
|
+
export { RespondWith, adapters, fixtures, mockFetch, mockGqlFetch, setup as setupFixtures };
|