import { describe, it, expect, vi, beforeEach } from 'vitest'; import { CreateLeagueMutation } from './CreateLeagueMutation'; import { LeagueService } from '@/lib/services/leagues/LeagueService'; // Mock dependencies vi.mock('@/lib/services/leagues/LeagueService', () => { return { LeagueService: vi.fn(), }; }); describe('CreateLeagueMutation', () => { let mutation: CreateLeagueMutation; let mockServiceInstance: any; beforeEach(() => { vi.clearAllMocks(); mockServiceInstance = { createLeague: vi.fn(), }; // Use mockImplementation to return the instance (LeagueService as any).mockImplementation(function() { return mockServiceInstance; }); mutation = new CreateLeagueMutation(); }); describe('execute', () => { describe('happy paths', () => { it('should successfully create a league with valid input', async () => { // Arrange const input = { name: 'Test League', description: 'A test league', visibility: 'public', ownerId: 'owner-123', }; const mockResult = { leagueId: 'league-123' }; mockServiceInstance.createLeague.mockResolvedValue(mockResult); // Act const result = await mutation.execute(input); // Assert expect(result.isOk()).toBe(true); expect(result.unwrap()).toBe('league-123'); expect(mockServiceInstance.createLeague).toHaveBeenCalledWith(input); expect(mockServiceInstance.createLeague).toHaveBeenCalledTimes(1); }); }); describe('failure modes', () => { it('should handle service failure during league creation', async () => { // Arrange const input = { name: 'Test League', description: 'A test league', visibility: 'public', ownerId: 'owner-123', }; const serviceError = new Error('Service error'); mockServiceInstance.createLeague.mockRejectedValue(serviceError); // Act const result = await mutation.execute(input); // Assert expect(result.isErr()).toBe(true); expect(result.getError()).toStrictEqual({ type: 'serverError', message: 'Service error', }); expect(mockServiceInstance.createLeague).toHaveBeenCalledTimes(1); }); }); describe('service instantiation', () => { it('should create LeagueService instance', () => { // Arrange & Act const mutation = new CreateLeagueMutation(); // Assert expect(mutation).toBeInstanceOf(CreateLeagueMutation); }); }); describe('result shape', () => { it('should return leagueId string on success', async () => { // Arrange const input = { name: 'Test League', description: 'A test league', visibility: 'public', ownerId: 'owner-123', }; const mockResult = { leagueId: 'league-123' }; mockServiceInstance.createLeague.mockResolvedValue(mockResult); // Act const result = await mutation.execute(input); // Assert expect(result.isOk()).toBe(true); const leagueId = result.unwrap(); expect(typeof leagueId).toBe('string'); expect(leagueId).toBe('league-123'); }); }); }); });