core tests
This commit is contained in:
52
core/leagues/application/use-cases/GetLeagueUseCase.test.ts
Normal file
52
core/leagues/application/use-cases/GetLeagueUseCase.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { GetLeagueUseCase, GetLeagueQuery } from './GetLeagueUseCase';
|
||||
|
||||
describe('GetLeagueUseCase', () => {
|
||||
let mockLeagueRepository: any;
|
||||
let mockEventPublisher: any;
|
||||
let useCase: GetLeagueUseCase;
|
||||
|
||||
const mockLeague = {
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
ownerId: 'owner-1',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockLeagueRepository = {
|
||||
findById: vi.fn().mockResolvedValue(mockLeague),
|
||||
};
|
||||
mockEventPublisher = {
|
||||
emitLeagueAccessed: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
useCase = new GetLeagueUseCase(mockLeagueRepository, mockEventPublisher);
|
||||
});
|
||||
|
||||
it('should return league data', async () => {
|
||||
const query: GetLeagueQuery = { leagueId: 'league-1' };
|
||||
const result = await useCase.execute(query);
|
||||
|
||||
expect(result).toEqual(mockLeague);
|
||||
expect(mockLeagueRepository.findById).toHaveBeenCalledWith('league-1');
|
||||
expect(mockEventPublisher.emitLeagueAccessed).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should emit event if driverId is provided', async () => {
|
||||
const query: GetLeagueQuery = { leagueId: 'league-1', driverId: 'driver-1' };
|
||||
await useCase.execute(query);
|
||||
|
||||
expect(mockEventPublisher.emitLeagueAccessed).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw error if league not found', async () => {
|
||||
mockLeagueRepository.findById.mockResolvedValue(null);
|
||||
const query: GetLeagueQuery = { leagueId: 'non-existent' };
|
||||
|
||||
await expect(useCase.execute(query)).rejects.toThrow('League with id non-existent not found');
|
||||
});
|
||||
|
||||
it('should throw error if leagueId is missing', async () => {
|
||||
const query: any = {};
|
||||
await expect(useCase.execute(query)).rejects.toThrow('League ID is required');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user