diff --git a/frontend/src/lib/planner/SwapSuggestionList.svelte b/frontend/src/lib/planner/SwapSuggestionList.svelte new file mode 100644 index 0000000..68273f8 --- /dev/null +++ b/frontend/src/lib/planner/SwapSuggestionList.svelte @@ -0,0 +1,116 @@ + + + +
+

+ Replacing +

+ + {replacingName}{#if replacingMeta}
{replacingMeta}{/if} +
+
+ + +

+ Swap to (easiest first) +

+ + +{#if recipes.length === 0} +

+ Keine Rezepte verfügbar. +

+{:else} + {#each recipes as recipe (recipe.id)} + {@const meta = recipeMeta(recipe)} + {@const alreadyPlanned = currentWeekRecipeIds.has(recipe.id)} +
+
+

+ {recipe.name} +

+ {#if meta} +

+ {meta} +

+ {/if} + {#if alreadyPlanned} +

+ ⚠ Bereits diese Woche +

+ {/if} +
+ +
+ {/each} +{/if} + + +{#if oncancel} + +{/if} diff --git a/frontend/src/lib/planner/SwapSuggestionList.test.ts b/frontend/src/lib/planner/SwapSuggestionList.test.ts new file mode 100644 index 0000000..9fc9f32 --- /dev/null +++ b/frontend/src/lib/planner/SwapSuggestionList.test.ts @@ -0,0 +1,88 @@ +import { describe, it, expect, vi } from 'vitest'; +import { render, screen } from '@testing-library/svelte'; +import { userEvent } from '@testing-library/user-event'; +import SwapSuggestionList from './SwapSuggestionList.svelte'; + +const recipes = [ + { id: 'r1', name: 'Quick carbonara', effort: 'easy', cookTimeMin: 20 }, + { id: 'r2', name: 'Chicken stir-fry', effort: 'easy', cookTimeMin: 25 }, + { id: 'r3', name: 'Mushroom risotto', effort: 'medium', cookTimeMin: 50 } +]; + +const baseProps = { + replacingName: 'Tomato pasta', + replacingMeta: '45 min · Easy', + recipes, + currentWeekRecipeIds: new Set(), + onpick: vi.fn() +}; + +describe('SwapSuggestionList', () => { + it('renders the Replacing banner', () => { + render(SwapSuggestionList, { props: baseProps }); + expect(screen.getByText(/Replacing/i)).toBeTruthy(); + }); + + it('renders old meal name with strikethrough', () => { + render(SwapSuggestionList, { props: baseProps }); + const struck = screen.getByTestId('replacing-name'); + expect(struck.textContent).toContain('Tomato pasta'); + expect(getComputedStyle(struck).textDecoration || struck.style.textDecoration).toContain('line-through'); + }); + + it('renders the easiest-first eyebrow label', () => { + render(SwapSuggestionList, { props: baseProps }); + expect(screen.getByText(/easiest first/i)).toBeTruthy(); + }); + + it('renders all recipe names', () => { + render(SwapSuggestionList, { props: baseProps }); + expect(screen.getByText('Quick carbonara')).toBeTruthy(); + expect(screen.getByText('Chicken stir-fry')).toBeTruthy(); + expect(screen.getByText('Mushroom risotto')).toBeTruthy(); + }); + + it('clicking Pick calls onpick with recipeId and name', async () => { + const onpick = vi.fn(); + const user = userEvent.setup(); + render(SwapSuggestionList, { props: { ...baseProps, onpick } }); + const buttons = screen.getAllByRole('button', { name: /Pick/i }); + await user.click(buttons[0]); + expect(onpick).toHaveBeenCalledWith('r1', 'Quick carbonara'); + }); + + it('shows already-planned warning for recipes in currentWeekRecipeIds', () => { + render(SwapSuggestionList, { + props: { ...baseProps, currentWeekRecipeIds: new Set(['r2']) } + }); + expect(screen.getByTestId('already-planned-r2')).toBeTruthy(); + }); + + it('does not show already-planned warning for recipes not in currentWeekRecipeIds', () => { + render(SwapSuggestionList, { props: baseProps }); + expect(screen.queryByTestId('already-planned-r1')).toBeNull(); + }); + + it('shows empty state when no recipes', () => { + render(SwapSuggestionList, { props: { ...baseProps, recipes: [] } }); + expect(screen.getByTestId('swap-empty-state')).toBeTruthy(); + }); + + it('renders optional Cancel button when oncancel provided', () => { + render(SwapSuggestionList, { props: { ...baseProps, oncancel: vi.fn() } }); + expect(screen.getByRole('button', { name: /Cancel/i })).toBeTruthy(); + }); + + it('does not render Cancel button when oncancel not provided', () => { + render(SwapSuggestionList, { props: baseProps }); + expect(screen.queryByRole('button', { name: /Cancel/i })).toBeNull(); + }); + + it('clicking Cancel calls oncancel', async () => { + const oncancel = vi.fn(); + const user = userEvent.setup(); + render(SwapSuggestionList, { props: { ...baseProps, oncancel } }); + await user.click(screen.getByRole('button', { name: /Cancel/i })); + expect(oncancel).toHaveBeenCalledOnce(); + }); +});