Files
F-r-ldraledigplannerare/frontend/src/hooks/useAnalytics.test.ts
T
jacobh2 fbb7a04580 Phase 1: Fix TypeScript compilation — add missing API functions and fix hook signatures
- Add 8 missing API client functions (getBreakPeriods, createBreakPeriod, deleteBreakPeriod, getTips, getParentalLeave, getPlannedDays, createPlannedDay, deletePlannedDay)
- Fix useAnalytics to accept optional childId parameter
- Fix ApiError strict optional properties handling
- Export HeaderProps from Header component
- Update hook tests to use correct data structures and optional parameters

npx tsc --noEmit now passes with zero errors.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-06-01 17:47:17 +02:00

80 lines
2.2 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest'
import { renderHook, act } from '@testing-library/react'
import { useAnalytics } from './useAnalytics'
import * as api from '@/services/api'
vi.mock('@/services/api')
describe('useAnalytics', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('initializes with null data', () => {
const { result } = renderHook(() => useAnalytics(null))
expect(result.current.data).toBeNull()
expect(result.current.isLoading).toBe(false)
})
it('fetches analytics data on mount', async () => {
const mockData = {
workspaceId: 'ws1',
childId: 'child1',
parentProjections: [],
combinedDaysUsed: 10,
combinedDaysLeft: 15,
combinedExpiringAtB4: 50,
}
vi.mocked(api.getWorkspaceOverview).mockResolvedValue(mockData as any)
renderHook(() => useAnalytics('ws1', 'child1'))
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 0))
})
expect(api.getWorkspaceOverview).toHaveBeenCalledWith('ws1', 'child1')
})
it('handles fetch errors', async () => {
vi.mocked(api.getWorkspaceOverview).mockRejectedValue(new Error('API error'))
const { result } = renderHook(() => useAnalytics('ws1', 'child1'))
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 0))
})
expect(result.current.error).toContain('API error')
})
it('does not fetch when workspaceId is null', () => {
vi.mocked(api.getWorkspaceOverview).mockResolvedValue({} as any)
renderHook(() => useAnalytics(null))
expect(api.getWorkspaceOverview).not.toHaveBeenCalled()
})
it('does not fetch when childId is missing', () => {
vi.mocked(api.getWorkspaceOverview).mockResolvedValue({} as any)
renderHook(() => useAnalytics('ws1'))
expect(api.getWorkspaceOverview).not.toHaveBeenCalled()
})
it('provides refresh function', async () => {
vi.mocked(api.getWorkspaceOverview).mockResolvedValue({} as any)
const { result } = renderHook(() => useAnalytics('ws1', 'child1'))
await act(async () => {
await result.current.refresh()
})
expect(api.getWorkspaceOverview).toHaveBeenCalled()
})
})