import { InMemoryLeagueRepository } from './InMemoryLeagueRepository'; import { LeagueData } from '../../../../core/leagues/application/ports/LeagueRepository'; describe('InMemoryLeagueRepository', () => { let repository: InMemoryLeagueRepository; beforeEach(() => { repository = new InMemoryLeagueRepository(); }); const createLeague = (id: string, name: string, ownerId: string): LeagueData => ({ id, name, ownerId, description: `Description for ${name}`, visibility: 'public', status: 'active', createdAt: new Date(), updatedAt: new Date(), maxDrivers: 100, approvalRequired: false, lateJoinAllowed: true, raceFrequency: 'weekly', raceDay: 'Monday', raceTime: '20:00', tracks: ['Spa'], scoringSystem: null, bonusPointsEnabled: true, penaltiesEnabled: true, protestsEnabled: true, appealsEnabled: true, stewardTeam: [], gameType: 'iRacing', skillLevel: 'Intermediate', category: 'Road', tags: [], }); describe('create and findById', () => { it('should return null when league does not exist', async () => { // When const result = await repository.findById('non-existent'); // Then expect(result).toBeNull(); }); it('should create and retrieve a league', async () => { // Given const league = createLeague('l1', 'League 1', 'o1'); // When await repository.create(league); const result = await repository.findById('l1'); // Then expect(result).toEqual(league); }); }); describe('findByName', () => { it('should find a league by name', async () => { // Given const league = createLeague('l1', 'Unique Name', 'o1'); await repository.create(league); // When const result = await repository.findByName('Unique Name'); // Then expect(result).toEqual(league); }); }); describe('update', () => { it('should update an existing league', async () => { // Given const league = createLeague('l1', 'Original Name', 'o1'); await repository.create(league); // When const updated = await repository.update('l1', { name: 'Updated Name' }); // Then expect(updated.name).toBe('Updated Name'); const result = await repository.findById('l1'); expect(result?.name).toBe('Updated Name'); }); it('should throw error when updating non-existent league', async () => { // When & Then await expect(repository.update('non-existent', { name: 'New' })).rejects.toThrow(); }); }); describe('delete', () => { it('should delete a league', async () => { // Given const league = createLeague('l1', 'To Delete', 'o1'); await repository.create(league); // When await repository.delete('l1'); // Then const result = await repository.findById('l1'); expect(result).toBeNull(); }); }); describe('search', () => { it('should find leagues by name or description', async () => { // Given const l1 = createLeague('l1', 'Formula 1', 'o1'); const l2 = createLeague('l2', 'GT3 Masters', 'o1'); await repository.create(l1); await repository.create(l2); // When const results = await repository.search('Formula'); // Then expect(results).toHaveLength(1); expect(results[0].id).toBe('l1'); }); }); });