refactor use cases
This commit is contained in:
@@ -11,7 +11,6 @@ import type { IResultRepository } from '../../domain/repositories/IResultReposit
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
|
||||
@@ -23,8 +22,6 @@ describe('ImportRaceResultsApiUseCase', () => {
|
||||
let driverRepository: { findByIRacingId: Mock };
|
||||
let standingRepository: { recalculate: Mock };
|
||||
let logger: { debug: Mock; info: Mock; warn: Mock; error: Mock };
|
||||
let output: UseCaseOutputPort<ImportRaceResultsApiResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
raceRepository = { findById: vi.fn() };
|
||||
leagueRepository = { findById: vi.fn() };
|
||||
@@ -37,19 +34,12 @@ describe('ImportRaceResultsApiUseCase', () => {
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<ImportRaceResultsApiResult> & { present: Mock };
|
||||
|
||||
useCase = new ImportRaceResultsApiUseCase(
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
useCase = new ImportRaceResultsApiUseCase(raceRepository as unknown as IRaceRepository,
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
resultRepository as unknown as IResultRepository,
|
||||
driverRepository as unknown as IDriverRepository,
|
||||
standingRepository as unknown as IStandingRepository,
|
||||
logger as unknown as Logger,
|
||||
output,
|
||||
);
|
||||
logger as unknown as Logger);
|
||||
});
|
||||
|
||||
it('should return parse error for invalid JSON', async () => {
|
||||
@@ -68,8 +58,7 @@ describe('ImportRaceResultsApiUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('PARSE_ERROR');
|
||||
expect(err.details?.message).toBe('Invalid JSON in results file content');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return race not found error', async () => {
|
||||
const input: ImportRaceResultsApiInput = { raceId: 'race-1', resultsFileContent: '[]' };
|
||||
@@ -89,8 +78,7 @@ describe('ImportRaceResultsApiUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('RACE_NOT_FOUND');
|
||||
expect(err.details?.message).toBe('Race race-1 not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return league not found error', async () => {
|
||||
const input: ImportRaceResultsApiInput = { raceId: 'race-1', resultsFileContent: '[]' };
|
||||
@@ -111,8 +99,7 @@ describe('ImportRaceResultsApiUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(err.details?.message).toBe('League league-1 not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return results exist error', async () => {
|
||||
const input: ImportRaceResultsApiInput = { raceId: 'race-1', resultsFileContent: '[]' };
|
||||
@@ -134,8 +121,7 @@ describe('ImportRaceResultsApiUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('RESULTS_EXIST');
|
||||
expect(err.details?.message).toBe('Results already exist for this race');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return driver not found error', async () => {
|
||||
const input: ImportRaceResultsApiInput = {
|
||||
@@ -162,8 +148,7 @@ describe('ImportRaceResultsApiUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('DRIVER_NOT_FOUND');
|
||||
expect(err.details?.message).toBe('Driver with iRacing ID 123 not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should import results successfully', async () => {
|
||||
const input: ImportRaceResultsApiInput = {
|
||||
@@ -187,9 +172,7 @@ describe('ImportRaceResultsApiUseCase', () => {
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presentedRaw = output.present.mock.calls[0]?.[0];
|
||||
expect(presentedRaw).toBeDefined();
|
||||
const presentedRaw = expect(presentedRaw).toBeDefined();
|
||||
const presented = presentedRaw as ImportRaceResultsApiResult;
|
||||
|
||||
expect(presented.success).toBe(true);
|
||||
|
||||
Reference in New Issue
Block a user