refactor racing use cases
This commit is contained in:
@@ -1,25 +1,56 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetLeagueScheduleUseCase } from './GetLeagueScheduleUseCase';
|
||||
import { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
GetLeagueScheduleUseCase,
|
||||
type GetLeagueScheduleInput,
|
||||
type GetLeagueScheduleResult,
|
||||
type GetLeagueScheduleErrorCode,
|
||||
} from './GetLeagueScheduleUseCase';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { League } from '../../domain/entities/League';
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
|
||||
describe('GetLeagueScheduleUseCase', () => {
|
||||
let useCase: GetLeagueScheduleUseCase;
|
||||
let leagueRepository: {
|
||||
findById: Mock;
|
||||
};
|
||||
let raceRepository: {
|
||||
findByLeagueId: Mock;
|
||||
};
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<GetLeagueScheduleResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
leagueRepository = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
raceRepository = {
|
||||
findByLeagueId: vi.fn(),
|
||||
};
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetLeagueScheduleResult> & { present: Mock };
|
||||
|
||||
useCase = new GetLeagueScheduleUseCase(
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return league schedule', async () => {
|
||||
it('should present league schedule when races exist', async () => {
|
||||
const leagueId = 'league-1';
|
||||
const league = { id: leagueId } as unknown as League;
|
||||
const race = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId,
|
||||
@@ -28,32 +59,83 @@ describe('GetLeagueScheduleUseCase', () => {
|
||||
car: 'Car 1',
|
||||
});
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
raceRepository.findByLeagueId.mockResolvedValue([race]);
|
||||
|
||||
const result = await useCase.execute({ leagueId });
|
||||
const input: GetLeagueScheduleInput = { leagueId };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
races: [
|
||||
{
|
||||
id: 'race-1',
|
||||
name: 'Track 1 - Car 1',
|
||||
scheduledAt: new Date('2023-01-01T10:00:00Z'),
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented =
|
||||
output.present.mock.calls[0]![0] as GetLeagueScheduleResult;
|
||||
|
||||
expect(presented.league).toBe(league);
|
||||
expect(presented.races).toHaveLength(1);
|
||||
expect(presented.races[0].race).toBe(race);
|
||||
});
|
||||
|
||||
it('should return empty schedule when no races', async () => {
|
||||
it('should present empty schedule when no races exist', async () => {
|
||||
const leagueId = 'league-1';
|
||||
const league = { id: leagueId } as unknown as League;
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
raceRepository.findByLeagueId.mockResolvedValue([]);
|
||||
|
||||
const result = await useCase.execute({ leagueId });
|
||||
const input: GetLeagueScheduleInput = { leagueId };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
races: [],
|
||||
});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented =
|
||||
output.present.mock.calls[0]![0] as GetLeagueScheduleResult;
|
||||
|
||||
expect(presented.league).toBe(league);
|
||||
expect(presented.races).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should return LEAGUE_NOT_FOUND error when league does not exist', async () => {
|
||||
const leagueId = 'missing-league';
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const input: GetLeagueScheduleInput = { leagueId };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueScheduleErrorCode,
|
||||
{ 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 REPOSITORY_ERROR when repository throws', async () => {
|
||||
const leagueId = 'league-1';
|
||||
const league = { id: leagueId } as League;
|
||||
const repositoryError = new Error('DB down');
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
raceRepository.findByLeagueId.mockRejectedValue(repositoryError);
|
||||
|
||||
const input: GetLeagueScheduleInput = { leagueId };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueScheduleErrorCode,
|
||||
{ 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