refactor racing use cases
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { FileProtestUseCase } from './FileProtestUseCase';
|
||||
import { FileProtestUseCase, type FileProtestInput, type FileProtestResult, type FileProtestErrorCode } from './FileProtestUseCase';
|
||||
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Result } from '@core/shared/application/Result';
|
||||
|
||||
describe('FileProtestUseCase', () => {
|
||||
let mockProtestRepo: {
|
||||
@@ -14,6 +17,7 @@ describe('FileProtestUseCase', () => {
|
||||
let mockLeagueMembershipRepo: {
|
||||
getLeagueMembers: Mock;
|
||||
};
|
||||
let output: UseCaseOutputPort<FileProtestResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
mockProtestRepo = {
|
||||
@@ -25,6 +29,9 @@ describe('FileProtestUseCase', () => {
|
||||
mockLeagueMembershipRepo = {
|
||||
getLeagueMembers: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<FileProtestResult> & { present: Mock };
|
||||
});
|
||||
|
||||
it('should return error when race does not exist', async () => {
|
||||
@@ -32,6 +39,7 @@ describe('FileProtestUseCase', () => {
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
output,
|
||||
);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue(null);
|
||||
@@ -41,10 +49,13 @@ describe('FileProtestUseCase', () => {
|
||||
protestingDriverId: 'driver1',
|
||||
accusedDriverId: 'driver2',
|
||||
incident: { lap: 5, description: 'Collision' },
|
||||
});
|
||||
} as FileProtestInput);
|
||||
|
||||
expect(result.isOk()).toBe(false);
|
||||
expect(result.error!.details.message).toBe('Race not found');
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('RACE_NOT_FOUND');
|
||||
expect(err.details?.message).toBe('Race not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error when protesting against self', async () => {
|
||||
@@ -52,6 +63,7 @@ describe('FileProtestUseCase', () => {
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
output,
|
||||
);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
|
||||
@@ -61,10 +73,13 @@ describe('FileProtestUseCase', () => {
|
||||
protestingDriverId: 'driver1',
|
||||
accusedDriverId: 'driver1',
|
||||
incident: { lap: 5, description: 'Collision' },
|
||||
});
|
||||
} as FileProtestInput);
|
||||
|
||||
expect(result.isOk()).toBe(false);
|
||||
expect(result.error!.details.message).toBe('Cannot file a protest against yourself');
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('SELF_PROTEST');
|
||||
expect(err.details?.message).toBe('Cannot file a protest against yourself');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error when protesting driver is not an active member', async () => {
|
||||
@@ -72,6 +87,7 @@ describe('FileProtestUseCase', () => {
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
output,
|
||||
);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
|
||||
@@ -84,10 +100,13 @@ describe('FileProtestUseCase', () => {
|
||||
protestingDriverId: 'driver1',
|
||||
accusedDriverId: 'driver2',
|
||||
incident: { lap: 5, description: 'Collision' },
|
||||
});
|
||||
} as FileProtestInput);
|
||||
|
||||
expect(result.isOk()).toBe(false);
|
||||
expect(result.error!.details.message).toBe('Protesting driver is not an active member of this league');
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('NOT_MEMBER');
|
||||
expect(err.details?.message).toBe('Protesting driver is not an active member of this league');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should create protest and return protestId on success', async () => {
|
||||
@@ -95,6 +114,7 @@ describe('FileProtestUseCase', () => {
|
||||
mockProtestRepo as unknown as IProtestRepository,
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
||||
output,
|
||||
);
|
||||
|
||||
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
|
||||
@@ -110,12 +130,10 @@ describe('FileProtestUseCase', () => {
|
||||
incident: { lap: 5, description: 'Collision' },
|
||||
comment: 'Test comment',
|
||||
proofVideoUrl: 'http://example.com/video',
|
||||
});
|
||||
} as FileProtestInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toEqual({
|
||||
protestId: expect.any(String),
|
||||
});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(mockProtestRepo.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
raceId: 'race1',
|
||||
@@ -127,5 +145,14 @@ describe('FileProtestUseCase', () => {
|
||||
status: 'pending',
|
||||
})
|
||||
);
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = (output.present as unknown as Mock).mock.calls[0][0] as FileProtestResult;
|
||||
expect(presented.protest.raceId).toBe('race1');
|
||||
expect(presented.protest.protestingDriverId).toBe('driver1');
|
||||
expect(presented.protest.accusedDriverId).toBe('driver2');
|
||||
expect(presented.protest.incident).toEqual({ lap: 5, description: 'Collision', timeInRace: undefined });
|
||||
expect(presented.protest.comment).toBe('Test comment');
|
||||
expect(presented.protest.proofVideoUrl).toBe('http://example.com/video');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user