refactor use cases
This commit is contained in:
@@ -7,7 +7,6 @@ import {
|
||||
} from './LeaveTeamUseCase';
|
||||
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
|
||||
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('LeaveTeamUseCase', () => {
|
||||
@@ -18,8 +17,6 @@ describe('LeaveTeamUseCase', () => {
|
||||
removeMembership: Mock;
|
||||
};
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<LeaveTeamResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
teamRepository = {
|
||||
findById: vi.fn(),
|
||||
@@ -34,16 +31,9 @@ describe('LeaveTeamUseCase', () => {
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<LeaveTeamResult> & { present: Mock };
|
||||
|
||||
useCase = new LeaveTeamUseCase(
|
||||
teamRepository as unknown as ITeamRepository,
|
||||
useCase = new LeaveTeamUseCase(teamRepository as unknown as ITeamRepository,
|
||||
membershipRepository as unknown as ITeamMembershipRepository,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
logger);
|
||||
});
|
||||
|
||||
it('should leave team successfully', async () => {
|
||||
@@ -65,9 +55,7 @@ describe('LeaveTeamUseCase', () => {
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as LeaveTeamResult;
|
||||
expect(presented.team.id).toBe('team-1');
|
||||
const presented = expect(presented.team.id).toBe('team-1');
|
||||
expect(presented.previousMembership).toEqual(membership);
|
||||
});
|
||||
|
||||
@@ -85,8 +73,7 @@ describe('LeaveTeamUseCase', () => {
|
||||
>;
|
||||
expect(err.code).toBe('TEAM_NOT_FOUND');
|
||||
expect(err.details.message).toBe('Team team-1 not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return error when not a member', async () => {
|
||||
const input: LeaveTeamInput = { teamId: 'team-1', driverId: 'driver-1' };
|
||||
@@ -103,8 +90,7 @@ describe('LeaveTeamUseCase', () => {
|
||||
>;
|
||||
expect(err.code).toBe('NOT_MEMBER');
|
||||
expect(err.details.message).toBe('Not a member of this team');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return error when owner tries to leave', async () => {
|
||||
const input: LeaveTeamInput = { teamId: 'team-1', driverId: 'driver-1' };
|
||||
@@ -129,8 +115,7 @@ describe('LeaveTeamUseCase', () => {
|
||||
expect(err.details.message).toBe(
|
||||
'Team owner cannot leave. Transfer ownership or disband team first.',
|
||||
);
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return error on repository failure', async () => {
|
||||
const input: LeaveTeamInput = { teamId: 'team-1', driverId: 'driver-1' };
|
||||
@@ -147,6 +132,5 @@ describe('LeaveTeamUseCase', () => {
|
||||
>;
|
||||
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