Files
F-r-ldraledigplannerare/frontend/tests/fixtures/hooks.ts
T
jacobh2 8e1cf3524c Lead the overview with the days about to be lost
The page opened with generic tiles and a bar reading forty-eight of ninety-six
weeks, which was neither weeks nor connected to the age-four ceiling it appeared
to describe. It is replaced by one headline and a bar per parent, in days,
against each parent's own savable limit as the server reports it.

The headline has two framings on the same arithmetic. Before anything is
planned it says what there is to plan and what will be lost without a plan,
because on the first day almost the whole entitlement is technically at risk
and saying so bluntly reads as an accusation. Once a plan exists it warns about
the days genuinely at stake.

A timeline of the years to the fourth birthday answers what the next four years
look like, which a month-at-a-time calendar cannot. Shortfalls, surplus advice
and balances sit below the fold, read-only: applying is automatic now, so a
second apply button would be a way to get two different answers.

Rejection messages move to one shared place that resolves a parent's name, so
an identifier can no longer surface in their place.
2026-09-17 21:32:15 +02:00

271 lines
7.4 KiB
TypeScript

/**
* Shared test fixtures — return values of `src/context/*` and `src/hooks/*`.
*
* None of these hooks export their result interface (e.g. `useAnalytics`'s
* `UseAnalyticsResult` is private to useAnalytics.ts), and this file
* deliberately does not add exports to make that possible — `ReturnType<typeof
* theHook>` gets the same fully-typed result without touching source files.
* `import type` erases at compile time, so this doesn't pull the hooks'
* runtime code (or React) into the test bundle.
*/
import { vi } from 'vitest'
import type { useWorkspace } from '@/context/WorkspaceContext'
import type { useAuth } from '@/context/AuthContext'
import type { useAnalytics } from '@/hooks/useAnalytics'
import type { useParentalLeave } from '@/hooks/useParentalLeave'
import type { usePlannedDays } from '@/hooks/usePlannedDays'
import type { useScraperFetch } from '@/hooks/useScraperFetch'
import type { useWorkspaceMembers } from '@/hooks/useWorkspaceMembers'
import type { useOptimizationTips } from '@/hooks/useOptimizationTips'
import type { useDayOverrides } from '@/hooks/useDayOverrides'
import type { useLeaveWishes } from '@/hooks/useLeaveWishes'
import type { usePlanProposal } from '@/hooks/usePlanProposal'
import type { usePlanApply } from '@/hooks/usePlanApply'
import type { usePlanSync } from '@/hooks/usePlanSync'
import type { useVacationDays } from '@/hooks/useVacationDays'
import type { useChildSettings } from '@/hooks/useChildSettings'
import type { useCalendarDays } from '@/hooks/useCalendarDays'
import type { useShowWeekNumbers } from '@/hooks/useCalendarPreferences'
import { makeChild, makePersonalData, makeVacationDays, makeWorkspace, makeParentalLeaveFetch } from './mockData'
import { makeWorkspaceOverview } from './analytics'
import { makeCalendarWindow } from './calendar'
// ---------------------------------------------------------------------------
// Contexts
// ---------------------------------------------------------------------------
export function makeWorkspaceContextValue(
overrides: Partial<ReturnType<typeof useWorkspace>> = {},
): ReturnType<typeof useWorkspace> {
const workspace = makeWorkspace()
const child = makeChild()
const parent = makePersonalData({ uuid: 'pd-a', name: 'Förälder A' })
return {
workspace,
isLoading: false,
error: null,
workspaceId: workspace.id,
setWorkspaceId: vi.fn(),
refetch: vi.fn(),
children: [{ id: child.id, name: child.name, birth_date: child.birthDate }],
selectedChildId: child.id,
selectChild: vi.fn(),
personalDataUuid: parent.uuid,
parentName: parent.name,
refreshContext: vi.fn(),
...overrides,
}
}
export function makeAuthContextValue(
overrides: Partial<ReturnType<typeof useAuth>> = {},
): ReturnType<typeof useAuth> {
return {
accessToken: 'token-123',
isAuthenticated: true,
isLoading: false,
login: vi.fn(),
register: vi.fn(),
logout: vi.fn(),
...overrides,
}
}
// ---------------------------------------------------------------------------
// Data-fetching hooks
// ---------------------------------------------------------------------------
export function makeAnalyticsResult(
overrides: Partial<ReturnType<typeof useAnalytics>> = {},
): ReturnType<typeof useAnalytics> {
return {
data: makeWorkspaceOverview(),
isLoading: false,
error: null,
refresh: vi.fn(),
...overrides,
}
}
export function makeParentalLeaveResult(
overrides: Partial<ReturnType<typeof useParentalLeave>> = {},
): ReturnType<typeof useParentalLeave> {
return {
data: makeParentalLeaveFetch(),
isLoading: false,
error: null,
refresh: vi.fn(),
...overrides,
}
}
export function makePlannedDaysResult(
overrides: Partial<ReturnType<typeof usePlannedDays>> = {},
): ReturnType<typeof usePlannedDays> {
return {
data: [],
isLoading: false,
error: null,
refresh: vi.fn(),
add: vi.fn(),
update: vi.fn(),
remove: vi.fn(),
...overrides,
}
}
export function makeScraperFetchResult(
overrides: Partial<ReturnType<typeof useScraperFetch>> = {},
): ReturnType<typeof useScraperFetch> {
return {
sessionId: null,
pollData: null,
resolvedIds: null,
error: null,
isUnauthorized: false,
start: vi.fn(),
cancel: vi.fn(),
isRunning: false,
isTerminal: false,
...overrides,
}
}
export function makeWorkspaceMembersResult(
overrides: Partial<ReturnType<typeof useWorkspaceMembers>> = {},
): ReturnType<typeof useWorkspaceMembers> {
return {
data: [makePersonalData({ uuid: 'pd-a', name: 'Förälder A' })],
isLoading: false,
error: null,
refresh: vi.fn(),
...overrides,
}
}
export function makeOptimizationTipsResult(
overrides: Partial<ReturnType<typeof useOptimizationTips>> = {},
): ReturnType<typeof useOptimizationTips> {
return {
data: [],
isLoading: false,
error: null,
refresh: vi.fn(),
dismiss: vi.fn(),
...overrides,
}
}
export function makeDayOverridesResult(
overrides: Partial<ReturnType<typeof useDayOverrides>> = {},
): ReturnType<typeof useDayOverrides> {
return {
data: [],
isLoading: false,
error: null,
upsert: vi.fn(),
remove: vi.fn(),
...overrides,
}
}
export function makeLeaveWishesResult(
overrides: Partial<ReturnType<typeof useLeaveWishes>> = {},
): ReturnType<typeof useLeaveWishes> {
return {
data: [],
isLoading: false,
isRefreshing: false,
error: null,
refresh: vi.fn(),
add: vi.fn(),
update: vi.fn(),
remove: vi.fn(),
...overrides,
}
}
export function makePlanProposalResult(
overrides: Partial<ReturnType<typeof usePlanProposal>> = {},
): ReturnType<typeof usePlanProposal> {
return {
data: null,
isLoading: false,
error: null,
propose: vi.fn(),
...overrides,
}
}
export function makePlanApplyResult(
overrides: Partial<ReturnType<typeof usePlanApply>> = {},
): ReturnType<typeof usePlanApply> {
return {
response: null,
isLoading: false,
error: null,
apply: vi.fn(),
...overrides,
}
}
export function makePlanSyncResult(
overrides: Partial<ReturnType<typeof usePlanSync>> = {},
): ReturnType<typeof usePlanSync> {
return {
isSyncing: false,
error: null,
lastChanges: null,
sync: vi.fn(),
...overrides,
}
}
export function makeVacationDaysResult(
overrides: Partial<ReturnType<typeof useVacationDays>> = {},
): ReturnType<typeof useVacationDays> {
return {
data: [makeVacationDays()],
isLoading: false,
error: null,
upsert: vi.fn(),
...overrides,
}
}
export function makeChildSettingsResult(
overrides: Partial<ReturnType<typeof useChildSettings>> = {},
): ReturnType<typeof useChildSettings> {
return {
data: makeChild(),
isLoading: false,
error: null,
update: vi.fn(),
...overrides,
}
}
export function makeCalendarDaysResult(
overrides: Partial<ReturnType<typeof useCalendarDays>> = {},
): ReturnType<typeof useCalendarDays> {
return {
data: makeCalendarWindow(),
isLoading: false,
error: null,
refetch: vi.fn(),
...overrides,
}
}
// ---------------------------------------------------------------------------
// Local-storage-backed preference hook (a tuple, not an object)
// ---------------------------------------------------------------------------
export function makeShowWeekNumbersTuple(
show = false,
setShow: (show: boolean) => void = vi.fn(),
): ReturnType<typeof useShowWeekNumbers> {
return [show, setShow]
}