Skip to content
Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ KI-verktøy får ikke ekte passord, tokens, personopplysninger eller andre hemme

- Søk dekker tittel og beskrivelse; ingrediens- og tagsøk er videre arbeid.
- Trending er ikke implementert. Høyest vurdering bruker gjennomsnittet uten vekting etter antall stemmer.
- Utkast beholdes bare i skjemaet og overlever ikke navigering eller oppdatering.
- Utkast lagres bare i nettleseren (localStorage) per bruker og oppskrift, ikke på serveren, og følger derfor ikke brukeren til en annen enhet. De slettes ved utlogging.
- Seed inneholder 51 demooppskrifter og demonstrerer ikke ytelse med store datamengder.
- Bildeoperasjoner bruker en global lås for korrekt koordinering. Dette begrenser samtidige skriveoperasjoner.
- Bildekvoter har faste grenser. Registrering har IP-begrensning, men flere kontoer kan fortsatt omgå brukerkvotene. Refererte bilder teller mot totalgrensen og slettes ikke av cleanup.
Expand Down
22 changes: 21 additions & 1 deletion apps/web/src/features/recipes/RecipeCreatePage.css
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,20 @@
background: var(--color-surface-selected);
}

.recipe-create-page__draft-actions {
display: flex;
flex-wrap: wrap;
gap: var(--space-2);
margin-top: var(--space-2);
}

/* Move up / move down / remove buttons at the end of each row. */
.recipe-create-page__delete-control {
display: flex;
align-items: center;
justify-content: center;
width: var(--target-size-min);
gap: var(--space-1);
min-width: var(--target-size-min);
height: var(--target-size-min);
}

Expand Down Expand Up @@ -381,6 +390,17 @@
padding-block: var(--space-8) var(--space-12);
}

/* Move/remove buttons get their own line under the fields, so the
fields keep their full width on phones. */
.recipe-create-page__repeatable-row {
grid-template-columns: auto minmax(0, 1fr);
}

.recipe-create-page__repeatable-row > .recipe-create-page__delete-control {
grid-column: 2;
justify-self: end;
}

.recipe-create-page__content {
margin-top: var(--space-6);
}
Expand Down
128 changes: 127 additions & 1 deletion apps/web/src/features/recipes/RecipeCreatePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('RecipeCreatePage', () => {
expect(screen.queryByLabelText('Step 2')).not.toBeInTheDocument();
});

it('supports structured ingredients and button-based reordering', async () => {
it('supports structured ingredients and reordering by dragging the handle', async () => {
const user = userEvent.setup();

mount();
Expand Down Expand Up @@ -346,3 +346,129 @@ it('does not fetch or reset the new recipe form after list invalidation', () =>
expect(title).toHaveValue('New draft');
expect(fetch).not.toHaveBeenCalled();
});
it('reorders ingredients and steps with the keyboard, keeping focus and announcing the position', async () => {
const user = userEvent.setup();
mount();
// Fill fields directly: this test is about moving rows, not typing, and
// per-keystroke typing made it too slow for the CI runner's 5 s limit.
const fill = (label: string, value: string) =>
fireEvent.change(screen.getByLabelText(label), { target: { value } });
fill('Ingredient 1', 'Flour');
await user.click(screen.getByRole('button', { name: 'Add ingredient' }));
fill('Ingredient 2', 'Sugar');
await user.click(screen.getByRole('button', { name: 'Add ingredient' }));
fill('Ingredient 3', 'Salt');

// The first row cannot move up; the last cannot move down.
expect(
screen.getByRole('button', { name: 'Move ingredient 1 up' }),
).toBeDisabled();
expect(
screen.getByRole('button', { name: 'Move ingredient 3 down' }),
).toBeDisabled();

screen.getByRole('button', { name: 'Move ingredient 1 down' }).focus();
await user.keyboard('{Enter}');
expect(screen.getByLabelText('Ingredient 1')).toHaveValue('Sugar');
expect(screen.getByLabelText('Ingredient 2')).toHaveValue('Flour');
// Focus follows the moved row, so pressing again keeps moving Flour.
expect(
screen.getByRole('button', { name: 'Move ingredient 2 down' }),
).toHaveFocus();
expect(screen.getByRole('status')).toHaveTextContent(
'Ingredient moved to position 2 of 3.',
);
await user.keyboard('{Enter}');
expect(screen.getByLabelText('Ingredient 3')).toHaveValue('Flour');
// At the last place the down button is disabled, so focus moves to up.
expect(
screen.getByRole('button', { name: 'Move ingredient 3 up' }),
).toHaveFocus();

fill('Step 1', 'Mix');
await user.click(screen.getByRole('button', { name: 'Add step' }));
fill('Step 2', 'Bake');
await user.click(screen.getByRole('button', { name: 'Move step 2 up' }));
expect(screen.getByLabelText('Step 1')).toHaveValue('Bake');
expect(screen.getByLabelText('Step 2')).toHaveValue('Mix');
expect(screen.getByRole('status')).toHaveTextContent(
'Step moved to position 1 of 2.',
);
});
describe('recipe drafts', () => {
beforeEach(() => localStorage.clear());

it('saves a draft that survives a reload and can be restored or discarded', async () => {
const user = userEvent.setup();
const first = mount();
fireEvent.change(screen.getByLabelText(/Recipe title/i), {
target: { value: 'Draft soup' },
});
fireEvent.change(screen.getByLabelText('Step 1'), {
target: { value: 'Simmer' },
});
await user.click(screen.getByRole('button', { name: 'Save draft' }));
expect(screen.getByText('Draft saved')).toBeInTheDocument();
first.unmount();

// A fresh form (as after navigation or reload) offers the draft.
const second = mount();
expect(screen.getByText('Saved draft found')).toBeInTheDocument();
expect(screen.getByLabelText(/Recipe title/i)).toHaveValue('');
await user.click(screen.getByRole('button', { name: 'Restore draft' }));
expect(screen.getByLabelText(/Recipe title/i)).toHaveValue('Draft soup');
expect(screen.getByLabelText('Step 1')).toHaveValue('Simmer');
second.unmount();

mount();
await user.click(screen.getByRole('button', { name: 'Discard draft' }));
expect(screen.queryByText('Saved draft found')).not.toBeInTheDocument();
expect(localStorage.length).toBe(0);
});

it('marks a draft of an existing recipe as outdated after the recipe changed', async () => {
localStorage.setItem(
`recipe-platform:draft:${apiUser.id}:${apiRecipe.id}`,
JSON.stringify({
version: 1,
savedAt: '2026-10-01T10:00:00Z',
baseUpdatedAt: '2026-01-01T00:00:00Z',
fields: {
title: 'Old title',
description: '',
category: 'soup',
difficulty: 'easy',
visibility: 'public',
prepTime: '1',
cookingTime: '1',
servings: '1',
tags: '',
imageUrl: null,
ingredients: [{ name: 'Water', amount: '1', unit: 'l' }],
instructions: ['Boil'],
},
}),
);
mockRecipeFetch(() => ({
data: { recipe: { ...apiRecipe, isCreatedByCurrentUser: true } },
}));
mount('/recipes/' + apiRecipe.id + '/edit');
expect(await screen.findByText('Outdated draft found')).toBeInTheDocument();
});

it('clears the draft after the recipe is saved', async () => {
mockRecipeFetch((operation) =>
operation === 'UpdateRecipe'
? { data: { updateRecipe: apiRecipe } }
: { data: { recipe: { ...apiRecipe, isCreatedByCurrentUser: true } } },
);
const user = userEvent.setup();
mount('/recipes/' + apiRecipe.id + '/edit');
await screen.findByRole('heading', { name: 'Edit Recipe' });
await user.click(screen.getByRole('button', { name: 'Save draft' }));
expect(localStorage.length).toBe(1);
await user.click(screen.getByRole('button', { name: 'Save changes' }));
await screen.findByRole('heading', { name: 'Saved recipe destination' });
expect(localStorage.length).toBe(0);
});
});
Loading