import { describe, it, expect, beforeEach, vi } from 'vitest' import { useSlug } from '../use-slug' import { SLUG_NEWS, RESET_PASSWORD_SLUG, PROMOTION_SLUG, SIMPLE_PAGE_SLUG, } from '#lib/constants' import { IFrameGameURLEnum, SlugType } from '#lib/enums' const mockGame = { getGameUrl: vi.fn(), getIframeGameMapGameApi: vi.fn(), } const mockNadal = { news: { postBySlug: vi.fn(), }, } const mockRoute = { params: { slug: '' }, } const mockRouter = { replace: vi.fn(), } // Mock dependencies vi.mock('nuxt/app', () => ({ useRoute: () => mockRoute, useRouter: () => mockRouter, })) vi.mock('#lib/composables', () => ({ useGames: () => mockGame, useNadal: () => mockNadal, })) describe('useSlug', () => { beforeEach(() => { const originalLocation = window.location delete (window as any).location window.location = { href: '', assign: vi.fn() } as any }) it('should return SlugType.IFRAMES for iframe game URLs', async () => { mockRoute.params.slug = IFrameGameURLEnum.VIRTUAL_SPORTS const { handleSlug } = useSlug() const result = await handleSlug() expect(result).toBe(SlugType.IFRAMES) }) it('should return RESET_PASSWORD_SLUG for reset password slug', async () => { mockRoute.params.slug = RESET_PASSWORD_SLUG const { handleSlug } = useSlug() const result = await handleSlug() expect(result).toBe(RESET_PASSWORD_SLUG) }) it('should return SlugType.PROMOTION_SLUG for promotion slugs', async () => { mockRoute.params.slug = PROMOTION_SLUG[0] const { handleSlug } = useSlug() const result = await handleSlug() expect(result).toBe(SlugType.PROMOTION_SLUG) }) it('should return SlugType.SIMPLE_PAGE_SLUG for simple page slugs', async () => { mockRoute.params.slug = SIMPLE_PAGE_SLUG[0] const { handleSlug } = useSlug() const result = await handleSlug() expect(result).toBe(SlugType.SIMPLE_PAGE_SLUG) }) it('should return SlugType.NEWS_SLUG for news slugs', async () => { mockRoute.params.slug = SLUG_NEWS.PAGE const { handleSlug } = useSlug() const result = await handleSlug() expect(result).toBe(SlugType.NEWS_SLUG) }) it('should return SlugType.NEWS_DETAIL for valid news detail slug', async () => { mockRoute.params.slug = 'news-slug.html' mockNadal.news.postBySlug.mockResolvedValue(true) const { handleSlug } = useSlug() const result = await handleSlug() expect(result).toBe(SlugType.NEWS_DETAIL) }) it('should redirect to 404 page for invalid slugs', async () => { mockRoute.params.slug = 'invalid-slug' const { handleSlug } = useSlug() await handleSlug() expect(mockRouter.replace).toHaveBeenCalledWith('/404') }) it('should call getGameUrl with the correct URL and set window.location.href', async () => { const mockUrl = 'http://example.com' mockRoute.params.slug = 'test-slug' mockGame.getGameUrl = vi.fn().mockResolvedValue(mockUrl) mockGame.getIframeGameMapGameApi = vi.fn().mockReturnValue('game-api-url') const { openMobileIframeGame } = useSlug() await openMobileIframeGame() expect(mockGame.getIframeGameMapGameApi).toHaveBeenCalledWith('/test-slug') expect(mockGame.getGameUrl).toHaveBeenCalledWith('game-api-url') expect(window.location.href).toBe(mockUrl) }) })