Require a planned day to carry its wish and pinned state

Both fields were made optional to avoid editing test files that were out of
scope at the time, but the API populates them on every planned day, so the
optionality described nothing real and quietly loosened the contract between
the wire format and the client — which is where this codebase has historically
gone wrong.

The inline objects that forced the compromise now go through the typed test
factory instead, so the next field added to a planned day is a one-line change
rather than another round of this.
This commit is contained in:
2026-09-17 18:33:30 +02:00
parent 6a7c0ef255
commit c368042c7e
4 changed files with 45 additions and 159 deletions
@@ -2,7 +2,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { PlannedDayModal } from './PlannedDayModal'
import type { PlannedLeaveDay } from '@/types'
import { makePlannedLeaveDay } from '@/../tests/fixtures'
describe('PlannedDayModal', () => {
const mockOnClose = vi.fn()
@@ -49,17 +49,10 @@ describe('PlannedDayModal', () => {
})
it('shows "Redigera planerad dag" title when editing existing day', () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(<PlannedDayModal {...defaultProps} isOpen={true} existingDay={existingDay} />)
expect(screen.getByTestId('modal-title')).toHaveTextContent('Redigera planerad dag')
})
@@ -430,17 +423,10 @@ describe('PlannedDayModal', () => {
})
it('displays local error after delete failure', async () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
mockOnDelete.mockRejectedValue(new Error('Delete failed'))
// Mock the confirm dialog
@@ -464,17 +450,10 @@ describe('PlannedDayModal', () => {
})
it('displays a generic error message for non-Error thrown in delete', async () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
mockOnDelete.mockRejectedValue('Some error')
vi.spyOn(window, 'confirm').mockReturnValue(true)
@@ -521,17 +500,12 @@ describe('PlannedDayModal', () => {
describe('existing day initialization', () => {
it('initializes day type from existing day', async () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p2',
date: '2026-06-15',
dayType: 'sjukpenning',
isDubbeldag: true,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(
<PlannedDayModal
@@ -552,17 +526,11 @@ describe('PlannedDayModal', () => {
})
it('initializes parent from existing day', async () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p2',
date: '2026-06-15',
dayType: 'sjukpenning',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(
<PlannedDayModal
@@ -583,17 +551,10 @@ describe('PlannedDayModal', () => {
})
it('shows correct title when editing existing day', () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(
<PlannedDayModal
@@ -607,29 +568,17 @@ describe('PlannedDayModal', () => {
})
it('reinitializes when existingDay changes', async () => {
const firstDay: PlannedLeaveDay = {
const firstDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
const secondDay: PlannedLeaveDay = {
const secondDay = makePlannedLeaveDay({
id: 'day-2',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'lagsta',
isDubbeldag: true,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
const { rerender } = render(
<PlannedDayModal
@@ -709,17 +658,10 @@ describe('PlannedDayModal', () => {
describe('delete button functionality', () => {
it('does not render delete button when onDelete not provided', () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(
<PlannedDayModal
@@ -733,17 +675,10 @@ describe('PlannedDayModal', () => {
})
it('does not render delete button in range mode', () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(
<PlannedDayModal
@@ -759,17 +694,10 @@ describe('PlannedDayModal', () => {
})
it('renders delete button when editing existing day with delete handler', () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(
<PlannedDayModal
@@ -784,17 +712,10 @@ describe('PlannedDayModal', () => {
})
it('shows confirmation dialog before deleting', async () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true)
mockOnDelete.mockResolvedValue(undefined)
@@ -816,17 +737,10 @@ describe('PlannedDayModal', () => {
})
it('does not call onDelete if confirmation is cancelled', async () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(false)
mockOnDelete.mockResolvedValue(undefined)
@@ -848,17 +762,10 @@ describe('PlannedDayModal', () => {
})
it('calls onDelete if confirmation is accepted', async () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true)
mockOnDelete.mockResolvedValue(undefined)
@@ -882,17 +789,10 @@ describe('PlannedDayModal', () => {
})
it('disables delete button when saving', () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(
<PlannedDayModal
@@ -909,17 +809,10 @@ describe('PlannedDayModal', () => {
})
it('shows loading text on delete button when saving', () => {
const existingDay: PlannedLeaveDay = {
const existingDay = makePlannedLeaveDay({
id: 'day-1',
workspaceId: 'ws-1',
childId: 'c-1',
parentPersonalDataUuid: 'p1',
date: '2026-06-15',
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
}
})
render(
<PlannedDayModal
+3 -8
View File
@@ -10,6 +10,7 @@ import {
makeCalendarDaysResult,
makeDayOverridesResult,
makeShowWeekNumbersTuple,
makePlannedLeaveDay,
} from '../../tests/fixtures'
import { makeCalendarDayEntry, makeCalendarWindow } from '../../tests/fixtures/calendar'
@@ -791,17 +792,11 @@ describe('Calendar', () => {
vi.mocked(usePlannedDays).mockReturnValue(
makePlannedDaysResult({
data: [
{
makePlannedLeaveDay({
id: 'pd-1',
workspaceId: 'ws-1',
childId: 'child-1',
parentPersonalDataUuid: 'person-1',
date: clickedDate,
dayType: 'vacation',
isDubbeldag: false,
createdAt: '2026-01-01',
updatedAt: '2026-01-01',
},
}),
],
})
)
+6 -5
View File
@@ -13,6 +13,7 @@ import {
makePlannedDaysResult,
makeScraperFetchResult,
makeWorkspaceMembersResult,
makePlannedLeaveDay,
} from '../../tests/fixtures'
vi.mock('@/hooks')
@@ -88,8 +89,8 @@ describe('Dashboard', () => {
vi.mocked(hooks.usePlannedDays).mockReturnValue(
makePlannedDaysResult({
data: [
{ id: '1', workspaceId: 'ws-1', childId: 'c-1', parentPersonalDataUuid: 'pd-1', date: '2026-06-15', dayType: 'vacation', isDubbeldag: false, createdAt: '', updatedAt: '' },
{ id: '2', workspaceId: 'ws-1', childId: 'c-1', parentPersonalDataUuid: 'pd-1', date: '2026-06-16', dayType: 'sjukpenning', isDubbeldag: false, createdAt: '', updatedAt: '' },
makePlannedLeaveDay({ id: '1', date: '2026-06-15' }),
makePlannedLeaveDay({ id: '2', date: '2026-06-16', dayType: 'sjukpenning' }),
],
})
)
@@ -445,9 +446,9 @@ describe('Dashboard', () => {
vi.mocked(hooks.usePlannedDays).mockReturnValue(
makePlannedDaysResult({
data: [
{ id: '1', workspaceId: 'ws-1', childId: 'c-1', parentPersonalDataUuid: 'pd-1', date: '2026-06-15', dayType: 'vacation', isDubbeldag: false, createdAt: '', updatedAt: '' },
{ id: '2', workspaceId: 'ws-1', childId: 'c-1', parentPersonalDataUuid: 'pd-1', date: '2026-06-16', dayType: 'sjukpenning', isDubbeldag: false, createdAt: '', updatedAt: '' },
{ id: '3', workspaceId: 'ws-1', childId: 'c-1', parentPersonalDataUuid: 'pd-1', date: '2026-06-17', dayType: 'lagsta', isDubbeldag: false, createdAt: '', updatedAt: '' },
makePlannedLeaveDay({ id: '1', date: '2026-06-15' }),
makePlannedLeaveDay({ id: '2', date: '2026-06-16', dayType: 'sjukpenning' }),
makePlannedLeaveDay({ id: '3', date: '2026-06-17', dayType: 'lagsta' }),
],
})
)
+3 -6
View File
@@ -105,13 +105,10 @@ export interface PlannedLeaveDay {
dayType: 'sjukpenning' | 'lagsta' | 'vacation'
isDubbeldag: boolean
// Which `LeaveWish` produced this day; null/omitted means ad-hoc (not tied to a wish).
// Optional (rather than required) so existing object literals elsewhere in the
// codebase that predate this field keep type-checking; `toPlannedLeaveDay` in
// services/api.ts always populates it from the wire response.
wishId?: string | null
// `toPlannedLeaveDay` in services/api.ts always populates it from the wire response.
wishId: string | null
// True when the user hand-edited this day's type — the planner must not retype it.
// Optional for the same reason as `wishId` above.
isPinned?: boolean
isPinned: boolean
createdAt: string
updatedAt: string
}