Skip to content

Commit 4affc3c

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

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-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,125 @@
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+
want: `export enum ApiPaths {
18+
GetApiV1User = "/api/v1/user"
19+
}`,
20+
},
21+
],
22+
[
23+
"basic with path parameter",
24+
{
25+
given: {
26+
"/api/v1/user/{user_id}": {
27+
parameters: [
28+
{
29+
name: "page",
30+
in: "query",
31+
schema: { type: "number" },
32+
description: "Page number.",
33+
},
34+
],
35+
get: {
36+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
37+
},
38+
},
39+
},
40+
want: `export enum ApiPaths {
41+
GetApiV1User = "/api/v1/user/:user_id"
42+
}`,
43+
},
44+
],
45+
[
46+
"with operationId",
47+
{
48+
given: {
49+
"/api/v1/user/{user_id}": {
50+
parameters: [
51+
{
52+
name: "page",
53+
in: "query",
54+
schema: { type: "number" },
55+
description: "Page number.",
56+
},
57+
],
58+
get: {
59+
operationId: "GetUserById",
60+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
61+
},
62+
},
63+
},
64+
want: `export enum ApiPaths {
65+
GetUserById = "/api/v1/user/:user_id"
66+
}`,
67+
},
68+
],
69+
[
70+
"with and without operationId",
71+
{
72+
given: {
73+
"/api/v1/user/{user_id}": {
74+
parameters: [
75+
{
76+
name: "page",
77+
in: "query",
78+
schema: { type: "number" },
79+
description: "Page number.",
80+
},
81+
],
82+
get: {
83+
operationId: "GetUserById",
84+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
85+
},
86+
post: {
87+
parameters: [{ name: "user_id", in: "path", description: "User ID." }],
88+
},
89+
},
90+
},
91+
want: `export enum ApiPaths {
92+
GetUserById = "/api/v1/user/:user_id",
93+
PostApiV1User = "/api/v1/user/:user_id"
94+
}`,
95+
},
96+
],
97+
[
98+
"invalid method",
99+
{
100+
given: {
101+
"/api/v1/user": {
102+
invalidMethod: {},
103+
},
104+
},
105+
want: `export enum ApiPaths {
106+
}`,
107+
},
108+
],
109+
];
110+
111+
for (const [testName, { given, want, ci }] of tests) {
112+
test.skipIf(ci?.skipIf)(
113+
testName,
114+
async () => {
115+
const result = astToString(makeApiPathsEnum(given));
116+
if (want instanceof URL) {
117+
expect(result).toMatchFileSnapshot(fileURLToPath(want));
118+
} else {
119+
expect(result).toBe(`${want}\n`);
120+
}
121+
},
122+
ci?.timeout,
123+
);
124+
}
125+
});

0 commit comments

Comments
 (0)