refactor racing use cases
This commit is contained in:
@@ -1,17 +1,26 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { GetLeagueFullConfigUseCase } from './GetLeagueFullConfigUseCase';
|
||||
import {
|
||||
GetLeagueFullConfigUseCase,
|
||||
type GetLeagueFullConfigInput,
|
||||
type GetLeagueFullConfigResult,
|
||||
type GetLeagueFullConfigErrorCode,
|
||||
} from './GetLeagueFullConfigUseCase';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
|
||||
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
|
||||
import type { LeagueFullConfigOutputPort } from '../ports/output/LeagueFullConfigOutputPort';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetLeagueFullConfigUseCase', () => {
|
||||
let useCase: GetLeagueFullConfigUseCase;
|
||||
let leagueRepository: ILeagueRepository;
|
||||
let seasonRepository: ISeasonRepository;
|
||||
let leagueScoringConfigRepository: ILeagueScoringConfigRepository;
|
||||
let gameRepository: IGameRepository;
|
||||
let leagueRepository: ILeagueRepository & { findById: ReturnType<typeof vi.fn> };
|
||||
let seasonRepository: ISeasonRepository & { findByLeagueId: ReturnType<typeof vi.fn> };
|
||||
let leagueScoringConfigRepository: ILeagueScoringConfigRepository & {
|
||||
findBySeasonId: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
let gameRepository: IGameRepository & { findById: ReturnType<typeof vi.fn> };
|
||||
let output: UseCaseOutputPort<GetLeagueFullConfigResult> & { present: ReturnType<typeof vi.fn> };
|
||||
|
||||
beforeEach(() => {
|
||||
leagueRepository = {
|
||||
@@ -31,16 +40,21 @@ describe('GetLeagueFullConfigUseCase', () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any;
|
||||
|
||||
output = { present: vi.fn() } as unknown as UseCaseOutputPort<GetLeagueFullConfigResult> & {
|
||||
present: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
useCase = new GetLeagueFullConfigUseCase(
|
||||
leagueRepository,
|
||||
seasonRepository,
|
||||
leagueScoringConfigRepository,
|
||||
gameRepository,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return league config when league exists', async () => {
|
||||
const params = { leagueId: 'league-1' };
|
||||
const input: GetLeagueFullConfigInput = { leagueId: 'league-1' };
|
||||
|
||||
const mockLeague = {
|
||||
id: 'league-1',
|
||||
@@ -69,33 +83,41 @@ describe('GetLeagueFullConfigUseCase', () => {
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(mockScoringConfig);
|
||||
gameRepository.findById.mockResolvedValue(mockGame);
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const output = result.value!;
|
||||
expect(output).toEqual({
|
||||
league: mockLeague,
|
||||
activeSeason: mockSeasons[0],
|
||||
scoringConfig: mockScoringConfig,
|
||||
game: mockGame,
|
||||
});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const firstCall = output.present.mock.calls[0]!;
|
||||
const presented = firstCall[0] as GetLeagueFullConfigResult;
|
||||
|
||||
expect(presented.config.league).toEqual(mockLeague);
|
||||
expect(presented.config.activeSeason).toEqual(mockSeasons[0]);
|
||||
expect(presented.config.scoringConfig).toEqual(mockScoringConfig);
|
||||
expect(presented.config.game).toEqual(mockGame);
|
||||
});
|
||||
|
||||
it('should return error when league not found', async () => {
|
||||
const params = { leagueId: 'league-1' };
|
||||
it('should return error when league not found and not call presenter', async () => {
|
||||
const input: GetLeagueFullConfigInput = { leagueId: 'league-1' };
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueFullConfigErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(error.details!.message).toBe('League with id league-1 not found');
|
||||
expect(error.details.message).toBe('League not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle no active season', async () => {
|
||||
const params = { leagueId: 'league-1' };
|
||||
const input: GetLeagueFullConfigInput = { leagueId: 'league-1' };
|
||||
|
||||
const mockLeague = {
|
||||
id: 'league-1',
|
||||
@@ -107,12 +129,37 @@ describe('GetLeagueFullConfigUseCase', () => {
|
||||
leagueRepository.findById.mockResolvedValue(mockLeague);
|
||||
seasonRepository.findByLeagueId.mockResolvedValue([]);
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const output = result.value!;
|
||||
expect(output).toEqual({
|
||||
league: mockLeague,
|
||||
});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const firstCall = output.present.mock.calls[0]!;
|
||||
const presented = firstCall[0] as GetLeagueFullConfigResult;
|
||||
|
||||
expect(presented.config.league).toEqual(mockLeague);
|
||||
expect(presented.config.activeSeason).toBeUndefined();
|
||||
expect(presented.config.scoringConfig).toBeUndefined();
|
||||
expect(presented.config.game).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return repository error when repository throws and not call presenter', async () => {
|
||||
const input: GetLeagueFullConfigInput = { leagueId: 'league-1' };
|
||||
|
||||
const thrownError = new Error('Repository failure');
|
||||
leagueRepository.findById.mockRejectedValue(thrownError);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueFullConfigErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('Repository failure');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user