import { describe, it, expect, vi, beforeEach } from 'vitest' import { useSports } from '../use-sports' import { IFrameGameURLEnum, ResponseStatus, SportScheduleFilterDay, } from '#lib/enums' import { SPORTS_LIMIT } from '#lib/constants' import { useApiFetch } from '#lib/composables/service/use-api-fetch' import { useAsyncData } from 'nuxt/app' // Mock dependencies const mockGames = { playGame: vi.fn(), openModalMaintainAthenaGames: vi.fn(), } const mockNavigate = { navigate: vi.fn(), } const mockSportsService = { getFootballSchedulesService: vi.fn(), } const mockCheckGame = { isMaintainAthenaGames: vi.fn(), } const mockNuxtApp = { $media: { hotMatchMedia: vi.fn(), }, } vi.mock('#lib/composables/game', () => ({ useGames: () => mockGames, useCheckGame: () => mockCheckGame, })) vi.mock('#lib/composables/common/use-navigate', () => ({ useNavigate: () => mockNavigate, })) vi.mock('#lib/composables/service/use-sports-service', () => ({ useSportsService: () => mockSportsService, })) vi.mock('nuxt/app', () => ({ useNuxtApp: () => mockNuxtApp, useCookie: vi.fn(), useRuntimeConfig: vi.fn().mockReturnValue({ public: { API_URL: '/api', }, }), useNuxtData: vi.fn().mockReturnValue({ data: [], }), useAsyncData: vi.fn().mockReturnValue({}), })) vi.mock('#lib/composables/service/use-api-fetch', () => ({ useApiFetch: vi.fn().mockReturnValue({ request: vi.fn(), }), })) vi.mock('#lib/utils', () => ({ checkIsToday: vi.fn(), checkIsTomorrow: vi.fn(), checkTimeUpcoming: vi.fn(), })) vi.mock('#imports', () => ({ persistedState: vi.fn().mockReturnValue({ localStorage: {}, }), })) describe('useSports composable', () => { let request: any beforeEach(() => { vi.clearAllMocks() request = useApiFetch().request }) it('should play a sport game when playSport is called', () => { const { playSport } = useSports() const item = { link: 'some-link', isMaintain: false } playSport(item) expect(mockNavigate.navigate).toHaveBeenCalledWith( 'some-link', undefined, undefined, ) }) it('should not play a sport game if isMaintain is true', () => { const { playSport } = useSports() const item = { link: 'some-link', isMaintain: true } playSport(item) expect(mockNavigate.navigate).not.toHaveBeenCalled() expect(mockGames.playGame).not.toHaveBeenCalled() }) it('should fetch and set football schedules', async () => { const mockSchedules = { 'League 1': [ { match_id: 1, league_name: 'League 1', league_image: 'image1.png' }, { match_id: 2, league_name: 'League 1', league_image: 'image1.png' }, ], 'League 2': [ { match_id: 3, league_name: 'League 2', league_image: 'image2.png' }, ], } mockSportsService.getFootballSchedulesService.mockResolvedValue({ data: { value: { status: ResponseStatus.OK, data: mockSchedules } }, }) const { setScheduleMatches, scheduleSports, totalSchedule } = useSports() await setScheduleMatches() expect(mockSportsService.getFootballSchedulesService).toHaveBeenCalled() expect(scheduleSports.value.length).toBeGreaterThan(0) expect(totalSchedule.value).toBe(2) }) it('should play K-Sport game when playKSportGame is called', () => { const { playKSportGame } = useSports() const hotmatchItem = { match_id: '123', league_id: '456', isDisable: false } playKSportGame(hotmatchItem) expect(mockNavigate.navigate).toHaveBeenCalledWith( `${IFrameGameURLEnum.K_SPORTS}?eventId=123&leagueId=456`, undefined, undefined, ) }) it('should not play K-Sport game if the item is disabled', () => { const { playKSportGame } = useSports() const hotmatchItem = { match_id: '123', league_id: '456', isDisable: true } playKSportGame(hotmatchItem) expect(mockNavigate.navigate).not.toHaveBeenCalled() }) it('should open modal when A-Sport game is maintained', () => { const { playASportGame } = useSports() mockCheckGame.isMaintainAthenaGames.mockReturnValue(true) const hotmatchItem = { athena_match_id: '123', athena_league_id: '456' } playASportGame(hotmatchItem, false) expect(mockGames.openModalMaintainAthenaGames).toHaveBeenCalled() }) it('should navigate to A-Sport game if not maintained', () => { const { playASportGame } = useSports() mockCheckGame.isMaintainAthenaGames.mockReturnValue(false) const hotmatchItem = { athena_match_id: '123', athena_league_id: '456' } playASportGame(hotmatchItem, false) expect(mockNavigate.navigate).toHaveBeenCalledWith( `${IFrameGameURLEnum.C_SPORTS}?mid=123`, false, false, ) }) it('should filter and return schedules by league slug', async () => { const mockSchedules = { 'League 1': [ { match_id: 1, league_name: 'League 1', league_image: 'image1.png', slug: 'league-1', }, { match_id: 2, league_name: 'League 1', league_image: 'image1.png', slug: 'league-1', }, ], 'League 2': [ { match_id: 3, league_name: 'League 2', league_image: 'image2.png', slug: 'league-2', }, ], } mockSportsService.getFootballSchedulesService.mockResolvedValue({ data: { value: { status: ResponseStatus.OK, data: mockSchedules } }, }) const { changeLeagueName, scheduleSports, setScheduleMatches } = useSports() await setScheduleMatches() const originalData = scheduleSports.value const leagueSlug = 'league-1' changeLeagueName(leagueSlug) expect(scheduleSports.value).toEqual( originalData.filter((match) => match.slug === leagueSlug), ) }) it('should load more schedules when onLoadMoreSchedules is called', async () => { const { onLoadMoreSchedules, scheduleSports, setScheduleMatches } = useSports() const mockSchedules = { 'League 1': [ { match_id: 1, league_name: 'League 1', league_image: 'image1.png' }, { match_id: 2, league_name: 'League 1', league_image: 'image1.png' }, ], 'League 2': [ { match_id: 3, league_name: 'League 2', league_image: 'image2.png' }, { match_id: 4, league_name: 'League 2', league_image: 'image2.png' }, { match_id: 5, league_name: 'League 2', league_image: 'image2.png' }, ], 'League 3': [ { match_id: 3, league_name: 'League 3', league_image: 'image2.png' }, { match_id: 4, league_name: 'League 3', league_image: 'image2.png' }, { match_id: 5, league_name: 'League 3', league_image: 'image2.png' }, ], 'League 4': [ { match_id: 3, league_name: 'League 4', league_image: 'image2.png' }, { match_id: 4, league_name: 'League 4', league_image: 'image2.png' }, { match_id: 5, league_name: 'League 4', league_image: 'image2.png' }, ], } mockSportsService.getFootballSchedulesService.mockResolvedValue({ data: { value: { status: ResponseStatus.OK, data: mockSchedules } }, }) await setScheduleMatches() expect(scheduleSports.value.length).toBe(3) onLoadMoreSchedules() expect(scheduleSports.value.length).toBeGreaterThan(SPORTS_LIMIT) }) it('should fetch hot matches data', async () => { const { fetchHotmatches, hotmatches } = useSports() await fetchHotmatches() expect(hotmatches.value).toEqual(undefined) }) it('should change filter day and refresh schedules', () => { const { changeFilterDay, currentFilterDay, scheduleSports } = useSports() const matchesData = [ { leagueName: 'League 1', matches: [{ text_time: '2024-01-01' }] }, ] scheduleSports.value = matchesData changeFilterDay(SportScheduleFilterDay.TODAY) expect(currentFilterDay.value).toBe(SportScheduleFilterDay.TODAY) expect(scheduleSports.value.length).toBe(0) }) })