refactor use cases
This commit is contained in:
@@ -8,15 +8,12 @@ import {
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('WithdrawFromRaceUseCase', () => {
|
||||
let raceRepository: { findById: ReturnType<typeof vi.fn> };
|
||||
let registrationRepository: { isRegistered: ReturnType<typeof vi.fn>; withdraw: ReturnType<typeof vi.fn> };
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<WithdrawFromRaceResult> & { present: ReturnType<typeof vi.fn> };
|
||||
|
||||
beforeEach(() => {
|
||||
raceRepository = {
|
||||
findById: vi.fn(),
|
||||
@@ -34,18 +31,13 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
error: vi.fn(),
|
||||
};
|
||||
|
||||
output = { present: vi.fn() } as unknown as UseCaseOutputPort<WithdrawFromRaceResult> & {
|
||||
present: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
});
|
||||
|
||||
const createUseCase = () =>
|
||||
new WithdrawFromRaceUseCase(
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
new WithdrawFromRaceUseCase(raceRepository as unknown as IRaceRepository,
|
||||
registrationRepository as unknown as IRaceRegistrationRepository,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
logger);
|
||||
|
||||
it('withdraws from race successfully', async () => {
|
||||
const race = {
|
||||
@@ -70,12 +62,6 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-1',
|
||||
status: 'withdrawn',
|
||||
});
|
||||
expect(registrationRepository.withdraw).toHaveBeenCalledWith('race-1', 'driver-1');
|
||||
});
|
||||
|
||||
@@ -95,8 +81,7 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<WithdrawFromRaceErrorCode, { message: string }>;
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(error.details?.message).toContain('Race race-unknown not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when registration is not found', async () => {
|
||||
const race = {
|
||||
@@ -122,8 +107,7 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<WithdrawFromRaceErrorCode, { message: string }>;
|
||||
expect(error.code).toBe('REGISTRATION_NOT_FOUND');
|
||||
expect(error.details?.message).toContain('Driver driver-unknown is not registered for race race-1');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when withdrawal is not allowed', async () => {
|
||||
const race = {
|
||||
@@ -149,8 +133,7 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<WithdrawFromRaceErrorCode, { message: string }>;
|
||||
expect(error.code).toBe('WITHDRAWAL_NOT_ALLOWED');
|
||||
expect(error.details?.message).toContain('Withdrawal is not allowed for this race');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('wraps repository errors and logs them', async () => {
|
||||
const race = {
|
||||
@@ -177,7 +160,6 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<WithdrawFromRaceErrorCode, { message: string }>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details?.message).toBe('DB failure');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user