88 lines
2.4 KiB
TypeScript
88 lines
2.4 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
|
|
import {
|
|
GetTotalRacesUseCase,
|
|
type GetTotalRacesInput,
|
|
type GetTotalRacesResult,
|
|
type GetTotalRacesErrorCode,
|
|
} from './GetTotalRacesUseCase';
|
|
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
|
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
describe('GetTotalRacesUseCase', () => {
|
|
let useCase: GetTotalRacesUseCase;
|
|
let raceRepository: {
|
|
findAll: Mock;
|
|
};
|
|
let logger: {
|
|
debug: Mock;
|
|
info: Mock;
|
|
warn: Mock;
|
|
error: Mock;
|
|
};
|
|
let output: UseCaseOutputPort<GetTotalRacesResult> & { present: Mock };
|
|
|
|
beforeEach(() => {
|
|
raceRepository = {
|
|
findAll: vi.fn(),
|
|
};
|
|
logger = {
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
};
|
|
output = {
|
|
present: vi.fn(),
|
|
} as unknown as UseCaseOutputPort<GetTotalRacesResult> & { present: Mock };
|
|
|
|
useCase = new GetTotalRacesUseCase(
|
|
raceRepository as unknown as IRaceRepository,
|
|
logger as unknown as Logger,
|
|
output,
|
|
);
|
|
});
|
|
|
|
it('should return total number of races', async () => {
|
|
const races = [
|
|
{ id: 'race-1', name: 'Race 1' },
|
|
{ id: 'race-2', name: 'Race 2' },
|
|
];
|
|
|
|
raceRepository.findAll.mockResolvedValue(races);
|
|
|
|
const input: GetTotalRacesInput = {};
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(output.present).toHaveBeenCalledTimes(1);
|
|
|
|
const payload = output.present.mock.calls[0][0] as GetTotalRacesResult;
|
|
expect(payload.totalRaces).toBe(2);
|
|
});
|
|
|
|
it('should return error on repository failure', async () => {
|
|
const repositoryError = new Error('Repository error');
|
|
|
|
raceRepository.findAll.mockRejectedValue(repositoryError);
|
|
|
|
const input: GetTotalRacesInput = {};
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
|
|
const errorResult = result.unwrapErr() as ApplicationErrorCode<
|
|
GetTotalRacesErrorCode,
|
|
{ message: string }
|
|
>;
|
|
|
|
expect(errorResult.code).toBe('REPOSITORY_ERROR');
|
|
expect(errorResult.details.message).toBe('Repository error');
|
|
|
|
expect(output.present).not.toHaveBeenCalled();
|
|
});
|
|
})
|