import { describe, it, expect, vi, beforeEach, beforeAll } from 'vitest' import { useGames } from '../use-games' import { GameProviderEnum, GameTypeEnum } from '#lib/enums' import { GAME_API } from '#lib/configs/api' const mockGameToast = { openModalMaintainAthenaGames: vi.fn(), onShowMaintainGame: vi.fn(), openMaintainProvider: vi.fn(), } const mockGameURL = { getGameUrlIframe: vi.fn(), getGameTypeUrl: vi.fn(), } const mockGameService = { getPGSoftGameHTML: vi.fn(), } const mockCheckGame = { checkCommingSoon: vi.fn(), checkMaintainProvider: vi.fn(), checkMaintainAthenaGames: vi.fn(), checkLogin: vi.fn(), checkPromotion: vi.fn(), checkBalance: vi.fn(), checkConditionsOfGame: vi.fn(), } const mockDevice = { isMobile: true, isSafari: false, } const mockRoute = { fullPath: '/test-path', params: {}, query: {}, } const mockRuntimeConfig = { public: { SITE_DOMAIN: 'https://example.com', }, } const mockNadal = { game: { launch: vi.fn(), }, } // Mock the required composables and utilities vi.mock('nuxt/app', () => ({ useRoute: () => mockRoute, useRuntimeConfig: () => mockRuntimeConfig, })) vi.mock('#lib/composables', () => ({ useGameToast: () => mockGameToast, useCheckGame: () => mockCheckGame, useGameURL: () => mockGameURL, useGameService: () => mockGameService, useAuth: () => vi.fn().mockReturnValue({ isOpenAuthentication: null, }), useNadal: () => mockNadal, })) vi.mock('#imports', () => ({ useDevice: () => mockDevice, })) describe('useGames', () => { beforeAll(() => { vi.spyOn(window, 'open') delete (window as any).location window.location = { href: '', assign: vi.fn() } as any }) beforeEach(() => { vi.clearAllMocks() }) it('should play PGSoft game when gameType is PG_URL', async () => { const { playGame } = useGames() const mockGame = { gameId: 'pgsoft-game-id', gameType: GameTypeEnum.PG_URL, } mockCheckGame.checkCommingSoon.mockReturnValue(false) mockCheckGame.checkMaintainProvider.mockReturnValue(false) mockCheckGame.checkMaintainAthenaGames.mockReturnValue(false) mockCheckGame.checkLogin.mockReturnValue(false) mockCheckGame.checkPromotion.mockReturnValue(false) mockCheckGame.checkBalance.mockReturnValue(false) mockGameService.getPGSoftGameHTML.mockReturnValue({ value: 'fake html', }) window.open = vi.fn().mockReturnValue(null) await playGame(mockGame) expect(mockCheckGame.checkCommingSoon).toHaveBeenCalledWith(mockGame) expect(mockCheckGame.checkMaintainProvider).toHaveBeenCalledWith(mockGame) expect(mockCheckGame.checkMaintainAthenaGames).toHaveBeenCalledWith(mockGame) expect(mockCheckGame.checkLogin).toHaveBeenCalledWith(mockGame) expect(mockCheckGame.checkPromotion).toHaveBeenCalledWith(mockGame) expect(mockCheckGame.checkBalance).toHaveBeenCalledWith(mockGame) expect(mockGameService.getPGSoftGameHTML).toHaveBeenCalledWith({ url_type: 'game-entry', path: '/pgsoft-game-id/index.html', extra_args: { btt: 1, l: 'vi' }, }) }) it('should play iframe game if getGameUrlIframe returns a URL', async () => { const { playGame } = useGames() vi.mocked(mockCheckGame.checkConditionsOfGame).mockReturnValue(false) vi.mocked(mockGameService.getPGSoftGameHTML).mockReturnValue(false) mockGameURL.getGameUrlIframe.mockReturnValue('/iframe-game-url') const mockGame = { gameUrl: '/iframe-game-url', gameId: 'iframe-game-id', provider: 'SOME_PROVIDER', gameType: 'IFRAME', } window.open = vi.fn().mockReturnValue(null) await playGame(mockGame) expect(mockGameURL.getGameUrlIframe).toHaveBeenCalledWith( '/iframe-game-url', ) expect(window.open).toHaveBeenCalledWith('/iframe-game-url', '_blank') }) it('should open the game in a new tab for specific providers', async () => { const { playGame } = useGames() mockGameURL.getGameUrlIframe.mockReturnValue(false) mockGameURL.getGameTypeUrl.mockReturnValue('/new-tab-game-url') const mockGame = { gameUrl: '/new-tab-game-url', gameId: 'new-tab-game-id', provider: GameProviderEnum.SPRIBE, gameType: 'SOME_TYPE', newTab: true, } window.open = vi.fn().mockReturnValue(null) await playGame(mockGame) expect(window.open).toHaveBeenCalledWith('/new-tab-game-url', '_blank') }) it('should handle case where game cannot be played', async () => { const { playGame } = useGames() vi.mocked(mockCheckGame.checkConditionsOfGame).mockReturnValue(false) vi.mocked(mockGameService.getPGSoftGameHTML).mockReturnValue(false) mockGameURL.getGameUrlIframe.mockReturnValue('') mockGameURL.getGameTypeUrl.mockReturnValue('') const mockGame = { gameUrl: '', gameId: 'some-game-id', provider: 'SOME_PROVIDER', } window.open = vi.fn().mockReturnValue(null) mockNadal.game.launch.mockResolvedValue({ url: '' }) await playGame(mockGame) expect(mockGameToast.onShowMaintainGame).toHaveBeenCalled() }) it('should handle opening a game with a constructed URL', async () => { const { playGame } = useGames() const mockGame = { gameId: 'game-id', provider: 'PLAYNGO', } mockNadal.game.launch.mockResolvedValue({ url: 'https://game-url.com' }) window.open = vi.fn().mockReturnValue(null) await playGame(mockGame) expect(mockNadal.game.launch).toHaveBeenCalledWith( GAME_API.PLAY_GAME, { gameName: 'game-id', homeUrl: "https://example.com/test-path", lobbyUrl: "https://example.com/test-path", id: "", isMobile: true, provider: "PLAYNGO", }, ) expect(window.open).toHaveBeenCalledWith('https://game-url.com', '_self') }) it('should open a constructed tab URL for casino games', async () => { const { playGame } = useGames() const mockGame = { apiUrl: 'https://casino-api.example.com', gameId: 'casino-game-id', provider: 'VIVO', newTab: true, } mockNadal.game.launch.mockResolvedValue({ url: 'https://casino-game-url.com', }) window.open = vi.fn().mockReturnValue(null) await playGame(mockGame) expect(mockNadal.game.launch).toHaveBeenCalledWith( GAME_API.PLAY_GAME, { gameName: 'casino-game-id', homeUrl: "https://example.com/test-path", lobbyUrl: "https://example.com/test-path", id: "", provider: "VIVO", isMobile: true, }, ) expect(window.open).toHaveBeenCalledWith( 'https://casino-game-url.com', '_blank', ) }) })