diff --git a/frontend/src/lib/onboarding/HouseholdSetupForm.svelte b/frontend/src/lib/onboarding/HouseholdSetupForm.svelte new file mode 100644 index 0000000..29aeedf --- /dev/null +++ b/frontend/src/lib/onboarding/HouseholdSetupForm.svelte @@ -0,0 +1,80 @@ + + +
diff --git a/frontend/src/lib/onboarding/HouseholdSetupForm.test.ts b/frontend/src/lib/onboarding/HouseholdSetupForm.test.ts new file mode 100644 index 0000000..aa535e2 --- /dev/null +++ b/frontend/src/lib/onboarding/HouseholdSetupForm.test.ts @@ -0,0 +1,81 @@ +import { describe, it, expect, vi } from 'vitest'; +import { render, screen } from '@testing-library/svelte'; +import { userEvent } from '@testing-library/user-event'; +import HouseholdSetupForm from './HouseholdSetupForm.svelte'; + +vi.mock('$app/forms', () => ({ + enhance: () => ({ destroy: () => {} }) +})); + +describe('HouseholdSetupForm', () => { + it('renders household name input with label', () => { + render(HouseholdSetupForm); + expect(screen.getByLabelText('Haushaltsname')).toBeInTheDocument(); + }); + + it('renders heading', () => { + render(HouseholdSetupForm); + expect(screen.getByText('Haushalt benennen')).toBeInTheDocument(); + }); + + it('renders Continue button', () => { + render(HouseholdSetupForm); + expect(screen.getByRole('button', { name: /weiter/i })).toBeInTheDocument(); + }); + + it('Continue button is disabled when name is empty', () => { + render(HouseholdSetupForm); + const btn = screen.getByRole('button', { name: /weiter/i }); + expect(btn).toBeDisabled(); + }); + + it('Continue button is enabled when name has text', async () => { + const user = userEvent.setup(); + render(HouseholdSetupForm); + + await user.type(screen.getByLabelText('Haushaltsname'), 'Familie Müller'); + expect(screen.getByRole('button', { name: /weiter/i })).not.toBeDisabled(); + }); + + it('shows validation error when submitting with empty name', async () => { + const user = userEvent.setup(); + render(HouseholdSetupForm); + + // override disabled to allow submit attempt by typing then clearing + const input = screen.getByLabelText('Haushaltsname'); + await user.type(input, 'a'); + await user.clear(input); + await user.click(screen.getByRole('button', { name: /weiter/i })); + + expect(screen.getByText('Haushaltsname ist erforderlich')).toBeInTheDocument(); + }); + + it('shows server-side error from form prop', () => { + render(HouseholdSetupForm, { + props: { + form: { + errors: { form: 'Haushalt konnte nicht erstellt werden.' }, + name: 'Smith family' + } + } + }); + expect(screen.getByText('Haushalt konnte nicht erstellt werden.')).toBeInTheDocument(); + }); + + it('repopulates name from form prop on server error', () => { + render(HouseholdSetupForm, { + props: { + form: { + errors: { form: 'Fehler' }, + name: 'Familie Müller' + } + } + }); + expect(screen.getByLabelText('Haushaltsname')).toHaveValue('Familie Müller'); + }); + + it('input has correct placeholder', () => { + render(HouseholdSetupForm); + expect(screen.getByPlaceholderText('z.B. Familie Müller')).toBeInTheDocument(); + }); +});