Skip to content

Commit 06ad85b

Browse files
committed
Avoid passing 'delete' and 'update' nested writes when creating a recipe.
1 parent 97eb9a1 commit 06ad85b

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

webserver/e2e/new.spec.ts

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { prisma } from '@lib/prisma.js';
2+
import { expect } from '@playwright/test';
3+
import { test } from './fixtures.js';
4+
5+
test('When not logged in, redirects to /login', async ({ page }) => {
6+
await page.goto(`/new`);
7+
expect(new URL(page.url()).pathname).toBe('/login');
8+
await expect(page.getByRole('paragraph')).toContainText("Please login to add a new recipe.");
9+
});
10+
11+
test.describe("Logged in", () => {
12+
let recipeId: number | undefined;
13+
14+
test.afterEach(async () => {
15+
if (recipeId !== undefined) {
16+
await prisma.recipe.delete({ where: { id: recipeId } })
17+
recipeId = undefined;
18+
}
19+
});
20+
21+
test('can create a new recipe', async ({ page, testLogin }) => {
22+
const {username} = testLogin;
23+
await page.goto(`/new`);
24+
const nameField = page.getByLabel("Recipe name:");
25+
await expect(nameField).toHaveValue("");
26+
await nameField.fill("Chocolate Chip Cookies");
27+
28+
const servingsField = page.getByLabel("Servings");
29+
await expect(servingsField).toHaveValue("");
30+
await servingsField.fill("4");
31+
32+
const oneIngredient = page.getByRole("group")
33+
.filter({ has: page.getByRole("heading", { name: "Ingredients" }) })
34+
.getByRole("listitem").first();
35+
36+
await expect(oneIngredient.getByPlaceholder("Amount")).toHaveValue("");
37+
await oneIngredient.getByPlaceholder("Amount").fill("7");
38+
39+
await expect(oneIngredient.getByPlaceholder("Unit")).toHaveValue("");
40+
await oneIngredient.getByPlaceholder("Unit").fill("oz");
41+
42+
await expect(oneIngredient.getByPlaceholder("Ingredient")).toHaveValue("");
43+
await oneIngredient.getByPlaceholder("Ingredient").fill("Pears");
44+
45+
await expect(oneIngredient.getByPlaceholder("Preparation")).toHaveValue("");
46+
await oneIngredient.getByPlaceholder("Preparation").fill("sliced");
47+
48+
49+
const instructions = page.getByRole("group")
50+
.filter({ has: page.getByRole("heading", { name: "Instructions" }) })
51+
.getByRole("textbox");
52+
await expect(instructions).toHaveText([""]);
53+
await instructions.first().fill("First step.");
54+
55+
const categories = page.getByRole("group")
56+
.filter({ has: page.getByRole("heading", { name: "Categories" }) })
57+
.getByRole("combobox");
58+
// Alphabetic:
59+
expect(await categories.evaluateAll(
60+
elems => elems.map(elem => elem instanceof HTMLInputElement ? elem.value : undefined))
61+
).toEqual([""]);
62+
await categories.first().fill("lunch");
63+
64+
await page.getByRole("button", { name: "Save" }).click();
65+
66+
await expect(page.getByRole("heading", {level: 2})).toHaveText("Chocolate Chip Cookies");
67+
68+
const recipe = await prisma.recipe.findMany({
69+
where: { author: {username}, slug: "chocolate-chip-cookies" },
70+
include: {
71+
ingredients: { orderBy: { order: "asc" } },
72+
categories: { orderBy: { name: "asc" } }
73+
}
74+
});
75+
expect(recipe.length).toEqual(1);
76+
recipeId = recipe[0]!.id;
77+
expect(recipe[0]).toMatchObject({
78+
name: "Chocolate Chip Cookies",
79+
slug: "chocolate-chip-cookies",
80+
ingredients: [{
81+
amount: "7",
82+
unit: "oz",
83+
name: "Pears",
84+
preparation: "sliced",
85+
}],
86+
steps: ["First step."],
87+
categories: [
88+
{ name: "lunch" },
89+
]
90+
});
91+
});
92+
});
93+

webserver/src/pages/edit/[username]/[slug]/submit.astro

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ async function updateRecipe(): Promise<UpdateResult> {
182182
const recipeCreateData: Prisma.RecipeCreateInput = Object.assign(
183183
recipeData,
184184
{
185-
// Strip out the deleteMany and update fields from RecipeUpdateInput.ingredients.
185+
// Strip out the deleteMany and update fields.
186+
sources: {create: recipeData.sources?.create},
186187
ingredients: { createMany: recipeData.ingredients.createMany },
187188
},
188189
);

0 commit comments

Comments
 (0)