-
Notifications
You must be signed in to change notification settings - Fork 242
/
toCallService.ts
103 lines (94 loc) 路 2.57 KB
/
toCallService.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
import { QueryPlan, PlanNode } from '@apollo/query-planner';
import { astSerializer, queryPlanSerializer } from '../snapshotSerializers';
const prettyFormat = require('pretty-format');
declare global {
namespace jest {
interface Matchers<R> {
toCallService(service: string): R;
}
}
}
// function printNode(node: ExecutionNode) {
// return prettyFormat(
// { nodes: [node], kind: 'QueryPlan' },
// {
// plugins: [queryPlanSerializer, astSerializer],
// },
// );
// }
const lineEndRegex = /^/gm;
function indentString(string: string, count = 2) {
if (!string) return string;
return string.replace(lineEndRegex, ' '.repeat(count));
}
function toCallService(
this: jest.MatcherUtils,
queryPlan: QueryPlan,
service: string,
): { message(): string; pass: boolean } {
// const receivedString = print(received);
// const expectedString = print(expected);
const printReceived = (string: string) =>
this.utils.RECEIVED_COLOR(indentString(string));
const printExpected = (string: string) =>
this.utils.EXPECTED_COLOR(indentString(string));
let pass = false;
// let initialServiceCall = null;
// recurse the node, find first match of service name, return
function walkExecutionNode(node?: PlanNode) {
if (!node) return;
if (node.kind === 'Fetch' && node.serviceName === service) {
pass = true;
// initialServiceCall = node;
return;
}
switch (node.kind) {
case 'Flatten':
walkExecutionNode(node.node);
break;
case 'Parallel':
case 'Sequence':
node.nodes.forEach(walkExecutionNode);
break;
default:
return;
}
}
walkExecutionNode(queryPlan.node);
const message = pass
? () =>
this.utils.matcherHint('.not.toCallService') +
'\n\n' +
`Expected query plan to not call service:\n` +
printExpected(service) +
'\n' +
`Received:\n` +
// FIXME print just the node
printReceived(
prettyFormat(queryPlan, {
plugins: [queryPlanSerializer, astSerializer],
}),
)
: () => {
return (
this.utils.matcherHint('.toCallService') +
'\n\n' +
`Expected query plan to call service:\n` +
printExpected(service) +
'\n' +
`Received query plan:\n` +
printReceived(
prettyFormat(queryPlan, {
plugins: [queryPlanSerializer, astSerializer],
}),
)
);
};
return {
message,
pass,
};
}
expect.extend({
toCallService,
});