feat(recipes): send null instead of undefined for blank serves/cookTimeMin

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-10 09:06:39 +02:00
parent 44b3f06474
commit b1eb9ed964
4 changed files with 26 additions and 4 deletions

View File

@@ -72,8 +72,8 @@ export const actions: Actions = {
params: { path: { id: params.id } }, params: { path: { id: params.id } },
body: { body: {
name: name.trim(), name: name.trim(),
serves: serves ? Number(serves) || undefined : undefined, serves: serves ? Number(serves) || null : null,
cookTimeMin: cookTimeMin ? Number(cookTimeMin) || undefined : undefined, cookTimeMin: cookTimeMin ? Number(cookTimeMin) || null : null,
effort, effort,
heroImageUrl, heroImageUrl,
ingredients: (parsedIngredients as { name: string; quantity: string; unit: string }[]) ingredients: (parsedIngredients as { name: string; quantity: string; unit: string }[])

View File

@@ -178,6 +178,19 @@ describe('edit recipe page — update action', () => {
})); }));
}); });
it('sends null for serves and cookTimeMin when fields are blank', async () => {
mockPut.mockResolvedValue({ error: undefined });
const fd = makeFormData({ serves: '', cookTimeMin: '' });
await actions.update({
request: { formData: async () => fd },
fetch: vi.fn(),
params: { id: 'r1' }
} as any).catch(() => {});
const body = mockPut.mock.calls[0][1].body;
expect(body.serves).toBeNull();
expect(body.cookTimeMin).toBeNull();
});
it('sends heroImageUrl in PUT body when provided', async () => { it('sends heroImageUrl in PUT body when provided', async () => {
mockPut.mockResolvedValue({ error: undefined }); mockPut.mockResolvedValue({ error: undefined });
const fd = makeFormData({ heroImageUrl: 'data:image/jpeg;base64,abc123' }); const fd = makeFormData({ heroImageUrl: 'data:image/jpeg;base64,abc123' });

View File

@@ -44,8 +44,8 @@ export const actions: Actions = {
const { error: apiError } = await api.POST('/v1/recipes', { const { error: apiError } = await api.POST('/v1/recipes', {
body: { body: {
name: name.trim(), name: name.trim(),
serves: serves ? Number(serves) || undefined : undefined, serves: serves ? Number(serves) || null : null,
cookTimeMin: cookTimeMin ? Number(cookTimeMin) || undefined : undefined, cookTimeMin: cookTimeMin ? Number(cookTimeMin) || null : null,
effort, effort,
heroImageUrl, heroImageUrl,
ingredients: (parsedIngredients as { name: string; quantity: string; unit: string }[]) ingredients: (parsedIngredients as { name: string; quantity: string; unit: string }[])

View File

@@ -145,6 +145,15 @@ describe('new recipe page — create action', () => {
expect(mockPost).toHaveBeenCalledWith('/v1/recipes', expect.objectContaining({ body: expect.objectContaining({ name: 'Test Rezept', effort: 'easy' }) })); expect(mockPost).toHaveBeenCalledWith('/v1/recipes', expect.objectContaining({ body: expect.objectContaining({ name: 'Test Rezept', effort: 'easy' }) }));
}); });
it('sends null for serves and cookTimeMin when fields are blank', async () => {
mockPost.mockResolvedValue({ error: undefined });
const fd = makeFormData({ serves: '', cookTimeMin: '' });
await actions.create({ request: { formData: async () => fd }, fetch: vi.fn() } as any).catch(() => {});
const body = mockPost.mock.calls[0][1].body;
expect(body.serves).toBeNull();
expect(body.cookTimeMin).toBeNull();
});
it('sends heroImageUrl in POST body when provided', async () => { it('sends heroImageUrl in POST body when provided', async () => {
mockPost.mockResolvedValue({ error: undefined }); mockPost.mockResolvedValue({ error: undefined });
const fd = makeFormData({ heroImageUrl: 'data:image/jpeg;base64,abc123' }); const fd = makeFormData({ heroImageUrl: 'data:image/jpeg;base64,abc123' });