feat: adds bookings and more tests

This commit is contained in:
Agusti Fernandez Pardo committed 2022-03-26 22:29:30 +01:00
1 parent c0d7623beb
commit ac307f7161
38 files changed
+515 -285

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
import handleEventTypeEdit from "@api/event-types/[id]/edit";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
describe("PATCH /api/event-types/[id]/edit with valid id and body updates an event-type", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "PATCH",
query: {
id: "2",
},
body: {
title: "Updated title",
slug: "updated-slug",
length: 1,
},
});
const event = await prisma.eventType.findUnique({ where: { id: parseInt(req.query.id) } });
await handleEventTypeEdit(req, res);
expect(res._getStatusCode()).toBe(200);
if (event) event.title = "Updated title";
expect(JSON.parse(res._getData())).toStrictEqual({ data: event });
});
});
describe("PATCH /api/event-types/[id]/edit with invalid id returns 404", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "PATCH",
query: {
id: "0",
},
body: {
title: "Updated title",
slug: "updated-slug",
length: 1,
},
});
const event = await prisma.eventType.findUnique({ where: { id: parseInt(req.query.id) } });
await handleEventTypeEdit(req, res);
expect(res._getStatusCode()).toBe(404);
if (event) event.title = "Updated title";
expect(JSON.parse(res._getData())).toStrictEqual({ "error": {
"clientVersion": "3.10.0",
"code": "P2025",
"meta": {
"cause": "Record to update not found.",
},
},
"message": "Event type with ID 0 not found and wasn't updated", });
});
});
describe("PATCH /api/event-types/[id]/edit with valid id and no body returns 400 error and zod validation errors", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "PATCH",
query: {
id: "2",
},
});
await handleEventTypeEdit(req, res);
expect(res._getStatusCode()).toBe(400);
expect(JSON.parse(res._getData())).toStrictEqual([{"code": "invalid_type", "expected": "string", "message": "Required", "path": ["title"], "received": "undefined"}, {"code": "invalid_type", "expected": "string", "message": "Required", "path": ["slug"], "received": "undefined"}, {"code": "invalid_type", "expected": "number", "message": "Required", "path": ["length"], "received": "undefined"}]);
});
});
describe("POST /api/event-types/[id]/edit fails, only PATCH allowed", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
query: {
id: "1",
},
body: {
title: "Updated title",
slug: "updated-slug",
length: 1,
},
});
await handleEventTypeEdit(req, res);
expect(res._getStatusCode()).toBe(405);
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only PATCH Method allowed for updating event-types" });
});
});
+78
View File
@@ -0,0 +1,78 @@
import handleUser from "@api/users/[id]";
import { createMocks } from "node-mocks-http";
import prisma from "@calcom/prisma";
import { stringifyISODate } from "@lib/utils/stringifyISODate";
describe("GET /api/users/[id] with valid id as string returns an user-type", () => {
it("returns a message with the specified events", async () => {
const { req, res } = createMocks({
method: "GET",
query: {
id: "1",
},
});
const user = await prisma.user.findUnique({ where: { id: 1 } });
await handleUser(req, res);
expect(res._getStatusCode()).toBe(200);
expect(JSON.parse(res._getData())).toEqual({ data: {...user, createdDate: stringifyISODate(user?.createdDate), emailVerified: stringifyISODate(user?.emailVerified)} });
});
});
// 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("GET /api/users/[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 handleUser(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",
},
]);
});
});
describe("GET /api/users/[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 user type with id 0
},
});
await handleUser(req, res);
expect(res._getStatusCode()).toBe(404);
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Event type not found" });
});
});
describe("POST /api/users/[id] fails, only GET allowed", () => {
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 handleUser(req, res);
expect(res._getStatusCode()).toBe(405);
expect(JSON.parse(res._getData())).toStrictEqual({ message: "Only GET Method allowed" });
});
});