forked from PipedreamHQ/eslint-plugin-pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.test.js
More file actions
235 lines (225 loc) · 6.55 KB
/
Copy pathrules.test.js
File metadata and controls
235 lines (225 loc) · 6.55 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const RuleTester = require("eslint").RuleTester;
const { rules } = require("../index");
const {
valid,
requiredPropertyKeyMissing,
requiredPropertyNameMissing,
requiredPropertyDescriptionMissing,
requiredPropertyVersionMissing,
requiredPropertyTypeMissing,
optionalPropWithoutDefaultValue,
missingPropsLabel,
missingPropsLabelTimer,
missingPropsLabelHttp,
missingPropsLabelDir,
missingPropsDescription,
missingPropsDescriptionTimer,
missingPropsDescriptionHttp,
missingPropsDescriptionDir,
badSourceName,
badSourceDescription,
tsVersion,
validActionWithAnnotations,
actionMissingAnnotations,
actionMissingAnnotationKey,
actionInvalidAnnotations,
} = require("./components");
const ruleTester = new RuleTester({
parserOptions: {
"ecmaVersion": 12,
"sourceType": "module",
},
});
function convertObjectToCJSExportString(obj) {
return `module.exports = ${JSON.stringify(obj)}`;
}
function convertObjectToESMExportString(obj) {
return `export default ${JSON.stringify(obj)}`;
}
function withPrecedingStatement(code) {
return `
import foo from "bar";
${code}
`;
}
function makeComponentTestCase ({
ruleName,
name = `${ruleName}-test`,
validComponents = [
valid,
],
invalidComponent,
errorMessage,
}) {
return {
name,
ruleName,
validComponents,
invalidComponent,
errorMessage,
};
}
const componentTestConfigs = [
{
ruleName: "required-properties-key",
invalidComponent: requiredPropertyKeyMissing,
errorMessage: "Components must export a key property. See https://pipedream.com/docs/components/guidelines/#required-metadata",
},
{
ruleName: "required-properties-name",
invalidComponent: requiredPropertyNameMissing,
errorMessage: "Components must export a name property. See https://pipedream.com/docs/components/guidelines/#required-metadata",
},
{
ruleName: "required-properties-description",
invalidComponent: requiredPropertyDescriptionMissing,
errorMessage: "Components must export a description property. See https://pipedream.com/docs/components/guidelines/#required-metadata",
},
{
ruleName: "required-properties-version",
invalidComponent: requiredPropertyVersionMissing,
errorMessage: "Components must export a version property. See https://pipedream.com/docs/components/guidelines/#required-metadata",
},
{
ruleName: "required-properties-type",
invalidComponent: requiredPropertyTypeMissing,
errorMessage: "Components must export a type property (\"source\" or \"action\")",
},
{
ruleName: "default-value-required-for-optional-props",
invalidComponent: optionalPropWithoutDefaultValue,
errorMessage: "Component prop test is marked \"optional\", so it may need a \"default\" property. See https://pipedream.com/docs/components/guidelines/#default-values",
},
{
ruleName: "props-label",
validComponents: [
missingPropsLabelTimer,
missingPropsLabelHttp,
missingPropsLabelDir,
],
invalidComponent: missingPropsLabel,
errorMessage: "Component prop test must have a label. See https://pipedream.com/docs/components/guidelines/#props",
},
{
ruleName: "props-description",
validComponents: [
missingPropsDescriptionTimer,
missingPropsDescriptionHttp,
missingPropsDescriptionDir,
],
invalidComponent: missingPropsDescription,
errorMessage: "Component prop test must have a description. See https://pipedream.com/docs/components/guidelines/#props",
},
{
ruleName: "source-name",
invalidComponent: badSourceName,
errorMessage: "Source names should start with \"New\". See https://pipedream.com/docs/components/guidelines/#source-name",
},
{
ruleName: "source-description",
invalidComponent: badSourceDescription,
errorMessage: "Source descriptions should start with \"Emit new\". See https://pipedream.com/docs/components/guidelines/#source-description",
},
{
name: "ts-version-test",
ruleName: "no-ts-version",
invalidComponent: tsVersion,
errorMessage: "{{ts}} macro should be removed before committing",
},
{
name: "action-annotations-missing",
ruleName: "action-annotations",
validComponents: [
validActionWithAnnotations,
],
invalidComponent: actionMissingAnnotations,
errorMessage: "Action component is missing required 'annotations' object",
},
{
name: "action-annotations-missing-key",
ruleName: "action-annotations",
validComponents: [
validActionWithAnnotations,
],
invalidComponent: actionMissingAnnotationKey,
errorMessage: "Property 'annotations' is missing required key: 'readOnlyHint'",
},
{
name: "action-annotations-invalid",
ruleName: "action-annotations",
validComponents: [
validActionWithAnnotations,
],
invalidComponent: actionInvalidAnnotations,
errorMessage: "Property 'annotations' must be an object expression",
},
];
const componentTestCases = componentTestConfigs.map(makeComponentTestCase);
// Run `ruleTester.run` on each test case
componentTestCases.forEach((testCase) => {
const {
name,
ruleName,
validComponents,
invalidComponent,
errorMessage,
} = testCase;
ruleTester.run(name, rules[ruleName], {
valid: validComponents.map((component) => ([
{
code: convertObjectToCJSExportString(component),
},
{
code: convertObjectToESMExportString(component),
},
])).flat(),
invalid: [
{
code: convertObjectToCJSExportString(invalidComponent),
errors: [
{
message: errorMessage,
},
],
},
{
code: convertObjectToESMExportString(invalidComponent),
errors: [
{
message: errorMessage,
},
],
},
],
});
});
RuleTester.describe("On ESM export default with preceding statements", () => {
// Run each test case on ESM default export components with preceding statements
// (lines above the `export default` declaration)
componentTestCases.forEach((testCase) => {
const {
name,
ruleName,
validComponent,
invalidComponent,
errorMessage,
} = testCase;
ruleTester.run(name, rules[ruleName], {
valid: [
{
code: withPrecedingStatement(convertObjectToESMExportString(validComponent)),
},
],
invalid: [
{
code: withPrecedingStatement(convertObjectToESMExportString(invalidComponent)),
errors: [
{
message: errorMessage,
},
],
},
],
});
});
});