website refactor
This commit is contained in:
@@ -5,38 +5,24 @@ import {
|
||||
type GetSeasonDetailsResult,
|
||||
type GetSeasonDetailsErrorCode,
|
||||
} from './GetSeasonDetailsUseCase';
|
||||
import type { SeasonRepository } from '../../domain/repositories/SeasonRepository';
|
||||
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetSeasonDetailsUseCase', () => {
|
||||
let useCase: GetSeasonDetailsUseCase;
|
||||
let leagueRepository: {
|
||||
findById: Mock;
|
||||
};
|
||||
let seasonRepository: {
|
||||
findById: Mock;
|
||||
};
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
leagueRepository = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
seasonRepository = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new GetSeasonDetailsUseCase(leagueRepository as unknown as ILeagueRepository,
|
||||
seasonRepository as unknown as ISeasonRepository);
|
||||
useCase = new GetSeasonDetailsUseCase(seasonRepository as any);
|
||||
});
|
||||
|
||||
it('returns full details for a season belonging to the league', async () => {
|
||||
const league = { id: 'league-1' };
|
||||
it('returns full details for a season', async () => {
|
||||
const season = Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: 'league-1',
|
||||
@@ -45,58 +31,30 @@ describe('GetSeasonDetailsUseCase', () => {
|
||||
status: 'planned',
|
||||
}).withMaxDrivers(24);
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const input: GetSeasonDetailsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const presented = result.unwrap();
|
||||
|
||||
const presented =
|
||||
(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.toString()).toBe('planned');
|
||||
expect(presented?.season.maxDrivers).toBe(24);
|
||||
expect(presented).toBeDefined();
|
||||
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.toString()).toBe('planned');
|
||||
expect(presented.season.maxDrivers).toBe(24);
|
||||
});
|
||||
|
||||
it('returns error when league not found', async () => {
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
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('LEAGUE_NOT_FOUND');
|
||||
expect(error.details.message).toBe('League not found: league-1');
|
||||
});
|
||||
|
||||
it('returns error when season not found', async () => {
|
||||
const league = { id: 'league-1' };
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const input: GetSeasonDetailsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
@@ -110,51 +68,15 @@ describe('GetSeasonDetailsUseCase', () => {
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(error.details.message).toBe(
|
||||
'Season season-1 does not belong to league league-1',
|
||||
);
|
||||
});
|
||||
|
||||
it('returns error when season belongs to different league', async () => {
|
||||
const league = { id: 'league-1' };
|
||||
const season = Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: 'league-2',
|
||||
gameId: 'iracing',
|
||||
name: 'Season',
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
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('SEASON_NOT_FOUND');
|
||||
expect(error.details.message).toBe(
|
||||
'Season season-1 does not belong to league league-1',
|
||||
);
|
||||
});
|
||||
expect(error.details.message).toBe('Season not found');
|
||||
});
|
||||
|
||||
it('returns repository error when an unexpected exception occurs', async () => {
|
||||
leagueRepository.findById.mockRejectedValue(
|
||||
seasonRepository.findById.mockRejectedValue(
|
||||
new Error('Unexpected repository failure'),
|
||||
);
|
||||
|
||||
const input: GetSeasonDetailsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
@@ -169,5 +91,5 @@ describe('GetSeasonDetailsUseCase', () => {
|
||||
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('Unexpected repository failure');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user