import { describe, it, expect, vi } from 'vitest' import { useSportsService } from '../use-sports-service' import { useAPI } from '#lib/composables' import { SPORTS } from '#lib/configs/api' import type { MatchData } from '#lib/types' import type { BaseResponse } from '#lib/types/api' // Mock `useAPI` composable vi.mock('#lib/composables', () => ({ useAPI: vi.fn(), })) describe('useSportsService', () => { it('should call useAPI with the correct endpoint for football schedules', async () => { const mockResponse: BaseResponse> = { success: true, data: { '2024-08-20': [ { matchId: '123', teamA: 'Team A', teamB: 'Team B', matchTime: '2024-08-20T15:00:00Z', }, ], }, } // Mock the return value of useAPI vi.mocked(useAPI).mockResolvedValue(mockResponse) const { getFootballSchedulesService } = useSportsService() const response = await getFootballSchedulesService() // Assert that useAPI was called with the correct endpoint expect(useAPI).toHaveBeenCalledWith(SPORTS.FOOTBALL_SCHEDULE) // Assert that the response is as expected expect(response).toEqual(mockResponse) }) it('should handle API errors gracefully', async () => { const mockError = new Error('API Error') // Mock the return value of useAPI to reject with an error vi.mocked(useAPI).mockRejectedValue(mockError) const { getFootballSchedulesService } = useSportsService() await expect(getFootballSchedulesService()).rejects.toThrow('API Error') }) })