adds testing for validations
parent
713f53acc4
commit
dfa227f1b6
|
@ -1,7 +1,6 @@
|
|||
import { createMocks } from "node-mocks-http";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
import { EventType } from "@calcom/prisma/client";
|
||||
|
||||
import handleEvent from "../pages/api/event-types/[id]";
|
||||
|
||||
|
@ -15,13 +14,38 @@ describe("/api/event-types/[id]", () => {
|
|||
const { req, res } = createMocks({
|
||||
method: "GET",
|
||||
query: {
|
||||
id: 1,
|
||||
id: "1",
|
||||
},
|
||||
});
|
||||
prisma.eventType.findUnique({ where: { id: 1 } }).then(async (data: EventType) => {
|
||||
await handleEvent(req, res);
|
||||
expect(res._getStatusCode()).toBe(200);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual({ event: data });
|
||||
});
|
||||
const event = await prisma.eventType.findUnique({ where: { id: 1 } });
|
||||
await handleEvent(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(200);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual({ data: event });
|
||||
});
|
||||
});
|
||||
|
||||
// This can never happen under our normal nextjs setup where query is always a string | string[].
|
||||
// But seemed a good example for testing an error validation
|
||||
describe("/api/event-types/[id] errors if query id is number, requires a string", () => {
|
||||
it("returns a message with the specified events", async () => {
|
||||
const { req, res } = createMocks({
|
||||
method: "GET",
|
||||
query: {
|
||||
id: 1, // passing query as a number, which should fail as nextjs will try to parse it as a string
|
||||
},
|
||||
});
|
||||
await handleEvent(req, res);
|
||||
|
||||
expect(res._getStatusCode()).toBe(400);
|
||||
expect(JSON.parse(res._getData())).toStrictEqual([
|
||||
{
|
||||
code: "invalid_type",
|
||||
expected: "string",
|
||||
received: "number",
|
||||
path: ["id"],
|
||||
message: "Expected string, received number",
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -28,15 +28,20 @@ type ResponseData = {
|
|||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
if (method === "GET") {
|
||||
const safe = await schema.safeParse(query);
|
||||
if (safe.success) {
|
||||
try {
|
||||
try {
|
||||
const safe = await schema.safeParse(query);
|
||||
// if (!safe.success) {
|
||||
// res.status(500).json({ error: safe.error.message });
|
||||
// }
|
||||
if (safe.success) {
|
||||
const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
|
||||
res.status(200).json({ data: event });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(400).json({ error: error });
|
||||
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ error: "Event type not found" });
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("catched", error);
|
||||
res.status(500).json({ error: error });
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
|
|
|
@ -14,32 +14,42 @@ const schema = z
|
|||
})
|
||||
.strict(); // Adding strict so that we can disallow passing in extra fields
|
||||
|
||||
type schema = z.infer<typeof schema>;
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "body",
|
||||
});
|
||||
|
||||
interface Body {
|
||||
title: string;
|
||||
slug: string;
|
||||
length: number;
|
||||
}
|
||||
// interface Body {
|
||||
// title: string;
|
||||
// slug: string;
|
||||
// length: number;
|
||||
// }
|
||||
type Data = {
|
||||
data?: EventType;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
type NextApiRequestWithBody = NextApiRequest & {
|
||||
body: Body;
|
||||
// body: Body;
|
||||
};
|
||||
|
||||
async function createEventType(req: NextApiRequestWithBody, res: NextApiResponse<Data>) {
|
||||
const { body, method }: { body: Body; method?: string } = req;
|
||||
const { body, method } = req;
|
||||
if (method === "POST") {
|
||||
schema.safeParse(body);
|
||||
const newEvent = await prisma.eventType.create({ data: body });
|
||||
res.status(201).json({ data: newEvent });
|
||||
const safe = schema.safeParse(body);
|
||||
if (safe.success && safe.data) {
|
||||
await prisma.eventType
|
||||
.create({
|
||||
data: { title: safe.data.title, slug: safe.data.slug, length: safe.data.length },
|
||||
})
|
||||
.then((event) => res.status(201).json({ data: event }))
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
res.status(400).json({ error: "Could not create event type" });
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only POST Method allowed" });
|
||||
|
|
|
@ -20,6 +20,6 @@
|
|||
"jsx": "preserve"
|
||||
},
|
||||
"include": [
|
||||
"./pages/api/*.ts"
|
||||
"./"
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue