Skip to content

Commit e956f78

Browse files
Xquikomermecitoglu
authored andcommitted
test: cover query parameter coercion
1 parent 0d92a83 commit e956f78

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

‎src/core/definer.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,46 @@ describe("defineRoute", () => {
4343
expect(response.status).toBe(200);
4444
});
4545

46+
it("should coerce and validate query parameters via Zod schema", async () => {
47+
const queryParams = z.object({
48+
query: z.string().min(1),
49+
sort: z.enum(["latest", "popular"]).optional(),
50+
limit: z.coerce.number().int().min(1).max(100).optional(),
51+
});
52+
const route = defineRoute({
53+
operationId: "listItems",
54+
method: "GET",
55+
summary: "List items",
56+
description: "List items with validated query parameters",
57+
tags: ["Items"],
58+
queryParams,
59+
action: mockAction,
60+
responses: {
61+
200: { description: "Items" },
62+
},
63+
});
64+
const request = {
65+
...mockRequest,
66+
url: "https://example.com/api/items?query=openapi&sort=latest&limit=20",
67+
};
68+
69+
mockAction.mockResolvedValue(new Response("Success"));
70+
71+
const response = await route.GET(request as unknown as Request, { params: Promise.resolve({}) });
72+
73+
expect(mockAction).toHaveBeenCalledWith({
74+
pathParams: null,
75+
queryParams: {
76+
query: "openapi",
77+
sort: "latest",
78+
limit: 20,
79+
},
80+
body: null,
81+
}, request);
82+
expect(response).toBeInstanceOf(Response);
83+
expect(response.status).toBe(200);
84+
});
85+
4686
it("should handle a POST route with pathParams and body", async () => {
4787
const pathSchema = z.object({ id: z.string() });
4888
const bodySchema = z.object({ name: z.string() });

0 commit comments

Comments
 (0)