Track preschool planning days, partner invites and real vacation forfeiture

Three strands of work that grew together across the same settings screen and
API client, so they land as one change.

Per-year preschool planning days ("studiedagar"): new table, repository
(SQLite + Postgres), service, router and migration 007, plus the hooks and
Settings UI to plan and record them per child and year. Allocation now
reserves the recorded figure for a year where one exists and only falls back
to the child-level estimate otherwise, marking that fallback as assumed.

Partner invitations: workspaces issue and list invitations, registration
accepts an invite token to join the inviter's workspace instead of creating a
personal one, and a /join/:token page plus the settings invite panel drive it.

Vacation forfeiture: tips and the dashboard now distinguish days a parent
genuinely loses from days that merely age, so the warning stops firing on
balances that were never at risk.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-20 11:22:07 +02:00
parent 6904961bb4
commit 0e9b20ec37
88 changed files with 5301 additions and 348 deletions
+152 -7
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'
import { render, screen, fireEvent, waitFor, act, within } from '@testing-library/react'
import { Settings } from './Settings'
import * as hooks from '@/hooks'
import { useWorkspace } from '@/context'
@@ -8,6 +8,7 @@ import { ApiRequestError } from '@/services/errors'
import { LanguageProvider } from '@/i18n'
import type { BalanceFlowProps } from '@/components/balance/BalanceFlow'
import type { CalendarExceptionsProps } from '@/components/calendar/CalendarExceptions'
import type { PartnerInvitePanelProps } from '@/components/settings/PartnerInvitePanel'
import {
makeWorkspaceContextValue,
makeWorkspaceMembersResult,
@@ -16,6 +17,7 @@ import {
makeChildSettingsResult,
makeDayOverridesResult,
makeCalendarDaysResult,
makePreschoolPlanningDaysResult,
} from '../../tests/fixtures'
vi.mock('@/hooks')
@@ -49,6 +51,16 @@ vi.mock('@/components/calendar/CalendarExceptions', () => ({
},
}))
// `PartnerInvitePanel` owns its own test suite (PartnerInvitePanel.test.tsx)
// — Settings only needs to prove it's mounted with the right workspace id.
let lastPartnerInvitePanelProps: PartnerInvitePanelProps | null = null
vi.mock('@/components/settings/PartnerInvitePanel', () => ({
PartnerInvitePanel: (props: PartnerInvitePanelProps) => {
lastPartnerInvitePanelProps = props
return <div data-testid="partner-invite-panel-stub" />
},
}))
function renderSettings() {
return render(<Settings />)
}
@@ -129,6 +141,7 @@ describe('Settings', () => {
vi.clearAllMocks()
lastBalanceFlowProps = null
lastCalendarExceptionsProps = null
lastPartnerInvitePanelProps = null
mockUpsertVacationDays = vi.fn().mockResolvedValue(undefined)
mockUpdateChildDetails = vi.fn().mockResolvedValue(undefined)
mockUpsertDayOverride = vi.fn().mockResolvedValue(undefined)
@@ -182,6 +195,15 @@ describe('Settings', () => {
data: mockCalendarWindow,
})
)
vi.mocked(hooks.usePreschoolPlanningDays).mockReturnValue(makePreschoolPlanningDaysResult())
// `useDebouncedCallback` is a real hook exercised end-to-end by its own
// test suite and by `VacationDaysTable.test.tsx` — here it's mocked to
// fire immediately (no timers to juggle) since these tests only care
// that Settings wires the right save handler through, not about the
// debounce timing itself.
vi.mocked(hooks.useDebouncedCallback).mockImplementation(
((callback: (...args: unknown[]) => void) => callback) as typeof hooks.useDebouncedCallback,
)
vi.mocked(api.updateParentSettings).mockResolvedValue({
uuid: 'pd-1',
name: 'Test Parent',
@@ -255,12 +277,12 @@ describe('Settings', () => {
expect(totalInput.value).toBe('25')
})
it('saves the edited year with its total days when Spara is clicked', async () => {
it('autosaves an edited year with its total days, with no Save button', async () => {
renderSettings()
const year = CURRENT_YEAR + 1
expect(screen.queryByTestId(`vacation-save-${year}`)).not.toBeInTheDocument()
const totalInput = screen.getByTestId(`vacation-total-days-${year}`)
fireEvent.change(totalInput, { target: { value: '30' } })
fireEvent.click(screen.getByTestId(`vacation-save-${year}`))
await waitFor(() =>
expect(mockUpsertVacationDays).toHaveBeenCalledWith({
@@ -273,14 +295,77 @@ describe('Settings', () => {
)
})
it('does not disable the vacation table while the family panel is saving', async () => {
it('does not disable the vacation inputs while the family panel is saving', async () => {
renderSettings()
fireEvent.click(screen.getByTestId('save-family-button'))
// The vacation save button must stay enabled — it has its own
// `isSavingVacation` flag, independent of the family panel's.
expect(screen.getByTestId(`vacation-save-${CURRENT_YEAR}`)).not.toBeDisabled()
// The vacation table has no shared saving flag with the family panel
// any more — each row autosaves and reports its own status.
expect(screen.getByTestId(`vacation-total-days-${CURRENT_YEAR}`)).not.toBeDisabled()
await waitFor(() => expect(screen.getByTestId('save-family-button')).not.toBeDisabled())
})
it('renders one table per workspace member, self first, keyed on (personalDataUuid, year)', async () => {
vi.mocked(hooks.useWorkspaceMembers).mockReturnValue(
makeWorkspaceMembersResult({
data: [selfMember, partnerMember],
refresh: mockRefreshMembers,
}),
)
vi.mocked(hooks.useVacationDays).mockReturnValue(
makeVacationDaysResult({
data: [
...mockVacationDays,
{
id: 'vd-2',
personalDataUuid: 'pd-2',
workspaceId: 'ws-1',
year: CURRENT_YEAR,
totalDays: 20,
savedDaysIn: 3,
createdAt: '2026-01-01T00:00:00',
updatedAt: '2026-01-01T00:00:00',
},
],
upsert: mockUpsertVacationDays,
}),
)
renderSettings()
const selfTable = screen.getByTestId('vacation-days-table-pd-1')
const partnerTable = screen.getByTestId('vacation-days-table-pd-2')
expect(
(within(selfTable).getByTestId(`vacation-total-days-${CURRENT_YEAR}`) as HTMLInputElement).value,
).toBe('10')
expect(
(within(partnerTable).getByTestId(`vacation-total-days-${CURRENT_YEAR}`) as HTMLInputElement).value,
).toBe('20')
// Self's table must come first in document order.
const tables = screen.getAllByTestId(/^vacation-days-table-/)
expect(tables[0]).toBe(selfTable)
expect(tables[1]).toBe(partnerTable)
})
it("saves a partner's edit under the partner's own personalDataUuid, not the logged-in user's", async () => {
vi.mocked(hooks.useWorkspaceMembers).mockReturnValue(
makeWorkspaceMembersResult({
data: [selfMember, partnerMember],
refresh: mockRefreshMembers,
}),
)
renderSettings()
const partnerTable = screen.getByTestId('vacation-days-table-pd-2')
fireEvent.change(within(partnerTable).getByTestId(`vacation-total-days-${CURRENT_YEAR}`), {
target: { value: '18' },
})
await waitFor(() =>
expect(mockUpsertVacationDays).toHaveBeenCalledWith(
expect.objectContaining({ personalDataUuid: 'pd-2', year: CURRENT_YEAR, totalDays: 18 }),
),
)
})
})
describe('family panel — child + profile (one combined Save)', () => {
@@ -322,6 +407,56 @@ describe('Settings', () => {
})
})
describe('preschool planning days — per-year editing', () => {
it('renders one row per vacation year, wired to usePreschoolPlanningDays', () => {
renderSettings()
for (let year = CURRENT_YEAR; year <= CURRENT_YEAR + 3; year++) {
expect(screen.getByTestId(`preschool-year-row-${year}`)).toBeInTheDocument()
}
expect(hooks.usePreschoolPlanningDays).toHaveBeenCalledWith('c-1', CURRENT_YEAR)
})
it('autosaves an edited planned-days value via setPlanned', async () => {
const mockSetPlanned = vi.fn().mockResolvedValue(undefined)
vi.mocked(hooks.usePreschoolPlanningDays).mockReturnValue(
makePreschoolPlanningDaysResult({ setPlanned: mockSetPlanned }),
)
renderSettings()
fireEvent.change(screen.getByTestId(`preschool-planned-${CURRENT_YEAR}`), { target: { value: '7' } })
await waitFor(() => expect(mockSetPlanned).toHaveBeenCalledWith(7))
})
it('autosaves an edited taken-days value via recordTaken', async () => {
const mockRecordTaken = vi.fn().mockResolvedValue(undefined)
vi.mocked(hooks.usePreschoolPlanningDays).mockReturnValue(
makePreschoolPlanningDaysResult({ recordTaken: mockRecordTaken }),
)
renderSettings()
fireEvent.change(screen.getByTestId(`preschool-taken-${CURRENT_YEAR}`), { target: { value: '4' } })
await waitFor(() => expect(mockRecordTaken).toHaveBeenCalledWith(4))
})
it('leaves takenDays blank (not 0) when it has never been recorded', () => {
vi.mocked(hooks.usePreschoolPlanningDays).mockReturnValue(
makePreschoolPlanningDaysResult({
data: { id: 'ppd-1', childId: 'c-1', year: CURRENT_YEAR, plannedDays: 6, takenDays: null, isDefault: false, createdAt: null, updatedAt: null },
}),
)
renderSettings()
expect((screen.getByTestId(`preschool-taken-${CURRENT_YEAR}`) as HTMLInputElement).value).toBe('')
})
})
it('mounts PartnerInvitePanel in the family section with the workspace id', () => {
renderSettings()
expect(screen.getByTestId('partner-invite-panel-stub')).toBeInTheDocument()
expect(lastPartnerInvitePanelProps).toEqual({ workspaceId: 'ws-1' })
})
it('adds a partner', async () => {
renderSettings()
fireEvent.change(screen.getByPlaceholderText('Partnerns namn'), { target: { value: 'Partner Name' } })
@@ -357,6 +492,16 @@ describe('Settings', () => {
)
})
it('describes a linked member as an unclaimed placeholder, not an already-linked account', () => {
renderSettings()
// The old copy ("Din partner är kopplad...") falsely implies an
// account link that doesn't exist for a placeholder member added via
// "Lägg till partner" — replaced by the `invite` namespace's copy,
// which correctly describes an invite as the way to connect one.
expect(screen.getByText(/Den här platsen är förberedd åt din partner/)).toBeInTheDocument()
expect(screen.queryByText('Din partner är kopplad till detta arbetsutrymme.')).not.toBeInTheDocument()
})
it('shows the linked partner and hides the add-partner form', () => {
renderSettings()
expect(screen.getByTestId('member-name-pd-2')).toHaveTextContent('Sam')