141 lines
4.5 KiB
TypeScript
141 lines
4.5 KiB
TypeScript
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 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,
|
|
scheduledAt: new Date('2023-01-01T10:00:00Z'),
|
|
track: 'Track 1',
|
|
car: 'Car 1',
|
|
});
|
|
|
|
leagueRepository.findById.mockResolvedValue(league);
|
|
raceRepository.findByLeagueId.mockResolvedValue([race]);
|
|
|
|
const input: GetLeagueScheduleInput = { leagueId };
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
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 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 input: GetLeagueScheduleInput = { leagueId };
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
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();
|
|
});
|
|
}); |