refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -6,27 +6,18 @@ import {
type RemoveLeagueMemberErrorCode,
} from './RemoveLeagueMemberUseCase';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
describe('RemoveLeagueMemberUseCase', () => {
let useCase: RemoveLeagueMemberUseCase;
let leagueMembershipRepository: { getMembership: Mock; getLeagueMembers: Mock; saveMembership: Mock };
let output: UseCaseOutputPort<RemoveLeagueMemberResult> & { present: Mock };
beforeEach(() => {
leagueMembershipRepository = {
getMembership: vi.fn(),
getLeagueMembers: vi.fn(),
saveMembership: vi.fn(),
};
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<RemoveLeagueMemberResult> & { present: Mock };
useCase = new RemoveLeagueMemberUseCase(
leagueMembershipRepository as unknown as ILeagueMembershipRepository,
output,
);
useCase = new RemoveLeagueMemberUseCase(leagueMembershipRepository as unknown as ILeagueMembershipRepository);
});
it('should remove league member by setting status to inactive', async () => {
@@ -48,19 +39,14 @@ describe('RemoveLeagueMemberUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
const removeResult = result.unwrap();
expect(removeResult.success).toBe(true);
expect(removeResult.message).toBeDefined();
expect(leagueMembershipRepository.saveMembership).toHaveBeenCalledTimes(1);
const savedMembership = leagueMembershipRepository.saveMembership.mock.calls[0]?.[0];
expect(savedMembership).toBeDefined();
expect(savedMembership!.status.toString()).toBe('inactive');
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({
leagueId,
memberId: targetDriverId,
removedRole: 'member',
} satisfies RemoveLeagueMemberResult);
});
it('should return error if membership not found', async () => {
@@ -79,8 +65,6 @@ describe('RemoveLeagueMemberUseCase', () => {
expect(error.code).toBe('MEMBERSHIP_NOT_FOUND');
expect(error.details.message).toBe('Membership not found for given league and driver');
expect(output.present).not.toHaveBeenCalled();
});
it('should return repository error when an exception occurs', async () => {
@@ -102,8 +86,6 @@ describe('RemoveLeagueMemberUseCase', () => {
expect(error.code).toBe('REPOSITORY_ERROR');
expect(error.details.message).toBe('DB error');
expect(output.present).not.toHaveBeenCalled();
});
it('prevents removing the last owner', async () => {
@@ -131,7 +113,6 @@ describe('RemoveLeagueMemberUseCase', () => {
>;
expect(error.code).toBe('CANNOT_REMOVE_LAST_OWNER');
expect(output.present).not.toHaveBeenCalled();
expect(leagueMembershipRepository.saveMembership).not.toHaveBeenCalled();
});
});
});