101 lines
3.0 KiB
TypeScript
101 lines
3.0 KiB
TypeScript
import { describe, expect, it, vi, type Mock } from 'vitest';
|
|
import { PrizeType, type Prize } from '../../domain/entities/Prize';
|
|
import type { PrizeRepository } from '../../domain/repositories/PrizeRepository';
|
|
import { GetPrizesUseCase, type GetPrizesInput } from './GetPrizesUseCase';
|
|
|
|
describe('GetPrizesUseCase', () => {
|
|
let prizeRepository: {
|
|
findByLeagueId: Mock;
|
|
findByLeagueIdAndSeasonId: Mock;
|
|
};
|
|
let useCase: GetPrizesUseCase;
|
|
|
|
beforeEach(() => {
|
|
prizeRepository = {
|
|
findByLeagueId: vi.fn(),
|
|
findByLeagueIdAndSeasonId: vi.fn(),
|
|
};
|
|
|
|
useCase = new GetPrizesUseCase(
|
|
prizeRepository as unknown as PrizeRepository,
|
|
);
|
|
});
|
|
|
|
it('retrieves and sorts prizes by leagueId when seasonId is not provided', async () => {
|
|
const prizes: Prize[] = [
|
|
{
|
|
id: 'p2',
|
|
leagueId: 'league-1',
|
|
seasonId: 'season-1',
|
|
position: 2,
|
|
name: 'Second',
|
|
amount: 50,
|
|
type: PrizeType.CASH,
|
|
awarded: false,
|
|
createdAt: new Date(),
|
|
},
|
|
{
|
|
id: 'p1',
|
|
leagueId: 'league-1',
|
|
seasonId: 'season-1',
|
|
position: 1,
|
|
name: 'First',
|
|
amount: 100,
|
|
type: PrizeType.CASH,
|
|
awarded: false,
|
|
createdAt: new Date(),
|
|
},
|
|
];
|
|
prizeRepository.findByLeagueId.mockResolvedValue(prizes);
|
|
|
|
const input: GetPrizesInput = { leagueId: 'league-1' };
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(prizeRepository.findByLeagueId).toHaveBeenCalledWith('league-1');
|
|
expect(prizeRepository.findByLeagueIdAndSeasonId).not.toHaveBeenCalled();
|
|
|
|
const value = result.value;
|
|
expect(value.prizes.map(p => p.position)).toEqual([1, 2]);
|
|
expect(value.prizes.map(p => p.id)).toEqual(['p1', 'p2']);
|
|
});
|
|
|
|
it('retrieves and sorts prizes by leagueId and seasonId when provided', async () => {
|
|
const prizes: Prize[] = [
|
|
{
|
|
id: 'p3',
|
|
leagueId: 'league-1',
|
|
seasonId: 'season-1',
|
|
position: 3,
|
|
name: 'Third',
|
|
amount: 25,
|
|
type: PrizeType.CASH,
|
|
awarded: false,
|
|
createdAt: new Date(),
|
|
},
|
|
{
|
|
id: 'p1',
|
|
leagueId: 'league-1',
|
|
seasonId: 'season-1',
|
|
position: 1,
|
|
name: 'First',
|
|
amount: 100,
|
|
type: PrizeType.CASH,
|
|
awarded: false,
|
|
createdAt: new Date(),
|
|
},
|
|
];
|
|
prizeRepository.findByLeagueIdAndSeasonId.mockResolvedValue(prizes);
|
|
|
|
const input: GetPrizesInput = { leagueId: 'league-1', seasonId: 'season-1' };
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(prizeRepository.findByLeagueIdAndSeasonId).toHaveBeenCalledWith('league-1', 'season-1');
|
|
expect(prizeRepository.findByLeagueId).not.toHaveBeenCalled();
|
|
|
|
const value = result.value;
|
|
expect(value.prizes.map(p => p.position)).toEqual([1, 3]);
|
|
expect(value.prizes.map(p => p.id)).toEqual(['p1', 'p3']);
|
|
});
|
|
}); |