/** * useChallongeTests Composable Tests */ import { describe, it, expect, beforeEach, vi } from 'vitest'; import { ref } from 'vue'; // Mock dependencies vi.mock('../../../src/utilities/tournament-query.js', () => ({ queryAllTournaments: vi.fn(() => Promise.resolve([ { id: 1, name: 'Tournament 1', state: 'pending' }, { id: 2, name: 'Tournament 2', state: 'underway' } ]) ) })); vi.mock('../../../src/composables/useAsyncState.js', () => ({ useAsyncState: vi.fn(() => { const data = ref(null); const isLoading = ref(false); const error = ref(null); return { data, isLoading, error, execute: vi.fn(async fn => { isLoading.value = true; error.value = null; try { const result = await fn(); data.value = result; return result; } catch (e) { error.value = e; throw e; } finally { isLoading.value = false; } }), reset: vi.fn(() => { data.value = null; error.value = null; isLoading.value = false; }) }; }) })); import { useChallongeTests } from '../../../src/composables/useChallongeTests.js'; describe('useChallongeTests', () => { let mockClient; let apiVersion; let tournamentScope; beforeEach(() => { vi.clearAllMocks(); mockClient = ref({ tournaments: { list: vi.fn(() => Promise.resolve([ { id: 1, name: 'Test Tournament 1' }, { id: 2, name: 'Test Tournament 2' } ]) ), get: vi.fn(id => Promise.resolve({ id, name: `Tournament ${id}`, participants: [] }) ) } }); apiVersion = ref('v2.1'); tournamentScope = ref('user'); }); it('creates composable with initial state', () => { const { tournaments, loading, error, searchQuery, currentPage } = useChallongeTests(mockClient, apiVersion, tournamentScope); expect(tournaments.value).toBeNull(); expect(loading.value).toBe(false); expect(error.value).toBeNull(); expect(searchQuery.value).toBe(''); expect(currentPage.value).toBe(1); }); it('loads tournaments successfully', async () => { const { testListTournaments, tournaments } = useChallongeTests( mockClient, apiVersion, tournamentScope ); await testListTournaments(); expect(tournaments.value).toBeDefined(); expect(Array.isArray(tournaments.value)).toBe(true); }); it('filters tournaments by search query', async () => { const { testListTournaments, searchQuery, filteredTournaments } = useChallongeTests(mockClient, apiVersion, tournamentScope); await testListTournaments(); // Set search query searchQuery.value = 'Tournament 1'; expect(filteredTournaments.value).toBeDefined(); expect(filteredTournaments.value.length).toBeGreaterThan(0); }); it('gets tournament name from different structures', () => { const { getTournamentName } = useChallongeTests( mockClient, apiVersion, tournamentScope ); // v1 structure expect(getTournamentName({ tournament: { name: 'Test' } })).toBe('Test'); // v2.1 structure expect(getTournamentName({ name: 'Test' })).toBe('Test'); // Empty expect(getTournamentName({})).toBe(''); }); it('gets tournament ID from different structures', () => { const { getTournamentId } = useChallongeTests( mockClient, apiVersion, tournamentScope ); expect(getTournamentId({ tournament: { id: 123 } })).toBe(123); expect(getTournamentId({ id: 456 })).toBe(456); }); it('gets tournament property', () => { const { getTournamentProp } = useChallongeTests( mockClient, apiVersion, tournamentScope ); const tournament = { tournament: { state: 'pending', url: 'test-url' } }; expect(getTournamentProp(tournament, 'state')).toBe('pending'); expect(getTournamentProp(tournament, 'url')).toBe('test-url'); }); it('toggles tournament details', async () => { const { toggleTournamentDetails, expandedTournamentId } = useChallongeTests( mockClient, apiVersion, tournamentScope ); expect(expandedTournamentId.value).toBeNull(); await toggleTournamentDetails(1); expect(expandedTournamentId.value).toBe(1); // Toggle off await toggleTournamentDetails(1); expect(expandedTournamentId.value).toBeNull(); }); it('resets state', async () => { const { testListTournaments, resetState, tournaments, searchQuery, expandedTournamentId } = useChallongeTests(mockClient, apiVersion, tournamentScope); await testListTournaments(); searchQuery.value = 'test'; expandedTournamentId.value = 1; resetState(); expect(searchQuery.value).toBe(''); expect(expandedTournamentId.value).toBeNull(); }); it('formats dates correctly', () => { const { formatDate } = useChallongeTests( mockClient, apiVersion, tournamentScope ); const date = '2024-01-15T10:30:00Z'; const formatted = formatDate(date); expect(formatted).toBeTruthy(); expect(typeof formatted).toBe('string'); expect(formatDate(null)).toBe(''); }); it('computes pagination info', async () => { const { testListTournaments, paginationInfo } = useChallongeTests( mockClient, apiVersion, tournamentScope ); await testListTournaments(); expect(paginationInfo.value).toContain('Showing'); }); it('handles v1 API without pagination', async () => { apiVersion.value = 'v1'; const { testListTournaments, tournaments } = useChallongeTests( mockClient, apiVersion, tournamentScope ); await testListTournaments(); expect(mockClient.value.tournaments.list).toHaveBeenCalled(); expect(tournaments.value).toBeDefined(); }); });