refactor racing use cases
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetSeasonDetailsUseCase } from './GetSeasonDetailsUseCase';
|
||||
import {
|
||||
GetSeasonDetailsUseCase,
|
||||
type GetSeasonDetailsInput,
|
||||
type GetSeasonDetailsResult,
|
||||
type GetSeasonDetailsErrorCode,
|
||||
} from './GetSeasonDetailsUseCase';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import { Season } from '../../domain/entities/Season';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetSeasonDetailsUseCase', () => {
|
||||
let useCase: GetSeasonDetailsUseCase;
|
||||
@@ -12,6 +19,9 @@ describe('GetSeasonDetailsUseCase', () => {
|
||||
let seasonRepository: {
|
||||
findById: Mock;
|
||||
};
|
||||
let output: UseCaseOutputPort<GetSeasonDetailsResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
leagueRepository = {
|
||||
@@ -20,9 +30,14 @@ describe('GetSeasonDetailsUseCase', () => {
|
||||
seasonRepository = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new GetSeasonDetailsUseCase(
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
seasonRepository as unknown as ISeasonRepository,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -39,34 +54,51 @@ describe('GetSeasonDetailsUseCase', () => {
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const result = await useCase.execute({
|
||||
const input: GetSeasonDetailsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
});
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.seasonId).toBe('season-1');
|
||||
expect(dto.leagueId).toBe('league-1');
|
||||
expect(dto.gameId).toBe('iracing');
|
||||
expect(dto.name).toBe('Detailed Season');
|
||||
expect(dto.status).toBe('planned');
|
||||
expect(dto.maxDrivers).toBe(24);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented =
|
||||
(output.present.mock.calls[0]?.[0] as GetSeasonDetailsResult | undefined) ??
|
||||
undefined;
|
||||
|
||||
expect(presented).toBeDefined();
|
||||
expect(presented?.leagueId).toBe('league-1');
|
||||
expect(presented?.season.id).toBe('season-1');
|
||||
expect(presented?.season.leagueId).toBe('league-1');
|
||||
expect(presented?.season.gameId).toBe('iracing');
|
||||
expect(presented?.season.name).toBe('Detailed Season');
|
||||
expect(presented?.season.status).toBe('planned');
|
||||
expect(presented?.season.maxDrivers).toBe(24);
|
||||
});
|
||||
|
||||
it('returns error when league not found', async () => {
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({
|
||||
const input: GetSeasonDetailsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
});
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'LEAGUE_NOT_FOUND',
|
||||
details: { message: 'League not found: league-1' },
|
||||
});
|
||||
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSeasonDetailsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(error.details.message).toBe('League not found: league-1');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns error when season not found', async () => {
|
||||
@@ -74,16 +106,25 @@ describe('GetSeasonDetailsUseCase', () => {
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({
|
||||
const input: GetSeasonDetailsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
});
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'SEASON_NOT_FOUND',
|
||||
details: { message: 'Season season-1 does not belong to league league-1' },
|
||||
});
|
||||
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSeasonDetailsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(error.details.message).toBe(
|
||||
'Season season-1 does not belong to league league-1',
|
||||
);
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns error when season belongs to different league', async () => {
|
||||
@@ -99,15 +140,48 @@ describe('GetSeasonDetailsUseCase', () => {
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const result = await useCase.execute({
|
||||
const input: GetSeasonDetailsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
});
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'SEASON_NOT_FOUND',
|
||||
details: { message: 'Season season-1 does not belong to league league-1' },
|
||||
});
|
||||
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSeasonDetailsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(error.details.message).toBe(
|
||||
'Season season-1 does not belong to league league-1',
|
||||
);
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns repository error when an unexpected exception occurs', async () => {
|
||||
leagueRepository.findById.mockRejectedValue(
|
||||
new Error('Unexpected repository failure'),
|
||||
);
|
||||
|
||||
const input: GetSeasonDetailsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSeasonDetailsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('Unexpected repository failure');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user