76b1e712b9
Works through every numbered item in NextSteps260913.md section 3, plus the three known verification issues in section 4.1. Surface what the backend already computed: - 3.1 render surplus_advice in PlanResults, mirroring the shortfall remedies - 3.2 surface VacationBalance.is_assumed so assumed statutory years are visible - 3.3 age-4 expiry given the proposed plan, via a new Age4Projection reusing the existing _age4_projections arithmetic (Dashboard stat verified, not rebuilt) Calendar cluster (one pass over the holiday data): - new GET /api/calendar/days exposing roda dagar, klamdagar and day overrides, paginated four years at a time - 3.4 shade roda dagar/klamdagar, with R/K badges so colour is not the only signal - 3.6 click-to-override behind an explicit mode toggle, leaving drag multi-select intact - 3.7 click month/year to advance - 3.8 per-year red-days list in Settings, statutory days plus overrides i18n: - 3.5 five backend-generated Swedish surfaces converted to message_key + data, following the tips_service pattern; old string fields replaced, not kept Flexible wishes (3.9): - PLANNING_ENGINE_SPEC section 10: window_mode, candidate search, cost function, deterministic tie-break, two-pass ordering, test matrix rows 15-24 - backend: entity, migration 006, _FULL_SCHEMA sync, repository, allocator - frontend: season-preset toggle in WishEditor, unplaced outcome rendering Fixes (4.1): - pin typecheck to the local tsc rather than npx resolving a newer TypeScript - ignore generated coverage/ and dist/ in the ESLint flat config - fix the flaky usePlanApply test properly, with waitFor rather than a retry Tests: backend 361 -> 404, frontend 540 -> 704. Coverage 89.47% -> 96.52% lines. Not yet verified in a browser. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
158 lines
4.5 KiB
TypeScript
158 lines
4.5 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import { renderHook, act } from '@testing-library/react'
|
|
import {
|
|
useShowWeekNumbers,
|
|
getLastLeaveType,
|
|
setLastLeaveType,
|
|
} from './useCalendarPreferences'
|
|
|
|
const SHOW_WEEK_NUMBERS_KEY = 'calendar_show_week_numbers'
|
|
const LAST_LEAVE_TYPE_KEY = 'calendar_last_leave_type'
|
|
|
|
// Node's own experimental `localStorage`/`sessionStorage` globals (active in
|
|
// newer Node runtimes without a valid --localstorage-file) shadow jsdom's
|
|
// working implementation and throw on every call. Stub a real in-memory
|
|
// Storage for these tests, matching the idiom used in WorkspaceContext.test.tsx.
|
|
function createStorageMock(): Storage {
|
|
let store: Record<string, string> = {}
|
|
return {
|
|
getItem: (key: string) => (key in store ? store[key]! : null),
|
|
setItem: (key: string, value: string) => {
|
|
store[key] = value
|
|
},
|
|
removeItem: (key: string) => {
|
|
delete store[key]
|
|
},
|
|
clear: () => {
|
|
store = {}
|
|
},
|
|
key: (index: number) => Object.keys(store)[index] ?? null,
|
|
get length() {
|
|
return Object.keys(store).length
|
|
},
|
|
}
|
|
}
|
|
|
|
describe('useShowWeekNumbers', () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal('localStorage', createStorageMock())
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it('defaults to false when nothing is stored', () => {
|
|
const { result } = renderHook(() => useShowWeekNumbers())
|
|
expect(result.current[0]).toBe(false)
|
|
})
|
|
|
|
it('reads the persisted value on mount', () => {
|
|
localStorage.setItem(SHOW_WEEK_NUMBERS_KEY, 'true')
|
|
const { result } = renderHook(() => useShowWeekNumbers())
|
|
expect(result.current[0]).toBe(true)
|
|
})
|
|
|
|
it('treats any non-"true" stored value as false', () => {
|
|
localStorage.setItem(SHOW_WEEK_NUMBERS_KEY, 'garbage')
|
|
const { result } = renderHook(() => useShowWeekNumbers())
|
|
expect(result.current[0]).toBe(false)
|
|
})
|
|
|
|
it('persists the new value and updates state when the setter is called', () => {
|
|
const { result } = renderHook(() => useShowWeekNumbers())
|
|
|
|
act(() => {
|
|
result.current[1](true)
|
|
})
|
|
|
|
expect(localStorage.getItem(SHOW_WEEK_NUMBERS_KEY)).toBe('true')
|
|
expect(result.current[0]).toBe(true)
|
|
|
|
act(() => {
|
|
result.current[1](false)
|
|
})
|
|
|
|
expect(localStorage.getItem(SHOW_WEEK_NUMBERS_KEY)).toBe('false')
|
|
expect(result.current[0]).toBe(false)
|
|
})
|
|
|
|
it('syncs across instances via the custom preferences-changed event', () => {
|
|
const { result: a } = renderHook(() => useShowWeekNumbers())
|
|
const { result: b } = renderHook(() => useShowWeekNumbers())
|
|
|
|
act(() => {
|
|
a.current[1](true)
|
|
})
|
|
|
|
expect(b.current[0]).toBe(true)
|
|
})
|
|
|
|
it('syncs across tabs via the native storage event', () => {
|
|
const { result } = renderHook(() => useShowWeekNumbers())
|
|
|
|
act(() => {
|
|
window.dispatchEvent(
|
|
new StorageEvent('storage', { key: SHOW_WEEK_NUMBERS_KEY, newValue: 'true' }),
|
|
)
|
|
})
|
|
|
|
expect(result.current[0]).toBe(true)
|
|
})
|
|
|
|
it('ignores storage events for unrelated keys', () => {
|
|
const { result } = renderHook(() => useShowWeekNumbers())
|
|
|
|
act(() => {
|
|
window.dispatchEvent(
|
|
new StorageEvent('storage', { key: 'some_other_key', newValue: 'true' }),
|
|
)
|
|
})
|
|
|
|
expect(result.current[0]).toBe(false)
|
|
})
|
|
|
|
it('removes its listeners on unmount', () => {
|
|
const removeSpy = vi.spyOn(window, 'removeEventListener')
|
|
const { unmount } = renderHook(() => useShowWeekNumbers())
|
|
|
|
unmount()
|
|
|
|
expect(removeSpy).toHaveBeenCalledWith('storage', expect.any(Function))
|
|
expect(removeSpy).toHaveBeenCalledWith('calendar:preferences-changed', expect.any(Function))
|
|
removeSpy.mockRestore()
|
|
})
|
|
})
|
|
|
|
describe('getLastLeaveType / setLastLeaveType', () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal('sessionStorage', createStorageMock())
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it('returns null when nothing is stored', () => {
|
|
expect(getLastLeaveType()).toBeNull()
|
|
})
|
|
|
|
it('returns null when the stored value is not a valid leave type', () => {
|
|
sessionStorage.setItem(LAST_LEAVE_TYPE_KEY, 'not-a-real-type')
|
|
expect(getLastLeaveType()).toBeNull()
|
|
})
|
|
|
|
it('round-trips a valid leave type', () => {
|
|
setLastLeaveType('vacation')
|
|
expect(sessionStorage.getItem(LAST_LEAVE_TYPE_KEY)).toBe('vacation')
|
|
expect(getLastLeaveType()).toBe('vacation')
|
|
})
|
|
|
|
it('accepts every valid leave type', () => {
|
|
for (const type of ['sjukpenning', 'lagsta', 'vacation'] as const) {
|
|
setLastLeaveType(type)
|
|
expect(getLastLeaveType()).toBe(type)
|
|
}
|
|
})
|
|
})
|