refactor racing use cases
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { CompleteRaceUseCase } from './CompleteRaceUseCase';
|
||||
import { CompleteRaceUseCase, type CompleteRaceInput, type CompleteRaceResult } from './CompleteRaceUseCase';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
|
||||
import type { CompleteRaceCommandDTO } from '../dto/CompleteRaceCommandDTO';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
describe('CompleteRaceUseCase', () => {
|
||||
let useCase: CompleteRaceUseCase;
|
||||
@@ -23,6 +23,7 @@ describe('CompleteRaceUseCase', () => {
|
||||
save: Mock;
|
||||
};
|
||||
let getDriverRating: Mock;
|
||||
let output: { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
raceRepository = {
|
||||
@@ -40,17 +41,19 @@ describe('CompleteRaceUseCase', () => {
|
||||
save: vi.fn(),
|
||||
};
|
||||
getDriverRating = vi.fn();
|
||||
output = { present: vi.fn() };
|
||||
useCase = new CompleteRaceUseCase(
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
raceRegistrationRepository as unknown as IRaceRegistrationRepository,
|
||||
resultRepository as unknown as IResultRepository,
|
||||
standingRepository as unknown as IStandingRepository,
|
||||
getDriverRating,
|
||||
output as unknown as UseCaseOutputPort<CompleteRaceResult>,
|
||||
);
|
||||
});
|
||||
|
||||
it('should complete race successfully when race exists and has registered drivers', async () => {
|
||||
const command: CompleteRaceCommandDTO = {
|
||||
const command: CompleteRaceInput = {
|
||||
raceId: 'race-1',
|
||||
};
|
||||
|
||||
@@ -75,7 +78,7 @@ describe('CompleteRaceUseCase', () => {
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(raceRepository.findById).toHaveBeenCalledWith('race-1');
|
||||
expect(raceRegistrationRepository.getRegisteredDrivers).toHaveBeenCalledWith('race-1');
|
||||
expect(getDriverRating).toHaveBeenCalledTimes(2);
|
||||
@@ -83,10 +86,12 @@ describe('CompleteRaceUseCase', () => {
|
||||
expect(standingRepository.save).toHaveBeenCalledTimes(2);
|
||||
expect(mockRace.complete).toHaveBeenCalled();
|
||||
expect(raceRepository.update).toHaveBeenCalledWith({ id: 'race-1', status: 'completed' });
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
expect(output.present).toHaveBeenCalledWith({ raceId: 'race-1', registeredDriverIds: ['driver-1', 'driver-2'] });
|
||||
});
|
||||
|
||||
it('should return error when race does not exist', async () => {
|
||||
const command: CompleteRaceCommandDTO = {
|
||||
const command: CompleteRaceInput = {
|
||||
raceId: 'race-1',
|
||||
};
|
||||
|
||||
@@ -96,10 +101,11 @@ describe('CompleteRaceUseCase', () => {
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toBe('RACE_NOT_FOUND');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error when no registered drivers', async () => {
|
||||
const command: CompleteRaceCommandDTO = {
|
||||
const command: CompleteRaceInput = {
|
||||
raceId: 'race-1',
|
||||
};
|
||||
|
||||
@@ -116,10 +122,11 @@ describe('CompleteRaceUseCase', () => {
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toBe('NO_REGISTERED_DRIVERS');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error when repository throws', async () => {
|
||||
const command: CompleteRaceCommandDTO = {
|
||||
const command: CompleteRaceInput = {
|
||||
raceId: 'race-1',
|
||||
};
|
||||
|
||||
@@ -137,6 +144,9 @@ describe('CompleteRaceUseCase', () => {
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toBe('UNKNOWN_ERROR');
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details?.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user