-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcommon.test.ts
364 lines (318 loc) · 10.6 KB
/
common.test.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import {
type ClassDeclaration,
Project,
type SourceFile,
type VariableDeclaration,
} from "ts-morph";
import { factory } from "typescript";
import { describe, expect, test, vi } from "vitest";
import type { LimitedUserConfig } from "../src/cli.mts";
import {
BuildCommonTypeName,
buildQueriesOutputPath,
buildRequestsOutputPath,
capitalizeFirstLetter,
exists,
extractPropertiesFromObjectParam,
formatOptions,
getClassNameFromClassNode,
getClassesFromService,
getNameFromVariable,
getShortType,
getVariableArrowFunctionParameters,
lowercaseFirstLetter,
safeParseNumber,
} from "../src/common.mts";
describe("common", () => {
test("safeParseNumber", () => {
const parsed = safeParseNumber("123");
expect(parsed).toBe(123);
const nan = safeParseNumber("abc");
expect(nan).toBeNaN();
const trueVal = safeParseNumber(true);
expect(trueVal).toBe(1);
const falseVal = safeParseNumber(false);
expect(falseVal).toBe(0);
});
test("capitalizeFirstLetter", () => {
const capitalized = capitalizeFirstLetter("testTestTest");
expect(capitalized).toBe("TestTestTest");
});
test("lowercaseFirstLetter", () => {
const lowercased = lowercaseFirstLetter("TestTestTest");
expect(lowercased).toBe("testTestTest");
});
test("buildCommonTypeName", () => {
const name = "Name";
const result = BuildCommonTypeName(name);
expect(result.escapedText).toBe("Common.Name");
});
test("buildCommonTypeName - identifier", () => {
const name = factory.createIdentifier("Name");
const result = BuildCommonTypeName(name);
expect(result.escapedText).toBe("Common.Name");
});
describe("getShortType", () => {
test("linux", () => {
const type = 'import("/my/absolute/path").MyType';
const result = getShortType(type);
expect(result).toBe("MyType");
});
test("no import", () => {
const type = "MyType";
const result = getShortType(type);
expect(result).toBe("MyType");
});
test("windows", () => {
const type = 'import("D:/types.gen").MyType';
const result = getShortType(type);
expect(result).toBe("MyType");
});
test("number", () => {
const type = 'import("C:/Projekt/test_3.0/path").MyType';
const result = getShortType(type);
expect(result).toBe("MyType");
});
test("underscore", () => {
const type = 'import("C:/Projekt/test_one/path").MyType';
const result = getShortType(type);
expect(result).toBe("MyType");
});
test("dash", () => {
const type = 'import("C:/Projekt/test-one/path").MyType';
const result = getShortType(type);
expect(result).toBe("MyType");
});
test("json", () => {
const type =
'{ import1?: import("/path/to/import1").Import1; import2: import("/path/to/import2").Import2; import3: import("/path/to/import3").Import3; }';
const result = getShortType(type);
expect(result).toBe(
"{ import1?: Import1; import2: Import2; import3: Import3; }",
);
});
});
test("formatOptions - converts string boolean to boolean (false)", () => {
const options: LimitedUserConfig = {
input: "input",
output: "output",
// biome-ignore lint: test
debug: "false" as any,
pageParam: "page",
nextPageParam: "nextPage",
initialPageParam: "1",
};
const formatted = formatOptions(options);
expect(formatted.debug).toStrictEqual(false);
});
test("formatOptions - converts string boolean to boolean (true)", () => {
const options: LimitedUserConfig = {
input: "input",
output: "output",
// biome-ignore lint: test
debug: "true" as any,
pageParam: "page",
nextPageParam: "nextPage",
initialPageParam: "1",
};
const formatted = formatOptions(options);
expect(formatted.debug).toStrictEqual(true);
});
test("formatOptions - converts string boolean to boolean (undefined)", () => {
const options: LimitedUserConfig = {
input: "input",
output: "output",
pageParam: "page",
nextPageParam: "nextPage",
initialPageParam: "1",
};
const formatted = formatOptions(options);
expect(formatted.debug).toStrictEqual(undefined);
});
test("formatOptions - converts string number to number", () => {
const options: LimitedUserConfig = {
input: "input",
output: "output",
// biome-ignore lint: test
debug: "123" as any,
pageParam: "page",
nextPageParam: "nextPage",
initialPageParam: "1",
};
const formatted = formatOptions(options);
expect(formatted.debug).toStrictEqual(123);
});
test("formatOptions - leaves other values unchanged", () => {
const options: LimitedUserConfig = {
input: "input",
output: "output",
// biome-ignore lint: test
debug: "123" as any,
lint: "eslint",
pageParam: "page",
nextPageParam: "nextPage",
initialPageParam: "1",
};
const formatted = formatOptions(options);
expect(formatted.lint).toStrictEqual("eslint");
});
test("formatOptions - converts string number to number", () => {
const options: LimitedUserConfig = {
input: "input",
output: "output",
// biome-ignore lint: test
debug: Number.NaN as any,
pageParam: "page",
nextPageParam: "nextPage",
initialPageParam: "1",
};
const formatted = formatOptions(options);
expect(formatted.debug).toStrictEqual(Number.NaN);
});
test("formatOptions - handle boolean true", () => {
const options: LimitedUserConfig = {
input: "input",
output: "output",
debug: true,
pageParam: "page",
nextPageParam: "nextPage",
initialPageParam: "1",
};
const formatted = formatOptions(options);
expect(formatted.debug).toStrictEqual(true);
});
test("formatOptions - handle boolean false", () => {
const options: LimitedUserConfig = {
input: "input",
output: "output",
debug: false,
pageParam: "page",
nextPageParam: "nextPage",
initialPageParam: "1",
};
const formatted = formatOptions(options);
expect(formatted.debug).toStrictEqual(false);
});
test("getClassNameFromClassNode - get's name", () => {
const klass = {
getName: () => "Test",
} as unknown as ClassDeclaration;
const result = getClassNameFromClassNode(klass);
expect(result).toBe("Test");
});
test("getClassNameFromClassNode - no name", () => {
const klass = {
getName: () => undefined,
} as unknown as ClassDeclaration;
expect(() => getClassNameFromClassNode(klass)).toThrowError(
"Class name not found",
);
});
test("getClassesFromService - returns class name and class", () => {
const klass = {
getName: vi.fn(() => "Test"),
};
const node = {
getClasses: vi.fn(() => [klass]),
} as unknown as SourceFile;
const result = getClassesFromService(node);
expect(result).toStrictEqual([
{
className: "Test",
klass,
},
]);
});
test("getClassesFromService - no classes", () => {
const node = {
getClasses: vi.fn(() => []),
} as unknown as SourceFile;
expect(() => getClassesFromService(node)).toThrowError("No classes found");
});
test("getClassesFromService - no name", () => {
const klass = {
getName: vi.fn(() => undefined),
};
const node = {
getClasses: vi.fn(() => [klass]),
} as unknown as SourceFile;
expect(() => getClassesFromService(node)).toThrowError(
"Class name not found",
);
});
test("getNameFromMethod - get method name", () => {
const method = {
getName: vi.fn(() => "test"),
} as unknown as VariableDeclaration;
const result = getNameFromVariable(method);
expect(result).toBe("test");
});
test("getNameFromMethod - no method name", () => {
const method = {
getName: vi.fn(() => undefined),
} as unknown as VariableDeclaration;
expect(() => getNameFromVariable(method)).toThrowError(
"Variable name not found",
);
});
test("getVariableArrowFunctionParameters", async () => {
const source = "const test = (queryClient: QueryClient) => {}";
const project = new Project();
const sourceFile = project.createSourceFile("test.ts", source);
const method = sourceFile.getVariableDeclarations()[0];
const result = getVariableArrowFunctionParameters(method);
expect(result[0].getName()).toStrictEqual("queryClient");
});
test('getVariableArrowFunctionParameters - throw error "Initializer is not an arrow function"', async () => {
const source = 'const foo = "bar"';
const project = new Project();
const sourceFile = project.createSourceFile("test.ts", source);
const method = sourceFile.getVariableDeclarations()[0];
expect(() => getVariableArrowFunctionParameters(method)).toThrowError(
"Initializer is not an arrow function",
);
});
test('getVariableArrowFunctionParameters - throw error "Initializer not found"', async () => {
const source = "const foo";
const project = new Project();
const sourceFile = project.createSourceFile("test.ts", source);
const method = sourceFile.getVariableDeclarations()[0];
expect(() => getVariableArrowFunctionParameters(method)).toThrowError(
"Initializer not found",
);
});
test("extractPropertiesFromObjectParam", async () => {
const source = `
type Params = { limit: number; offset: number; };
const test = (params: Params) => {}
`;
const project = new Project();
const sourceFile = project.createSourceFile("test.ts", source);
const method = sourceFile.getVariableDeclarations()[0];
const params = getVariableArrowFunctionParameters(method);
const props = extractPropertiesFromObjectParam(params[0]);
expect(props.map((p) => p.name)).toStrictEqual(["limit", "offset"]);
});
test("exists - file exists", async () => {
const f = "tests/common.test.ts";
const result = await exists(f);
expect(result).toBe(true);
});
test("exists - file does not exist", async () => {
const f = "tests/nonexistent.test.ts";
const result = await exists(f);
expect(result).toBe(false);
});
test("buildRequestsOutputPath", async () => {
const output = "output";
const result = buildRequestsOutputPath(output);
// windows: output\requests | linux/mac: output/requests
expect(result).toMatch(/output[/\\]requests/);
});
test("buildQueriesOutputPath", async () => {
const output = "output";
const result = buildQueriesOutputPath(output);
// windows: output\queries | linux/mac: output/queries
expect(result).toMatch(/output[/\\]queries/);
});
});