diff --git a/packages/trpc/src/procedures/mealPlans/createMealPlan.spec.ts b/packages/trpc/src/procedures/mealPlans/createMealPlan.spec.ts new file mode 100644 index 000000000..4773a8839 --- /dev/null +++ b/packages/trpc/src/procedures/mealPlans/createMealPlan.spec.ts @@ -0,0 +1,60 @@ +import { trpcSetup, tearDown } from "../../testutils"; +import { prisma } from "@recipesage/prisma"; +import { User } from "@prisma/client"; +import type { CreateTRPCProxyClient } from "@trpc/client"; +import type { AppRouter } from "../../index"; + +describe("createMealPlan", () => { + let user: User; + let trpc: CreateTRPCProxyClient; + + beforeAll(async () => { + ({ user, trpc } = await trpcSetup()); + }); + + afterAll(() => { + return tearDown(user.id); + }); + + describe("success", () => { + it("creates a meal plan", async () => { + const { user: user2 } = await trpcSetup(); + const collaboratorUsers = [user2]; + const response = await prisma.mealPlan.create({ + data: { + title: "Protein", + userId: user.id, + collaboratorUsers: { + createMany: { + data: collaboratorUsers.map((collaboratorUser) => ({ + userId: collaboratorUser.id, + })), + }, + }, + }, + }); + expect(typeof response?.id).toBe("string"); + + const updatedMealPlan = await prisma.mealPlan.findUnique({ + where: { + id: response.id, + }, + }); + expect(updatedMealPlan?.title).toEqual("Protein"); + + await tearDown(user2.id); + }); + }); + describe("error", () => { + it("must throw on meal plan not found", async () => { + return expect(async () => { + await trpc.mealPlans.createMealPlan.mutate({ + title: "Protein", + collaboratorUserIds: ["00000ca5-50e7-4144-bc11-e82925837a14"], + }); + }).rejects.toThrow( + "One or more of the collaborators you specified are not valid", + ); + }); + }); +}); diff --git a/packages/trpc/src/procedures/mealPlans/deleteMealPlan.spec.ts b/packages/trpc/src/procedures/mealPlans/deleteMealPlan.spec.ts new file mode 100644 index 000000000..10985175d --- /dev/null +++ b/packages/trpc/src/procedures/mealPlans/deleteMealPlan.spec.ts @@ -0,0 +1,82 @@ +import { trpcSetup, tearDown } from "../../testutils"; +import { prisma } from "@recipesage/prisma"; +import { User } from "@prisma/client"; +import type { CreateTRPCProxyClient } from "@trpc/client"; +import type { AppRouter } from "../../index"; + +describe("deleteMealPlan", () => { + let user: User; + let trpc: CreateTRPCProxyClient; + + beforeAll(async () => { + ({ user, trpc } = await trpcSetup()); + }); + + afterAll(() => { + return tearDown(user.id); + }); + + describe("success", () => { + it("deletes a meal plan", async () => { + const { user: user2 } = await trpcSetup(); + const collaboratorUsers = [user2]; + const response = await prisma.mealPlan.create({ + data: { + title: "Protein", + userId: user.id, + collaboratorUsers: { + createMany: { + data: collaboratorUsers.map((collaboratorUser) => ({ + userId: collaboratorUser.id, + })), + }, + }, + }, + }); + + await trpc.mealPlans.deleteMealPlan.mutate({ + id: response.id, + }); + + const updatedResponse = await prisma.mealPlan.findUnique({ + where: { + id: response.id, + }, + }); + expect(updatedResponse).toEqual(null); + }); + }); + describe("error", () => { + it("must throw on meal plan not found", async () => { + const { user: user2 } = await trpcSetup(); + + await trpc.mealPlans.createMealPlan.mutate({ + title: "Protein", + collaboratorUserIds: [user2.id], + }); + return expect(async () => { + await trpc.mealPlans.deleteMealPlan.mutate({ + id: "00000ca5-50e7-4144-bc11-e82925837a14", + }); + }).rejects.toThrow( + "Meal plan with that id does not exist or you do not own it", + ); + }); + + it("must throw on meal plan not owned", async () => { + const { user: user2, trpc: trpc2 } = await trpcSetup(); + + const response = await trpc.mealPlans.createMealPlan.mutate({ + title: "Protein", + collaboratorUserIds: [user2.id], + }); + return expect(async () => { + await trpc2.mealPlans.deleteMealPlan.mutate({ + id: response.id, + }); + }).rejects.toThrow( + "Meal plan with that id does not exist or you do not own it", + ); + }); + }); +}); diff --git a/packages/trpc/src/procedures/mealPlans/getMealPlan.spec.ts b/packages/trpc/src/procedures/mealPlans/getMealPlan.spec.ts new file mode 100644 index 000000000..d4120f8b1 --- /dev/null +++ b/packages/trpc/src/procedures/mealPlans/getMealPlan.spec.ts @@ -0,0 +1,76 @@ +import { trpcSetup, tearDown } from "../../testutils"; +import { prisma } from "@recipesage/prisma"; +import { User } from "@prisma/client"; +import type { CreateTRPCProxyClient } from "@trpc/client"; +import type { AppRouter } from "../../index"; + +describe("getMealPlan", () => { + let user: User; + let trpc: CreateTRPCProxyClient; + + beforeAll(async () => { + ({ user, trpc } = await trpcSetup()); + }); + + afterAll(() => { + return tearDown(user.id); + }); + + describe("success", () => { + it("gets a meal plan", async () => { + const { user: user2 } = await trpcSetup(); + const collaboratorUsers = [user2]; + const createdMealPlan = await prisma.mealPlan.create({ + data: { + title: "Protein", + userId: user.id, + collaboratorUsers: { + createMany: { + data: collaboratorUsers.map((collaboratorUser) => ({ + userId: collaboratorUser.id, + })), + }, + }, + }, + }); + + const fetchedMealPlan = await trpc.mealPlans.getMealPlan.query({ + id: createdMealPlan.id, + }); + expect(fetchedMealPlan.id).toEqual(createdMealPlan.id); + }); + }); + describe("error", () => { + it("throws when meal plan not found", async () => { + return expect(async () => { + await trpc.mealPlans.getMealPlan.query({ + id: "00000000-0c70-4718-aacc-05add19096b5", + }); + }).rejects.toThrow("Meal plan with that id not found"); + }); + it("must throw on meal plan not owned", async () => { + const { user: user2 } = await trpcSetup(); + const collaboratorUsers = [user2]; + await prisma.mealPlan.create({ + data: { + title: "Protein", + userId: user.id, + collaboratorUsers: { + createMany: { + data: collaboratorUsers.map((collaboratorUser) => ({ + userId: collaboratorUser.id, + })), + }, + }, + }, + }); + return expect(async () => { + await trpc.mealPlans.getMealPlan.query({ + id: user2.id, + }); + }).rejects.toThrow( + "Meal plan with that id not found or you do not have access", + ); + }); + }); +}); diff --git a/packages/trpc/src/procedures/mealPlans/updateMealPlan.spec.ts b/packages/trpc/src/procedures/mealPlans/updateMealPlan.spec.ts new file mode 100644 index 000000000..eb8725f67 --- /dev/null +++ b/packages/trpc/src/procedures/mealPlans/updateMealPlan.spec.ts @@ -0,0 +1,89 @@ +import { trpcSetup, tearDown } from "../../testutils"; +import { prisma } from "@recipesage/prisma"; +import { User } from "@prisma/client"; +import type { CreateTRPCProxyClient } from "@trpc/client"; +import type { AppRouter } from "../../index"; + +describe("updateMealPlan", () => { + let user: User; + let trpc: CreateTRPCProxyClient; + + beforeAll(async () => { + ({ user, trpc } = await trpcSetup()); + }); + + afterAll(() => { + return tearDown(user.id); + }); + describe("success", () => { + it("updates a meal plan", async () => { + const { user: user2 } = await trpcSetup(); + const collaboratorUsers = [user2]; + const createdMealPlan = await prisma.mealPlan.create({ + data: { + title: "Protein", + userId: user.id, + collaboratorUsers: { + createMany: { + data: collaboratorUsers.map((collaboratorUser) => ({ + userId: collaboratorUser.id, + })), + }, + }, + }, + }); + const response = await trpc.mealPlans.updateMealPlan.mutate({ + title: "not protein", + id: createdMealPlan.id, + collaboratorUserIds: [user2.id], + }); + const updatedMealPlan = await prisma.mealPlan.findUnique({ + where: { + id: response.id, + }, + }); + expect(updatedMealPlan?.title).toEqual("not protein"); + + await tearDown(user2.id); + }); + }); + describe("error", () => { + it("throws when meal plan not found", async () => { + return expect(async () => { + await trpc.mealPlans.updateMealPlan.mutate({ + id: "00000000-0c70-4718-aacc-05add19096b5", + title: "Protein", + collaboratorUserIds: [user.id], + }); + }).rejects.toThrow( + "Meal plan with that id does not exist or you do not have access", + ); + }); + it("must throw on meal plan not owned", async () => { + const { user: user2 } = await trpcSetup(); + const collaboratorUsers = [user2]; + await prisma.mealPlan.create({ + data: { + title: "Protein", + userId: user.id, + collaboratorUsers: { + createMany: { + data: collaboratorUsers.map((collaboratorUser) => ({ + userId: collaboratorUser.id, + })), + }, + }, + }, + }); + return expect(async () => { + await trpc.mealPlans.updateMealPlan.mutate({ + id: user2.id, + title: "Protein", + collaboratorUserIds: [user2.id], + }); + }).rejects.toThrow( + "Meal plan with that id does not exist or you do not have access", + ); + }); + }); +});