40227-vm/frontend/src/shared/api/zoneCheckins.test.ts
2026-06-12 10:56:13 +02:00

78 lines
2.2 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
checkInZone,
clearTodayZoneCheckin,
getTodayZoneCheckin,
listZoneCheckinHistory,
} from '@/shared/api/zoneCheckins';
import { apiRequest } from '@/shared/api/httpClient';
import type { ZoneCheckinTodayDto } from '@/shared/types/zoneCheckins';
vi.mock('@/shared/api/httpClient', () => ({
apiRequest: vi.fn(),
}));
const apiRequestMock = vi.mocked(apiRequest);
describe('zoneCheckins API', () => {
beforeEach(() => {
apiRequestMock.mockReset();
});
it('fetches today zone check-in status', async () => {
const todayCheckin: ZoneCheckinTodayDto = {
zone: 'green',
checkedInAt: '2026-06-12T10:00:00Z',
};
apiRequestMock.mockResolvedValueOnce(todayCheckin);
await expect(getTodayZoneCheckin()).resolves.toEqual(todayCheckin);
expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins/today');
});
it('checks in to a zone', async () => {
const todayCheckin: ZoneCheckinTodayDto = {
zone: 'blue',
checkedInAt: '2026-06-12T10:30:00Z',
};
apiRequestMock.mockResolvedValueOnce(todayCheckin);
await expect(checkInZone('blue')).resolves.toEqual(todayCheckin);
expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins', {
method: 'POST',
body: { data: { zone: 'blue' } },
});
});
it('clears today zone check-in', async () => {
const clearedCheckin: ZoneCheckinTodayDto = {
zone: null,
checkedInAt: null,
};
apiRequestMock.mockResolvedValueOnce(clearedCheckin);
await expect(clearTodayZoneCheckin()).resolves.toEqual(clearedCheckin);
expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins/today', {
method: 'DELETE',
});
});
it('lists zone check-in history', async () => {
const historyResponse = {
rows: [
{ id: '1', zone: 'green', date: '2026-06-11' },
{ id: '2', zone: 'yellow', date: '2026-06-10' },
],
count: 2,
};
apiRequestMock.mockResolvedValueOnce(historyResponse);
await expect(listZoneCheckinHistory()).resolves.toEqual(historyResponse);
expect(apiRequestMock).toHaveBeenCalledWith('/zone_checkins');
});
});