Files
maternal-app/maternal-web/lib/api/__tests__/tracking.test.ts
2025-10-01 19:01:52 +00:00

105 lines
2.8 KiB
TypeScript

import { trackingApi } from '../tracking'
import apiClient from '../client'
jest.mock('../client')
const mockedApiClient = apiClient as jest.Mocked<typeof apiClient>
describe('trackingApi', () => {
beforeEach(() => {
jest.clearAllMocks()
})
describe('createActivity', () => {
it('transforms frontend data to backend format', async () => {
const mockActivity = {
id: 'act_123',
childId: 'chd_456',
type: 'feeding',
startedAt: '2024-01-01T12:00:00Z',
metadata: { amount: 120, type: 'bottle' },
loggedBy: 'usr_789',
createdAt: '2024-01-01T12:00:00Z',
}
mockedApiClient.post.mockResolvedValue({
data: { data: { activity: mockActivity } },
} as any)
const result = await trackingApi.createActivity('chd_456', {
type: 'feeding',
timestamp: '2024-01-01T12:00:00Z',
data: { amount: 120, type: 'bottle' },
})
expect(mockedApiClient.post).toHaveBeenCalledWith(
'/api/v1/activities?childId=chd_456',
{
type: 'feeding',
startedAt: '2024-01-01T12:00:00Z',
metadata: { amount: 120, type: 'bottle' },
notes: undefined,
}
)
expect(result).toEqual({
...mockActivity,
timestamp: mockActivity.startedAt,
data: mockActivity.metadata,
})
})
})
describe('getActivities', () => {
it('transforms backend data to frontend format', async () => {
const mockActivities = [
{
id: 'act_123',
childId: 'chd_456',
type: 'feeding',
startedAt: '2024-01-01T12:00:00Z',
metadata: { amount: 120 },
},
{
id: 'act_124',
childId: 'chd_456',
type: 'sleep',
startedAt: '2024-01-01T14:00:00Z',
metadata: { duration: 120 },
},
]
mockedApiClient.get.mockResolvedValue({
data: { data: { activities: mockActivities } },
} as any)
const result = await trackingApi.getActivities('chd_456', 'feeding')
expect(mockedApiClient.get).toHaveBeenCalledWith('/api/v1/activities', {
params: { childId: 'chd_456', type: 'feeding' },
})
expect(result).toEqual([
{
id: 'act_123',
childId: 'chd_456',
type: 'feeding',
startedAt: '2024-01-01T12:00:00Z',
metadata: { amount: 120 },
timestamp: '2024-01-01T12:00:00Z',
data: { amount: 120 },
},
{
id: 'act_124',
childId: 'chd_456',
type: 'sleep',
startedAt: '2024-01-01T14:00:00Z',
metadata: { duration: 120 },
timestamp: '2024-01-01T14:00:00Z',
data: { duration: 120 },
},
])
})
})
})