Skip to content

Scheduling infrastructure for absolutely everyone.

feat: tests all codepaths of event-type/[id]

Agusti Fernandez Pardo84ab74b46a05
diff --git a/__tests__/event-types.test.ts b/__tests__/event-types.test.ts
index d6eb7fe57f1..2a76abd3cd9 100644
--- a/__tests__/event-types.test.ts
+++ b/__tests__/event-types.test.ts
@@ -9,7 +9,7 @@ afterAll((done) => {
done();
});
-describe("/api/event-types/[id]", () => {
+describe("/api/event-types/[id] with valid id as string returns an event-type", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "GET",
@@ -49,3 +49,33 @@ describe("/api/event-types/[id] errors if query id is number, requires a string"
]);
});
});
+
+describe("/api/event-types/[id] an id not present in db like 0, throws 404 not found", () => {
+ it("returns a message with the specified events", async () => {
+ const { req, res } = createMocks({
+ method: "GET",
+ query: {
+ id: "0", // There's no event type with id 0
+ },
+ });
+ await handleEvent(req, res);
+
+ expect(res._getStatusCode()).toBe(404);
+ expect(JSON.parse(res._getData())).toStrictEqual({ error: "Event type not found" });
+ });
+});
+
+describe("/api/event-types/[id] only allow GET, fails with POST", () => {
+ it("returns a message with the specified events", async () => {
+ const { req, res } = createMocks({
+ method: "POST", // This POST method is not allowed
+ query: {
+ id: "1",
+ },
+ });
+ await handleEvent(req, res);
+
+ expect(res._getStatusCode()).toBe(405);
+ expect(JSON.parse(res._getData())).toStrictEqual({ error: "Only GET Method allowed" });
+ });
+});
diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].ts
index 7ecc4362c91..8694d32c386 100644
--- a/pages/api/event-types/[id].ts
+++ b/pages/api/event-types/[id].ts
@@ -7,6 +7,8 @@ const prisma = new PrismaClient();
const schema = z
.object({
+ // since nextjs parses query params as strings,
+ // we need to cast them to numbers using z.transform() and parseInt()
id: z
.string()
.regex(/^\d+$/)
@@ -28,20 +30,12 @@ type ResponseData = {
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
if (method === "GET") {
- 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 } });
+ const safe = await schema.safeParse(query);
+ if (safe.success) {
+ const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
- 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 });
+ if (event) res.status(200).json({ data: event });
+ if (!event) res.status(404).json({ error: "Event type not found" });
}
} else {
// Reject any other HTTP method than POST