refactor racing use cases
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetLeagueScoringConfigUseCase } from './GetLeagueScoringConfigUseCase';
|
||||
import {
|
||||
GetLeagueScoringConfigUseCase,
|
||||
type GetLeagueScoringConfigResult,
|
||||
type GetLeagueScoringConfigInput,
|
||||
type GetLeagueScoringConfigErrorCode,
|
||||
} from './GetLeagueScoringConfigUseCase';
|
||||
import { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
|
||||
import { IGameRepository } from '../../domain/repositories/IGameRepository';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetLeagueScoringConfigUseCase', () => {
|
||||
let useCase: GetLeagueScoringConfigUseCase;
|
||||
@@ -11,26 +18,31 @@ describe('GetLeagueScoringConfigUseCase', () => {
|
||||
let seasonRepository: { findByLeagueId: Mock };
|
||||
let leagueScoringConfigRepository: { findBySeasonId: Mock };
|
||||
let gameRepository: { findById: Mock };
|
||||
let getLeagueScoringPresetById: Mock;
|
||||
let presetProvider: { getPresetById: Mock };
|
||||
let output: UseCaseOutputPort<GetLeagueScoringConfigResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
leagueRepository = { findById: vi.fn() };
|
||||
seasonRepository = { findByLeagueId: vi.fn() };
|
||||
leagueScoringConfigRepository = { findBySeasonId: vi.fn() };
|
||||
gameRepository = { findById: vi.fn() };
|
||||
getLeagueScoringPresetById = vi.fn();
|
||||
presetProvider = { getPresetById: vi.fn() };
|
||||
output = { present: vi.fn() } as unknown as UseCaseOutputPort<GetLeagueScoringConfigResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
useCase = new GetLeagueScoringConfigUseCase(
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
seasonRepository as unknown as ISeasonRepository,
|
||||
leagueScoringConfigRepository as unknown as ILeagueScoringConfigRepository,
|
||||
gameRepository as unknown as IGameRepository,
|
||||
getLeagueScoringPresetById,
|
||||
presetProvider,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return scoring config for active season', async () => {
|
||||
const leagueId = 'league-1';
|
||||
const league = { id: leagueId };
|
||||
const input: GetLeagueScoringConfigInput = { leagueId: 'league-1' };
|
||||
const league = { id: input.leagueId };
|
||||
const season = { id: 'season-1', status: 'active', gameId: 'game-1' };
|
||||
const scoringConfig = { scoringPresetId: 'preset-1', championships: [] };
|
||||
const game = { id: 'game-1', name: 'Game 1' };
|
||||
@@ -40,25 +52,25 @@ describe('GetLeagueScoringConfigUseCase', () => {
|
||||
seasonRepository.findByLeagueId.mockResolvedValue([season]);
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(scoringConfig);
|
||||
gameRepository.findById.mockResolvedValue(game);
|
||||
getLeagueScoringPresetById.mockResolvedValue(preset);
|
||||
presetProvider.getPresetById.mockReturnValue(preset);
|
||||
|
||||
const result = await useCase.execute({ leagueId });
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
leagueId,
|
||||
seasonId: 'season-1',
|
||||
gameId: 'game-1',
|
||||
gameName: 'Game 1',
|
||||
scoringPresetId: 'preset-1',
|
||||
preset,
|
||||
championships: [],
|
||||
});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented =
|
||||
output.present.mock.calls[0][0] as GetLeagueScoringConfigResult;
|
||||
expect(presented.league).toEqual(league);
|
||||
expect(presented.season).toEqual(season);
|
||||
expect(presented.scoringConfig).toEqual(scoringConfig);
|
||||
expect(presented.game).toEqual(game);
|
||||
expect(presented.preset).toEqual(preset);
|
||||
});
|
||||
|
||||
it('should return scoring config for first season if no active', async () => {
|
||||
const leagueId = 'league-1';
|
||||
const league = { id: leagueId };
|
||||
const input: GetLeagueScoringConfigInput = { leagueId: 'league-1' };
|
||||
const league = { id: input.leagueId };
|
||||
const season = { id: 'season-1', status: 'inactive', gameId: 'game-1' };
|
||||
const scoringConfig = { scoringPresetId: undefined, championships: [] };
|
||||
const game = { id: 'game-1', name: 'Game 1' };
|
||||
@@ -68,16 +80,18 @@ describe('GetLeagueScoringConfigUseCase', () => {
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(scoringConfig);
|
||||
gameRepository.findById.mockResolvedValue(game);
|
||||
|
||||
const result = await useCase.execute({ leagueId });
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
leagueId,
|
||||
seasonId: 'season-1',
|
||||
gameId: 'game-1',
|
||||
gameName: 'Game 1',
|
||||
championships: [],
|
||||
});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented =
|
||||
output.present.mock.calls[0][0] as GetLeagueScoringConfigResult;
|
||||
expect(presented.league).toEqual(league);
|
||||
expect(presented.season).toEqual(season);
|
||||
expect(presented.scoringConfig).toEqual(scoringConfig);
|
||||
expect(presented.game).toEqual(game);
|
||||
expect(presented.preset).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return error if league not found', async () => {
|
||||
@@ -86,7 +100,13 @@ describe('GetLeagueScoringConfigUseCase', () => {
|
||||
const result = await useCase.execute({ leagueId: 'league-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.error).toEqual({ code: 'LEAGUE_NOT_FOUND' });
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueScoringConfigErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(err.details.message).toBe('League not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error if no seasons', async () => {
|
||||
@@ -96,7 +116,13 @@ describe('GetLeagueScoringConfigUseCase', () => {
|
||||
const result = await useCase.execute({ leagueId: 'league-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.error).toEqual({ code: 'NO_SEASONS' });
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueScoringConfigErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('NO_SEASONS');
|
||||
expect(err.details.message).toBe('No seasons found for league');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error if no seasons (null)', async () => {
|
||||
@@ -106,29 +132,69 @@ describe('GetLeagueScoringConfigUseCase', () => {
|
||||
const result = await useCase.execute({ leagueId: 'league-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.error).toEqual({ code: 'NO_SEASONS' });
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueScoringConfigErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('NO_SEASONS');
|
||||
expect(err.details.message).toBe('No seasons found for league');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error if no scoring config', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
seasonRepository.findByLeagueId.mockResolvedValue([{ id: 'season-1', status: 'active', gameId: 'game-1' }]);
|
||||
seasonRepository.findByLeagueId.mockResolvedValue([
|
||||
{ id: 'season-1', status: 'active', gameId: 'game-1' },
|
||||
]);
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.error).toEqual({ code: 'NO_SCORING_CONFIG' });
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueScoringConfigErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('NO_SCORING_CONFIG');
|
||||
expect(err.details.message).toBe('Scoring configuration not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error if game not found', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
seasonRepository.findByLeagueId.mockResolvedValue([{ id: 'season-1', status: 'active', gameId: 'game-1' }]);
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue({ scoringPresetId: undefined, championships: [] });
|
||||
seasonRepository.findByLeagueId.mockResolvedValue([
|
||||
{ id: 'season-1', status: 'active', gameId: 'game-1' },
|
||||
]);
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue({
|
||||
scoringPresetId: undefined,
|
||||
championships: [],
|
||||
});
|
||||
gameRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.error).toEqual({ code: 'GAME_NOT_FOUND' });
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueScoringConfigErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('GAME_NOT_FOUND');
|
||||
expect(err.details.message).toBe('Game not found for season');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should wrap repository errors', async () => {
|
||||
leagueRepository.findById.mockRejectedValue(new Error('db down'));
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueScoringConfigErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('db down');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user