Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meal Plan tests #1378

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions packages/trpc/src/procedures/mealPlans/createMealPlan.spec.ts
Original file line number Diff line number Diff line change
@@ -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<AppRouter>;

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",
);
});
});
});
82 changes: 82 additions & 0 deletions packages/trpc/src/procedures/mealPlans/deleteMealPlan.spec.ts
Original file line number Diff line number Diff line change
@@ -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<AppRouter>;

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",
);
});
});
});
76 changes: 76 additions & 0 deletions packages/trpc/src/procedures/mealPlans/getMealPlan.spec.ts
Original file line number Diff line number Diff line change
@@ -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<AppRouter>;

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",
);
});
});
});
89 changes: 89 additions & 0 deletions packages/trpc/src/procedures/mealPlans/updateMealPlan.spec.ts
Original file line number Diff line number Diff line change
@@ -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<AppRouter>;

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",
);
});
});
});