@khanacademy/wonder-blocks-testing 2.0.8 → 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 +22 -0
- package/dist/es/index.js +255 -140
- package/dist/index.js +304 -145
- 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/fixtures/__tests__/fixtures.test.js +66 -22
- package/src/fixtures/adapters/__tests__/adapter-group.test.js +24 -0
- package/src/fixtures/adapters/__tests__/adapter.test.js +6 -0
- package/src/fixtures/adapters/storybook.js +4 -1
- package/src/fixtures/fixtures.basic.stories.js +1 -1
- package/src/fixtures/fixtures.defaultwrapper.stories.js +1 -1
- package/src/fixtures/fixtures.js +47 -3
- package/src/fixtures/types.js +10 -1
- 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
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
export opaque type GqlMockResponse<TData> =
|
|
3
|
-
| {|
|
|
4
|
-
type: "data",
|
|
5
|
-
data: TData,
|
|
6
|
-
|}
|
|
7
|
-
| {|
|
|
8
|
-
type: "parse",
|
|
9
|
-
|}
|
|
10
|
-
| {|
|
|
11
|
-
type: "abort",
|
|
12
|
-
|}
|
|
13
|
-
| {|
|
|
14
|
-
type: "status",
|
|
15
|
-
statusCode: number,
|
|
16
|
-
|}
|
|
17
|
-
| {|
|
|
18
|
-
type: "invalid",
|
|
19
|
-
|}
|
|
20
|
-
| {|type: "graphql", errors: $ReadOnlyArray<string>|};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Helpers to define rejection states for mocking GQL requests.
|
|
24
|
-
*/
|
|
25
|
-
export const RespondWith = Object.freeze({
|
|
26
|
-
data: <TData>(data: TData): GqlMockResponse<TData> => ({
|
|
27
|
-
type: "data",
|
|
28
|
-
data,
|
|
29
|
-
}),
|
|
30
|
-
unparseableBody: (): GqlMockResponse<any> => ({type: "parse"}),
|
|
31
|
-
abortedRequest: (): GqlMockResponse<any> => ({type: "abort"}),
|
|
32
|
-
errorStatusCode: (statusCode: number): GqlMockResponse<any> => {
|
|
33
|
-
if (statusCode < 300) {
|
|
34
|
-
throw new Error(`${statusCode} is not a valid error status code`);
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
type: "status",
|
|
38
|
-
statusCode,
|
|
39
|
-
};
|
|
40
|
-
},
|
|
41
|
-
nonGraphQLBody: (): GqlMockResponse<any> => ({type: "invalid"}),
|
|
42
|
-
graphQLErrors: (
|
|
43
|
-
errorMessages: $ReadOnlyArray<string>,
|
|
44
|
-
): GqlMockResponse<any> => ({
|
|
45
|
-
type: "graphql",
|
|
46
|
-
errors: errorMessages,
|
|
47
|
-
}),
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Turns an ErrorResponse value in an actual Response that will invoke
|
|
52
|
-
* that error.
|
|
53
|
-
*/
|
|
54
|
-
export const makeGqlMockResponse = <TData>(
|
|
55
|
-
response: GqlMockResponse<TData>,
|
|
56
|
-
): Promise<Response> => {
|
|
57
|
-
switch (response.type) {
|
|
58
|
-
case "data":
|
|
59
|
-
return Promise.resolve(
|
|
60
|
-
({
|
|
61
|
-
status: 200,
|
|
62
|
-
text: () =>
|
|
63
|
-
Promise.resolve(
|
|
64
|
-
JSON.stringify({
|
|
65
|
-
data: response.data,
|
|
66
|
-
}),
|
|
67
|
-
),
|
|
68
|
-
}: any),
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
case "parse":
|
|
72
|
-
return Promise.resolve(
|
|
73
|
-
({
|
|
74
|
-
status: 200,
|
|
75
|
-
text: () => Promise.resolve("INVALID JSON"),
|
|
76
|
-
}: any),
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
case "abort":
|
|
80
|
-
const abortError = new Error("Mock request aborted");
|
|
81
|
-
abortError.name = "AbortError";
|
|
82
|
-
return Promise.reject(abortError);
|
|
83
|
-
|
|
84
|
-
case "status":
|
|
85
|
-
return Promise.resolve(
|
|
86
|
-
({
|
|
87
|
-
status: response.statusCode,
|
|
88
|
-
text: () => Promise.resolve(JSON.stringify({})),
|
|
89
|
-
}: any),
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
case "invalid":
|
|
93
|
-
return Promise.resolve(
|
|
94
|
-
({
|
|
95
|
-
status: 200,
|
|
96
|
-
text: () =>
|
|
97
|
-
Promise.resolve(
|
|
98
|
-
JSON.stringify({
|
|
99
|
-
valid: "json",
|
|
100
|
-
that: "is not a valid graphql response",
|
|
101
|
-
}),
|
|
102
|
-
),
|
|
103
|
-
}: any),
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
case "graphql":
|
|
107
|
-
return Promise.resolve(
|
|
108
|
-
({
|
|
109
|
-
status: 200,
|
|
110
|
-
text: () =>
|
|
111
|
-
Promise.resolve(
|
|
112
|
-
JSON.stringify({
|
|
113
|
-
errors: response.errors.map((e) => ({
|
|
114
|
-
message: e,
|
|
115
|
-
})),
|
|
116
|
-
}),
|
|
117
|
-
),
|
|
118
|
-
}: any),
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
default:
|
|
122
|
-
throw new Error(`Unknown response type: ${response.type}`);
|
|
123
|
-
}
|
|
124
|
-
};
|