import { describe, it, expect, vi, beforeEach } from 'vitest' import { useMaintain } from '../use-maintain' import { useNuxtApp } from 'nuxt/app' import { useMaintenanceGamesStore } from '#lib/stores' import { formatDate } from '#lib/utils' import { DATE_TIME_FORMAT } from '#lib/constants' const mockNadal = { site: { maintenance: vi.fn(), }, } vi.mock('nuxt/app', () => ({ useNuxtApp: vi.fn().mockReturnValue({ $site: { maintenance: vi.fn(), }, }), })) vi.mock('#lib/stores', () => ({ useMaintenanceGamesStore: vi.fn().mockReturnValue({ setMaintenanceGames: vi.fn(), }), })) vi.mock('#lib/utils', () => ({ formatDate: vi.fn().mockImplementation((date) => date), })) vi.mock('#lib/composables', () => ({ useNadal: () => mockNadal, })) describe('useMaintain', () => { let maintainStore: any beforeEach(() => { maintainStore = useMaintenanceGamesStore() vi.clearAllMocks() }) it('should initialize with default values', () => { const { isLoading } = useMaintain() expect(isLoading.value).toBe(false) }) it('should fetch maintenance games and update store', async () => { const { getMaintenanceGames, isLoading } = useMaintain() const mockData = [ { start_time: '2023-01-01T00:00:00Z', end_time: '2023-01-01T12:00:00Z', enable: true, }, ] mockNadal.site.maintenance.mockResolvedValue(mockData) await getMaintenanceGames() expect(isLoading.value).toBe(false) expect(formatDate).toHaveBeenCalledWith( mockData[0].start_time, DATE_TIME_FORMAT.DATE_FORMAT_MAINTAIN, ) expect(formatDate).toHaveBeenCalledWith( mockData[0].end_time, DATE_TIME_FORMAT.DATE_FORMAT_MAINTAIN, ) expect(maintainStore.setMaintenanceGames).toHaveBeenCalledWith( mockData[0].enable, mockData[0].start_time, mockData[0].end_time, ) }) it('should handle empty maintenance data', async () => { const { getMaintenanceGames, isLoading } = useMaintain() mockNadal.site.maintenance.mockResolvedValue([]) await getMaintenanceGames() expect(isLoading.value).toBe(false) expect(maintainStore.setMaintenanceGames).not.toHaveBeenCalled() }) it('should handle errors and reset loading state', async () => { const { getMaintenanceGames, isLoading } = useMaintain() mockNadal.site.maintenance.mockRejectedValue(new Error('API Error')) await expect(getMaintenanceGames()).rejects.toThrow('API Error') expect(isLoading.value).toBe(false) expect(maintainStore.setMaintenanceGames).not.toHaveBeenCalled() }) })