refactor
This commit is contained in:
113
core/racing/application/use-cases/CancelRaceUseCase.test.ts
Normal file
113
core/racing/application/use-cases/CancelRaceUseCase.test.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { CancelRaceUseCase } from './CancelRaceUseCase';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import { SessionType } from '../../domain/value-objects/SessionType';
|
||||
import { RacingDomainInvariantError, RacingDomainValidationError } from '../../domain/errors/RacingDomainError';
|
||||
|
||||
describe('CancelRaceUseCase', () => {
|
||||
let useCase: CancelRaceUseCase;
|
||||
let raceRepository: {
|
||||
findById: Mock;
|
||||
update: Mock;
|
||||
};
|
||||
let logger: {
|
||||
debug: Mock;
|
||||
warn: Mock;
|
||||
info: Mock;
|
||||
error: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
raceRepository = {
|
||||
findById: vi.fn(),
|
||||
update: vi.fn(),
|
||||
};
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
info: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
useCase = new CancelRaceUseCase(raceRepository as unknown as IRaceRepository, logger as unknown as Logger);
|
||||
});
|
||||
|
||||
it('should cancel race successfully', async () => {
|
||||
const raceId = 'race-1';
|
||||
const race = Race.create({
|
||||
id: raceId,
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date(),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'scheduled',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
|
||||
const result = await useCase.execute({ raceId });
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(raceRepository.findById).toHaveBeenCalledWith(raceId);
|
||||
expect(raceRepository.update).toHaveBeenCalledWith(expect.objectContaining({ id: raceId, status: 'cancelled' }));
|
||||
expect(logger.info).toHaveBeenCalledWith(`[CancelRaceUseCase] Race ${raceId} cancelled successfully.`);
|
||||
});
|
||||
|
||||
it('should return error if race not found', async () => {
|
||||
const raceId = 'race-1';
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ raceId });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toBeInstanceOf(RacingDomainValidationError);
|
||||
expect(result.unwrapErr().message).toBe('Race not found');
|
||||
expect(logger.warn).toHaveBeenCalledWith(`[CancelRaceUseCase] Race with ID ${raceId} not found.`);
|
||||
});
|
||||
|
||||
it('should return domain error if race is already cancelled', async () => {
|
||||
const raceId = 'race-1';
|
||||
const race = Race.create({
|
||||
id: raceId,
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date(),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'cancelled',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
|
||||
const result = await useCase.execute({ raceId });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toBeInstanceOf(RacingDomainInvariantError);
|
||||
expect(result.unwrapErr().message).toBe('Race is already cancelled');
|
||||
expect(logger.warn).toHaveBeenCalledWith(`[CancelRaceUseCase] Domain error cancelling race ${raceId}: Race is already cancelled`);
|
||||
});
|
||||
|
||||
it('should return domain error if race is completed', async () => {
|
||||
const raceId = 'race-1';
|
||||
const race = Race.create({
|
||||
id: raceId,
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date(),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'completed',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
|
||||
const result = await useCase.execute({ raceId });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toBeInstanceOf(RacingDomainInvariantError);
|
||||
expect(result.unwrapErr().message).toBe('Cannot cancel a completed race');
|
||||
expect(logger.warn).toHaveBeenCalledWith(`[CancelRaceUseCase] Domain error cancelling race ${raceId}: Cannot cancel a completed race`);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user