-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy path_mocks.ts
106 lines (92 loc) · 3.11 KB
/
_mocks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { NetworkInterface, BatchedNetworkInterface, Request } from 'apollo-client/transport/networkInterface';
import { ApolloClient } from 'apollo-client';
import { ExecutionResult, DocumentNode } from 'graphql';
import { print } from 'graphql-tag/printer';
export function mockNetworkInterface(
...mockedResponses: MockedResponse[],
): NetworkInterface {
return new MockNetworkInterface(...mockedResponses);
}
export function mockBatchedNetworkInterface(
...mockedResponses: MockedResponse[],
): BatchedNetworkInterface {
return new MockBatchedNetworkInterface(...mockedResponses);
}
export interface ParsedRequest {
variables?: Object;
query?: DocumentNode;
debugName?: string;
}
export interface MockedResponse {
request: ParsedRequest;
result?: ExecutionResult;
error?: Error;
delay?: number;
}
export class MockNetworkInterface implements NetworkInterface {
private mockedResponsesByKey: { [key: string]: MockedResponse[] } = {};
constructor(...mockedResponses: MockedResponse[]) {
mockedResponses.forEach((mockedResponse) => {
this.addMockedReponse(mockedResponse);
});
}
public addMockedReponse(mockedResponse: MockedResponse) {
const key = requestToKey(mockedResponse.request);
let mockedResponses = this.mockedResponsesByKey[key];
if (!mockedResponses) {
mockedResponses = [];
this.mockedResponsesByKey[key] = mockedResponses;
}
mockedResponses.push(mockedResponse);
}
public query(request: Request) {
return new Promise((resolve, reject) => {
const parsedRequest: ParsedRequest = {
query: request.query,
variables: request.variables,
debugName: request.debugName,
};
const key = requestToKey(parsedRequest);
const responses = this.mockedResponsesByKey[key];
if (!responses || responses.length === 0) {
throw new Error(`No more mocked responses for the query: ${print(request.query)}, variables: ${JSON.stringify(request.variables)}`);
}
const { result, error, delay } = responses.shift();
if (!result && !error) {
throw new Error(`Mocked response should contain either result or error: ${key}`);
}
setTimeout(() => {
if (error) {
reject(error);
} else {
resolve(result);
}
}, delay ? delay : 0);
});
}
}
export class MockBatchedNetworkInterface
extends MockNetworkInterface implements BatchedNetworkInterface {
public batchQuery(requests: Request[]): Promise<ExecutionResult[]> {
const resultPromises: Promise<ExecutionResult>[] = [];
requests.forEach((request) => {
resultPromises.push(this.query(request));
});
return Promise.all(resultPromises);
}
}
function requestToKey(request: ParsedRequest): string {
const queryString = request.query && print(request.query);
return JSON.stringify({
variables: request.variables,
debugName: request.debugName,
query: queryString,
});
}
export function mockClient(...args): ApolloClient {
const networkInterface = mockNetworkInterface(...args);
return new ApolloClient({
networkInterface,
addTypename: false,
});
}