Initial commit
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
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 = {
|
||||
workspace_id: 'ws1',
|
||||
total_days: 25,
|
||||
days_used: 10,
|
||||
days_left: 15,
|
||||
projected_left_at_age_4: 50,
|
||||
}
|
||||
|
||||
vi.mocked(api.getWorkspaceOverview).mockResolvedValue(mockData as any)
|
||||
|
||||
const { result } = renderHook(() => useAnalytics('ws1'))
|
||||
|
||||
await act(async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 0))
|
||||
})
|
||||
|
||||
expect(api.getWorkspaceOverview).toHaveBeenCalledWith('ws1')
|
||||
})
|
||||
|
||||
it('handles fetch errors', async () => {
|
||||
vi.mocked(api.getWorkspaceOverview).mockRejectedValue(new Error('API error'))
|
||||
|
||||
const { result } = renderHook(() => useAnalytics('ws1'))
|
||||
|
||||
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('provides refresh function', async () => {
|
||||
vi.mocked(api.getWorkspaceOverview).mockResolvedValue({} as any)
|
||||
|
||||
const { result } = renderHook(() => useAnalytics('ws1'))
|
||||
|
||||
await act(async () => {
|
||||
await result.current.refresh()
|
||||
})
|
||||
|
||||
expect(api.getWorkspaceOverview).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user