Skip to content

Commit e4ce821

Browse files
tests: adds tests for --make-paths-enum option and paths-enum.ts transformer
1 parent 253c23b commit e4ce821

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

packages/openapi-typescript/test/index.test.ts

+65
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,71 @@ export type operations = Record<string, never>;`,
694694
},
695695
},
696696
],
697+
[
698+
"$refs > path object & paths enum",
699+
{
700+
given: new URL("./fixtures/path-object-refs.yaml", import.meta.url),
701+
want: `export interface paths {
702+
"/get-item": {
703+
parameters: {
704+
query?: never;
705+
header?: never;
706+
path?: never;
707+
cookie?: never;
708+
};
709+
get: {
710+
parameters: {
711+
query?: never;
712+
header?: never;
713+
path?: never;
714+
cookie?: never;
715+
};
716+
requestBody?: never;
717+
responses: {
718+
/** @description OK */
719+
200: {
720+
headers: {
721+
[name: string]: unknown;
722+
};
723+
content: {
724+
"application/json": components["schemas"]["Item"];
725+
};
726+
};
727+
};
728+
};
729+
put?: never;
730+
post?: never;
731+
delete?: never;
732+
options?: never;
733+
head?: never;
734+
patch?: never;
735+
trace?: never;
736+
};
737+
}
738+
export type webhooks = Record<string, never>;
739+
export interface components {
740+
schemas: {
741+
Item: {
742+
id: string;
743+
name: string;
744+
};
745+
};
746+
responses: never;
747+
parameters: never;
748+
requestBodies: never;
749+
headers: never;
750+
pathItems: never;
751+
}
752+
export type $defs = Record<string, never>;
753+
export type operations = Record<string, never>;
754+
export enum ApiPaths {
755+
GetGetitem = "/get-item"
756+
}`,
757+
options: {
758+
makePathsEnum: true,
759+
}
760+
},
761+
],
697762
];
698763

699764
for (const [testName, { given, want, options, ci }] of tests) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { fileURLToPath } from "node:url";
2+
import { astToString } from "../../src/lib/ts.js";
3+
import makeApiPathsEnum from "../../src/transform/paths-enum.js";
4+
import type { GlobalContext } from "../../src/types.js";
5+
import { type TestCase } from "../test-helpers.js";
6+
7+
describe("transformPathsObjectToEnum", () => {
8+
const tests: TestCase<any, GlobalContext>[] = [
9+
[
10+
"basic",
11+
{
12+
given: {
13+
"/api/v1/user": {
14+
get: {
15+
},
16+
},
17+
},
18+
want: `export enum ApiPaths {
19+
GetApiV1User = "/api/v1/user"
20+
}`,
21+
},
22+
],
23+
[
24+
"basic with path parameter",
25+
{
26+
given: {
27+
"/api/v1/user/{user_id}": {
28+
parameters: [
29+
{
30+
name: "page",
31+
in: "query",
32+
schema: { type: "number" },
33+
description: "Page number.",
34+
},
35+
],
36+
get: {
37+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
38+
},
39+
},
40+
},
41+
want: `export enum ApiPaths {
42+
GetApiV1User = "/api/v1/user/:user_id"
43+
}`,
44+
},
45+
],
46+
[
47+
"with operationId",
48+
{
49+
given: {
50+
"/api/v1/user/{user_id}": {
51+
parameters: [
52+
{
53+
name: "page",
54+
in: "query",
55+
schema: { type: "number" },
56+
description: "Page number.",
57+
},
58+
],
59+
get: {
60+
operationId: "GetUserById",
61+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
62+
},
63+
},
64+
},
65+
want: `export enum ApiPaths {
66+
GetUserById = "/api/v1/user/:user_id"
67+
}`,
68+
},
69+
],
70+
[
71+
"with and without operationId",
72+
{
73+
given: {
74+
"/api/v1/user/{user_id}": {
75+
parameters: [
76+
{
77+
name: "page",
78+
in: "query",
79+
schema: { type: "number" },
80+
description: "Page number.",
81+
},
82+
],
83+
get: {
84+
operationId: "GetUserById",
85+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
86+
},
87+
post: {
88+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
89+
},
90+
},
91+
},
92+
want: `export enum ApiPaths {
93+
GetUserById = "/api/v1/user/:user_id",
94+
PostApiV1User = "/api/v1/user/:user_id"
95+
}`,
96+
},
97+
],
98+
[
99+
"invalid method",
100+
{
101+
given: {
102+
"/api/v1/user": {
103+
invalidMethod: {
104+
},
105+
},
106+
},
107+
want: `export enum ApiPaths {
108+
}`,
109+
},
110+
],
111+
];
112+
113+
for (const [testName, { given, want, ci }] of tests) {
114+
test.skipIf(ci?.skipIf)(
115+
testName,
116+
async () => {
117+
const result = astToString(makeApiPathsEnum(given));
118+
if (want instanceof URL) {
119+
expect(result).toMatchFileSnapshot(fileURLToPath(want));
120+
} else {
121+
expect(result).toBe(`${want}\n`);
122+
}
123+
},
124+
ci?.timeout,
125+
);
126+
}
127+
});

0 commit comments

Comments
 (0)