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

[test] update the recipe #1362

Merged
merged 3 commits into from
Apr 27, 2024
Merged
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
141 changes: 141 additions & 0 deletions packages/trpc/src/procedures/recipes/updateRecipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
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";
import { recipeFactory } from "../../factories/recipeFactory";

describe("updateRecipe", () => {
let user: User;
let trpc: CreateTRPCProxyClient<AppRouter>;

beforeAll(async () => {
({ user, trpc } = await trpcSetup());
});

afterAll(() => {
return tearDown(user.id);
});

describe("success", () => {
it("updates the recipe", async () => {
const recipe = await prisma.recipe.create({
data: {
...recipeFactory(user.id),
title: "Mexican chicken",
},
});
const response = await trpc.recipes.updateRecipe.mutate({
...recipeFactory(user.id),
title: "marmelad",
id: recipe.id,
labelIds: [],
imageIds: [],
folder: "main",
});
expect(response.title).toEqual("marmelad");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to query the DB again and check that it was properly updated in the database


const updatedRecipe = await prisma.recipe.findUnique({
where: {
id: recipe.id,
},
});
expect(updatedRecipe?.title).toEqual("marmelad");
});

it("allows to update recipe with image", async () => {
const image = await prisma.image.create({
data: {
location: "somehosting.com/1",
userId: user.id,
key: "someKey",
json: {},
},
});

const recipe = await prisma.recipe.create({
data: {
...recipeFactory(user.id),
},
});
await trpc.recipes.updateRecipe.mutate({
...recipeFactory(user.id),
id: recipe.id,
labelIds: [],
imageIds: [image.id],
folder: "main",
});

const recipeImage = await prisma.recipeImage.findFirst({
where: {
recipeId: recipe.id,
},
});
expect(typeof recipeImage?.id).toBe("string");
});
});
describe("error", () => {
it("throws an error when updating a recipe that does not exist", async () => {
return expect(async () => {
await trpc.recipes.updateRecipe.mutate({
...recipeFactory(user.id),
title: "marmelad",
labelIds: [],
imageIds: [],
folder: "main",
id: "00000000-0c70-4718-aacc-05add19096b5",
});
}).rejects.toThrow("Recipe not found");
});
});
it("fails to update a recipe with differrent user", async () => {
const { user: user2 } = await trpcSetup();
const recipe = await prisma.recipe.create({
data: {
...recipeFactory(user2.id),
title: "salad",
},
});

await expect(async () => {
await trpc.recipes.updateRecipe.mutate({
...recipeFactory(user.id),
title: "salad",
labelIds: [],
imageIds: [],
folder: "main",
id: recipe.id,
});
}).rejects.toThrow("Recipe not found");
return tearDown(user2.id);
});
it("throws an error when updating a recipe with label ids the user does not own", async () => {
const { user: user2 } = await trpcSetup();

const recipe = await prisma.recipe.create({
data: {
...recipeFactory(user.id),
},
});

const label = await prisma.label.create({
data: {
title: "salads",
userId: user2.id,
labelGroupId: null,
},
});

await expect(async () => {
await trpc.recipes.updateRecipe.mutate({
...recipeFactory(user.id),
title: "salads",
labelIds: [label.id],
imageIds: [],
folder: "main",
id: recipe.id,
});
}).rejects.toThrow("You do not own one of the specified label ids");
return tearDown(user2.id);
});
});