import { useDevice, useRouter } from '#imports' import { describe, it, expect, vi, beforeEach } from 'vitest' import { useNavigate } from '../use-navigate' import { useUser, useGames, useAuth } from '#lib/composables' import { useCheckGame } from '#lib/composables/game/use-check-game' import { isValidURL } from '#lib/utils' import { IFrameGameURLEnum } from '#lib/enums' vi.mock('#imports', () => ({ useDevice: vi.fn(), useRouter: vi.fn(), })) vi.mock('#lib/composables', () => ({ useUser: vi.fn(), useGames: vi.fn(), useAuth: vi.fn(), })) vi.mock('#lib/composables/game/use-check-game', () => ({ useCheckGame: vi.fn(), })) vi.mock('#lib/utils', () => ({ isValidURL: vi.fn(), })) describe('useNavigate', () => { let isMobile: boolean let router: any let isLogged: any let openAuthModal: any let getGameUrl: any let getIframeGameMapGameApi: any let openModalMaintainAthenaGames: any let isMaintainAthenaGames: any beforeEach(() => { isMobile = false router = { push: vi.fn() } isLogged = { value: false } openAuthModal = vi.fn() getGameUrl = vi.fn() getIframeGameMapGameApi = vi.fn() openModalMaintainAthenaGames = vi.fn() isMaintainAthenaGames = vi.fn() // @ts-ignore vi.mocked(useDevice).mockReturnValue({ isMobile }) vi.mocked(useRouter).mockReturnValue(router) // @ts-ignore vi.mocked(useUser).mockReturnValue({ isLogged }) // @ts-ignore vi.mocked(useAuth).mockReturnValue({ openAuthModal }) // @ts-ignore vi.mocked(useGames).mockReturnValue({ getGameUrl, getIframeGameMapGameApi, openModalMaintainAthenaGames, }) // @ts-ignore vi.mocked(useCheckGame).mockReturnValue({ isMaintainAthenaGames }) }) it('should navigate to the URL if logged in and no maintenance', async () => { isLogged.value = true isMaintainAthenaGames.mockReturnValue(false) const { navigate } = useNavigate() await navigate('/test-url') expect(router.push).toHaveBeenCalledWith('/test-url') }) it('should open auth modal if login is required and user is not logged in', async () => { isLogged.value = false const { navigate } = useNavigate() await navigate('/test-url', true) expect(openAuthModal).toHaveBeenCalled() expect(router.push).not.toHaveBeenCalled() }) it('should open maintenance modal if the URL corresponds to a maintenance game', async () => { isMaintainAthenaGames.mockReturnValue(true) const { navigate } = useNavigate() await navigate('/maintenance-url') expect(openModalMaintainAthenaGames).toHaveBeenCalled() expect(router.push).not.toHaveBeenCalled() }) it('should open the URL in a new tab for mobile if it matches iframe game mapping', async () => { isMobile = true vi.mocked(useDevice).mockReturnValue({ isMobile }) const url = IFrameGameURLEnum.LIVE_CASINO // Use one of the enums that exists in MAPPING_IFRAME_GAMES global.window = Object.create(window) const newTabMock = { location: { href: '' }, close: vi.fn() } const openSpy = vi.spyOn(window, 'open').mockReturnValue(newTabMock) const { navigate } = useNavigate() await navigate(url) expect(openSpy).toHaveBeenCalledWith('', '_blank') expect(newTabMock.location.href).toBe('') }) it('should open the URL in a new tab for mobile if it matches iframe game mapping', async () => { isMobile = true vi.mocked(useDevice).mockReturnValue({ isMobile }) const url = IFrameGameURLEnum.C_SPORTS // Use one of the enums that exists in MAPPING_IFRAME_GAMES global.window = Object.create(window) const newTabMock = { location: { href: '' }, close: vi.fn() } const navigateWithURLSpy = vi.spyOn(useNavigate(), 'navigateWithURL') const { navigate } = useNavigate() await navigate(url) expect(navigateWithURLSpy).not.toHaveBeenCalled() expect(newTabMock.location.href).toBe('') }) it('should handle URL navigation and validation in new tab', async () => { isMobile = false vi.mocked(useDevice).mockReturnValue({ isMobile }) global.window = Object.create(window) const newTabMock = { location: { href: '' }, close: vi.fn() } const openSpy = vi.spyOn(window, 'open').mockReturnValue(newTabMock) vi.mocked(isValidURL).mockReturnValue(false) const urlResponse = 'https://valid-url.com' getGameUrl.mockResolvedValue(urlResponse) const { navigate } = useNavigate() await navigate('/test-url', false, true) expect(openSpy).toHaveBeenCalledWith('', '_blank') expect(newTabMock.location.href).toBe(urlResponse) }) it('should close the new tab if URL validation fails', async () => { isMobile = false vi.mocked(useDevice).mockReturnValue({ isMobile }) global.window = Object.create(window) const newTabMock = { location: { href: '' }, close: vi.fn() } const openSpy = vi.spyOn(window, 'open').mockReturnValue(newTabMock) vi.mocked(isValidURL).mockReturnValue(false) getGameUrl.mockResolvedValue(undefined) const { navigate } = useNavigate() await navigate('/invalid-url', false, true) expect(newTabMock.close).toHaveBeenCalled() expect(router.push).not.toHaveBeenCalled() }) it('should set the new tab location to the URL if it is valid', async () => { isMobile = false vi.mocked(useDevice).mockReturnValue({ isMobile }) global.window = Object.create(window) // Mock new tab behavior const newTabMock = { location: { href: '' }, close: vi.fn() } const openSpy = vi.spyOn(window, 'open').mockReturnValue(newTabMock) // Mock isValidURL to return true vi.mocked(isValidURL).mockReturnValue(true) const url = 'https://valid-url.com' const { navigate } = useNavigate() await navigate(url, false, true) // Check that window.open was called to open a new tab expect(openSpy).toHaveBeenCalledWith('', '_blank') // Check that the new tab's location was set to the valid URL expect(newTabMock.location.href).toBe(url) }) })