website refactor
This commit is contained in:
@@ -14,6 +14,7 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
let raceRepository: { findById: ReturnType<typeof vi.fn> };
|
||||
let registrationRepository: { isRegistered: ReturnType<typeof vi.fn>; withdraw: ReturnType<typeof vi.fn> };
|
||||
let logger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
raceRepository = {
|
||||
findById: vi.fn(),
|
||||
@@ -29,14 +30,12 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
|
||||
};
|
||||
} as any;
|
||||
});
|
||||
|
||||
const createUseCase = () =>
|
||||
new WithdrawFromRaceUseCase(raceRepository as unknown as IRaceRepository,
|
||||
registrationRepository as unknown as IRaceRegistrationRepository,
|
||||
new WithdrawFromRaceUseCase(raceRepository as any,
|
||||
registrationRepository as any,
|
||||
logger);
|
||||
|
||||
it('withdraws from race successfully', async () => {
|
||||
@@ -45,9 +44,7 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
isUpcoming: vi.fn().mockReturnValue(true),
|
||||
};
|
||||
|
||||
raceRepository.findById.mockResolvedValue(
|
||||
race as unknown as Awaited<ReturnType<IRaceRepository['findById']>>,
|
||||
);
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
registrationRepository.isRegistered.mockResolvedValue(true);
|
||||
registrationRepository.withdraw.mockResolvedValue(undefined);
|
||||
|
||||
@@ -61,7 +58,8 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const presented = result.unwrap();
|
||||
expect(presented.status).toBe('withdrawn');
|
||||
expect(registrationRepository.withdraw).toHaveBeenCalledWith('race-1', 'driver-1');
|
||||
});
|
||||
|
||||
@@ -81,7 +79,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');
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when registration is not found', async () => {
|
||||
const race = {
|
||||
@@ -89,9 +87,7 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
isUpcoming: vi.fn().mockReturnValue(true),
|
||||
};
|
||||
|
||||
raceRepository.findById.mockResolvedValue(
|
||||
race as unknown as Awaited<ReturnType<IRaceRepository['findById']>>,
|
||||
);
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
registrationRepository.isRegistered.mockResolvedValue(false);
|
||||
|
||||
const useCase = createUseCase();
|
||||
@@ -107,7 +103,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');
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when withdrawal is not allowed', async () => {
|
||||
const race = {
|
||||
@@ -115,9 +111,7 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
isUpcoming: vi.fn().mockReturnValue(false),
|
||||
};
|
||||
|
||||
raceRepository.findById.mockResolvedValue(
|
||||
race as unknown as Awaited<ReturnType<IRaceRepository['findById']>>,
|
||||
);
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
registrationRepository.isRegistered.mockResolvedValue(true);
|
||||
|
||||
const useCase = createUseCase();
|
||||
@@ -133,7 +127,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');
|
||||
});
|
||||
});
|
||||
|
||||
it('wraps repository errors and logs them', async () => {
|
||||
const race = {
|
||||
@@ -141,9 +135,7 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
isUpcoming: vi.fn().mockReturnValue(true),
|
||||
};
|
||||
|
||||
raceRepository.findById.mockResolvedValue(
|
||||
race as unknown as Awaited<ReturnType<IRaceRepository['findById']>>,
|
||||
);
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
registrationRepository.isRegistered.mockResolvedValue(true);
|
||||
registrationRepository.withdraw.mockRejectedValue(new Error('DB failure'));
|
||||
|
||||
@@ -162,4 +154,4 @@ describe('WithdrawFromRaceUseCase', () => {
|
||||
expect(error.details?.message).toBe('DB failure');
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user