refactor use cases
This commit is contained in:
@@ -4,7 +4,6 @@ import type { IProtestRepository } from '../../domain/repositories/IProtestRepos
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
|
||||
describe('ReviewProtestUseCase', () => {
|
||||
let useCase: ReviewProtestUseCase;
|
||||
@@ -12,8 +11,6 @@ describe('ReviewProtestUseCase', () => {
|
||||
let raceRepository: { findById: Mock };
|
||||
let leagueMembershipRepository: { getLeagueMembers: Mock };
|
||||
let logger: { debug: Mock; info: Mock; warn: Mock; error: Mock };
|
||||
let output: UseCaseOutputPort<ReviewProtestResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
protestRepository = { findById: vi.fn(), update: vi.fn() };
|
||||
raceRepository = { findById: vi.fn() };
|
||||
@@ -24,14 +21,10 @@ describe('ReviewProtestUseCase', () => {
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = { present: vi.fn() } as unknown as UseCaseOutputPort<ReviewProtestResult> & { present: Mock };
|
||||
useCase = new ReviewProtestUseCase(
|
||||
protestRepository as unknown as IProtestRepository,
|
||||
useCase = new ReviewProtestUseCase(protestRepository as unknown as IProtestRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
leagueMembershipRepository as unknown as ILeagueMembershipRepository,
|
||||
logger as unknown as Logger,
|
||||
output,
|
||||
);
|
||||
logger as unknown as Logger);
|
||||
});
|
||||
|
||||
it('should return protest not found error', async () => {
|
||||
@@ -52,8 +45,7 @@ describe('ReviewProtestUseCase', () => {
|
||||
code: 'PROTEST_NOT_FOUND',
|
||||
details: { message: 'Protest not found' },
|
||||
});
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return race not found error', async () => {
|
||||
const mockProtest = { raceId: 'race-1' };
|
||||
@@ -75,8 +67,7 @@ describe('ReviewProtestUseCase', () => {
|
||||
code: 'RACE_NOT_FOUND',
|
||||
details: { message: 'Race not found' },
|
||||
});
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return not league admin error', async () => {
|
||||
const mockProtest = { raceId: 'race-1', uphold: vi.fn(), dismiss: vi.fn() };
|
||||
@@ -100,8 +91,7 @@ describe('ReviewProtestUseCase', () => {
|
||||
code: 'NOT_LEAGUE_ADMIN',
|
||||
details: { message: 'Only league owners and admins can review protests' },
|
||||
});
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should uphold protest successfully', async () => {
|
||||
const mockProtest = { id: 'protest-1', raceId: 'race-1', uphold: vi.fn().mockReturnValue({}), dismiss: vi.fn() };
|
||||
@@ -122,15 +112,13 @@ describe('ReviewProtestUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.uphold());
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as ReviewProtestResult;
|
||||
expect(presented).toEqual({
|
||||
const value = result.unwrap();
|
||||
expect(value).toEqual({
|
||||
leagueId: 'league-1',
|
||||
protestId: 'protest-1',
|
||||
status: 'upheld',
|
||||
});
|
||||
expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.uphold());
|
||||
});
|
||||
|
||||
it('should dismiss protest successfully', async () => {
|
||||
@@ -152,15 +140,13 @@ describe('ReviewProtestUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.dismiss());
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as ReviewProtestResult;
|
||||
expect(presented).toEqual({
|
||||
const value = result.unwrap();
|
||||
expect(value).toEqual({
|
||||
leagueId: 'league-1',
|
||||
protestId: 'protest-1',
|
||||
status: 'dismissed',
|
||||
});
|
||||
expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.dismiss());
|
||||
});
|
||||
|
||||
it('should return repository error when update throws', async () => {
|
||||
@@ -185,6 +171,5 @@ describe('ReviewProtestUseCase', () => {
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<ReviewProtestErrorCode, { message: string }>;
|
||||
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