Skip to content

Commit 5a8547a

Browse files
committed
test: bundlePaths with predefined schemas
1 parent 831fa85 commit 5a8547a

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

src/core/route.test.ts

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from "@jest/globals";
2+
import { type ZodType, z } from "zod";
23
import { bundlePaths, createRouteRecord } from "./route";
34
import type { OperationObject } from "@omer-x/openapi-types/operation";
4-
import type { ZodType } from "zod";
55

66
describe("createRouteRecord", () => {
77
it("should create a route record with the correct method, path, and apiData", () => {
@@ -103,4 +103,129 @@ describe("bundlePaths", () => {
103103
},
104104
});
105105
});
106+
107+
it("should create references for the responses that are identical to pre-defined schemas", () => {
108+
const source = [
109+
{
110+
method: "get",
111+
path: "/user",
112+
apiData: {
113+
summary: "Get user",
114+
responses: {
115+
200: {
116+
description: "Successful response",
117+
content: {
118+
"application/json": {
119+
schema: {
120+
type: "object",
121+
properties: {
122+
name: {
123+
type: "string",
124+
},
125+
email: {
126+
type: "string",
127+
format: "email",
128+
},
129+
},
130+
required: ["name", "email"],
131+
additionalProperties: false,
132+
},
133+
},
134+
},
135+
},
136+
},
137+
} as OperationObject,
138+
},
139+
];
140+
const storedSchemas: Record<string, ZodType> = {
141+
UserDTO: z.object({
142+
name: z.string(),
143+
email: z.string().email(),
144+
}),
145+
};
146+
147+
const result = bundlePaths(source, storedSchemas);
148+
149+
expect(result).toStrictEqual({
150+
"/user": {
151+
get: {
152+
parameters: undefined,
153+
requestBody: undefined,
154+
responses: {
155+
200: {
156+
content: {
157+
"application/json": {
158+
schema: {
159+
$ref: "#/components/schemas/UserDTO",
160+
},
161+
},
162+
},
163+
description: "Successful response",
164+
},
165+
},
166+
summary: "Get user",
167+
},
168+
},
169+
});
170+
});
171+
172+
it("should handle the cases where schema is undefined or referenced already", () => {
173+
const source = [
174+
{
175+
method: "get",
176+
path: "/user",
177+
apiData: {
178+
summary: "Get user",
179+
parameters: [
180+
{
181+
$ref: "#/components/parameters/QueryLimit",
182+
},
183+
],
184+
responses: {
185+
200: {
186+
description: "Successful response",
187+
content: {
188+
"application/json": {
189+
schema: undefined,
190+
},
191+
},
192+
},
193+
404: {
194+
$ref: "#/components/responses/NotFound",
195+
},
196+
},
197+
} as OperationObject,
198+
},
199+
];
200+
const storedSchemas: Record<string, ZodType> = {};
201+
202+
const result = bundlePaths(source, storedSchemas);
203+
204+
expect(result).toStrictEqual({
205+
"/user": {
206+
get: {
207+
parameters: [
208+
{
209+
$ref: "#/components/parameters/QueryLimit",
210+
},
211+
],
212+
requestBody: undefined,
213+
responses: {
214+
200: {
215+
content: {
216+
"application/json": {
217+
schema: undefined,
218+
},
219+
},
220+
description: "Successful response",
221+
},
222+
404: {
223+
$ref: "#/components/responses/NotFound",
224+
},
225+
},
226+
summary: "Get user",
227+
},
228+
},
229+
});
230+
});
106231
});

0 commit comments

Comments
 (0)