-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
[test] update the recipe #1362
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
packages/trpc/src/procedures/recipes/updateRecipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
||
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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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