refactor use cases
This commit is contained in:
@@ -2,7 +2,6 @@ import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { JoinLeagueUseCase, type JoinLeagueResult, type JoinLeagueInput, type JoinLeagueErrorCode } from './JoinLeagueUseCase';
|
||||
import { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import { LeagueMembership } from '../../domain/entities/LeagueMembership';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('JoinLeagueUseCase', () => {
|
||||
@@ -17,8 +16,6 @@ describe('JoinLeagueUseCase', () => {
|
||||
warn: Mock;
|
||||
error: Mock;
|
||||
};
|
||||
let output: UseCaseOutputPort<JoinLeagueResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
membershipRepository = {
|
||||
getMembership: vi.fn(),
|
||||
@@ -30,15 +27,8 @@ describe('JoinLeagueUseCase', () => {
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<JoinLeagueResult> & { present: Mock };
|
||||
|
||||
useCase = new JoinLeagueUseCase(
|
||||
membershipRepository as unknown as ILeagueMembershipRepository,
|
||||
logger as unknown as Logger,
|
||||
output,
|
||||
);
|
||||
useCase = new JoinLeagueUseCase(membershipRepository as unknown as ILeagueMembershipRepository,
|
||||
logger as unknown as Logger);
|
||||
});
|
||||
|
||||
it('should join league successfully', async () => {
|
||||
@@ -61,9 +51,7 @@ describe('JoinLeagueUseCase', () => {
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as JoinLeagueResult;
|
||||
expect(presented.membership.id).toBe('membership-1');
|
||||
const presented = expect(presented.membership.id).toBe('membership-1');
|
||||
expect(presented.membership.leagueId.toString()).toBe('league-1');
|
||||
expect(presented.membership.driverId.toString()).toBe('driver-1');
|
||||
expect(presented.membership.role.toString()).toBe('member');
|
||||
@@ -88,8 +76,7 @@ describe('JoinLeagueUseCase', () => {
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<JoinLeagueErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('ALREADY_MEMBER');
|
||||
expect(err.details?.message).toBe('Driver is already a member of this league or has a pending membership.');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return error on repository failure', async () => {
|
||||
const command: JoinLeagueInput = { leagueId: 'league-1', driverId: 'driver-1' };
|
||||
@@ -103,6 +90,5 @@ describe('JoinLeagueUseCase', () => {
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<JoinLeagueErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details?.message).toBe('Repository error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user