forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryProvider.ts
More file actions
134 lines (120 loc) · 4.45 KB
/
Copy pathqueryProvider.ts
File metadata and controls
134 lines (120 loc) · 4.45 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { inspect } from 'util';
import { Octokit } from '@octokit/rest';
import {
ApolloQueryResult,
QueryOptions,
DocumentNode,
OperationVariables,
MutationOptions,
FetchResult,
} from 'apollo-boost';
import { SinonSandbox, SinonStubbedInstance } from 'sinon';
import equals from 'fast-deep-equal';
interface RecordedQueryResult<T> {
variables?: OperationVariables;
result: ApolloQueryResult<any>;
}
interface RecordedMutationResult<T> {
variables?: OperationVariables;
result: FetchResult<any>;
}
export class QueryProvider {
private _graphqlQueryResponses: Map<DocumentNode, RecordedQueryResult<any>[]>;
private _graphqlMutationResponses: Map<DocumentNode, RecordedMutationResult<any>[]>;
private _octokit: SinonStubbedInstance<Octokit>;
constructor(private _sinon: SinonSandbox) {
this._graphqlQueryResponses = new Map();
this._graphqlMutationResponses = new Map();
// Create the stubbed Octokit instance indirectly like this, rather than using `this._sinon.createStubbedInstance()`,
// because the exported Octokit function is actually a bound constructor method. `Object.getPrototypeOf(Octokit)` returns
// the correct prototype, but `Octokit.prototype` does not.
this._octokit = this._sinon.stub(Object.create(Object.getPrototypeOf(Octokit)));
}
get octokit(): Octokit {
// Cast through "any" because SinonStubbedInstance<Octokit> does not properly map the type of the
// overloaded "authenticate" method.
return (this._octokit as any) as Octokit;
}
expectGraphQLQuery<T>(q: QueryOptions, result: ApolloQueryResult<T>) {
if (!q.query) {
throw new Error('Empty GraphQL query used in expectation. Is the GraphQL loader configured properly?');
}
const cannedResponse: RecordedQueryResult<T> = { variables: q.variables, result };
const cannedResponses = this._graphqlQueryResponses.get(q.query) || [];
if (cannedResponses.length === 0) {
this._graphqlQueryResponses.set(q.query, [cannedResponse]);
} else {
cannedResponses.push(cannedResponse);
}
}
expectGraphQLMutation<T>(m: MutationOptions, result: FetchResult<T>) {
const cannedResponse: RecordedMutationResult<T> = { variables: m.variables, result };
const cannedResponses = this._graphqlMutationResponses.get(m.mutation) || [];
if (cannedResponses.length === 0) {
this._graphqlMutationResponses.set(m.mutation, [cannedResponse]);
} else {
cannedResponses.push(cannedResponse);
}
}
expectOctokitRequest<R>(accessorPath: string[], args: any[], response: R) {
let currentStub: SinonStubbedInstance<any> = this._octokit;
accessorPath.forEach((accessor, i) => {
let nextStub = currentStub[accessor];
if (nextStub === undefined) {
nextStub =
i < accessorPath.length - 1
? {}
: this._sinon.stub().callsFake((...variables) => {
throw new Error(
`Unexpected octokit query: ${accessorPath.join('.')}(${variables
.map(v => inspect(v))
.join(', ')})`,
);
});
currentStub[accessor] = nextStub;
}
currentStub = nextStub;
});
currentStub.withArgs(...args).resolves({ data: response });
}
emulateGraphQLQuery<T>(q: QueryOptions): ApolloQueryResult<T> {
const cannedResponses = this._graphqlQueryResponses.get(q.query) || [];
const cannedResponse = cannedResponses.find(
each =>
!!each.variables &&
Object.keys(each.variables).every(key => each.variables![key] === q.variables![key]),
);
if (cannedResponse) {
return cannedResponse.result;
} else {
if (cannedResponses.length > 0) {
let message = 'Variables did not match any expected queries:\n';
for (const { variables } of cannedResponses) {
message += ` ${inspect(variables, { depth: 3 })}\n`;
}
console.error(message);
}
throw new Error(`Unexpected GraphQL query: ${q}`);
}
}
emulateGraphQLMutation<T>(m: MutationOptions<T, OperationVariables>): FetchResult<T> {
const cannedResponses = this._graphqlMutationResponses.get(m.mutation) || [];
const cannedResponse = cannedResponses.find(
each =>
!!each.variables &&
Object.keys(each.variables).every(key => equals(each.variables![key], m.variables![key])),
);
if (cannedResponse) {
return cannedResponse.result;
} else {
if (cannedResponses.length > 0) {
let message = 'Variables did not match any expected queries:\n';
for (const { variables } of cannedResponses) {
message += ` ${inspect(variables, { depth: 3 })}\n`;
}
console.error(message);
}
throw new Error(`Unexpected GraphQL mutation: ${m}`);
}
}
}