refactor use cases
This commit is contained in:
@@ -7,7 +7,6 @@ import {
|
||||
} from './GetLeagueAdminPermissionsUseCase';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
@@ -16,7 +15,6 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
let mockMembershipRepo: ILeagueMembershipRepository;
|
||||
let mockFindById: Mock;
|
||||
let mockGetMembership: Mock;
|
||||
let output: UseCaseOutputPort<GetLeagueAdminPermissionsResult> & { present: Mock };
|
||||
const logger: Logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
@@ -50,17 +48,11 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
getLeagueMembers: vi.fn(),
|
||||
} as ILeagueMembershipRepository;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetLeagueAdminPermissionsResult> & { present: Mock };
|
||||
});
|
||||
});
|
||||
|
||||
const createUseCase = () => new GetLeagueAdminPermissionsUseCase(
|
||||
mockLeagueRepo,
|
||||
const createUseCase = () => new GetLeagueAdminPermissionsUseCase(mockLeagueRepo,
|
||||
mockMembershipRepo,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
logger);
|
||||
|
||||
const input: GetLeagueAdminPermissionsInput = {
|
||||
leagueId: 'league1',
|
||||
@@ -77,8 +69,7 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(err.details.message).toBe('League not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns USER_NOT_MEMBER when membership is missing and does not call output', async () => {
|
||||
mockFindById.mockResolvedValue({ id: 'league1' });
|
||||
@@ -91,8 +82,7 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('USER_NOT_MEMBER');
|
||||
expect(err.details.message).toBe('User is not a member of this league');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns USER_NOT_MEMBER when membership is not active and does not call output', async () => {
|
||||
mockFindById.mockResolvedValue({ id: 'league1' });
|
||||
@@ -105,8 +95,7 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('USER_NOT_MEMBER');
|
||||
expect(err.details.message).toBe('User is not a member of this league');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns USER_NOT_MEMBER when role is member and does not call output', async () => {
|
||||
mockFindById.mockResolvedValue({ id: 'league1' });
|
||||
@@ -119,8 +108,7 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('USER_NOT_MEMBER');
|
||||
expect(err.details.message).toBe('User is not a member of this league');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns admin permissions for admin role and calls output once', async () => {
|
||||
const league = { id: 'league1' } as unknown as { id: string };
|
||||
@@ -133,9 +121,7 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]?.[0] as GetLeagueAdminPermissionsResult;
|
||||
expect(presented.league).toBe(league);
|
||||
const presented = expect(presented.league).toBe(league);
|
||||
expect(presented.permissions).toEqual({
|
||||
canManageSchedule: true,
|
||||
canManageMembers: true,
|
||||
@@ -155,9 +141,7 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]?.[0] as GetLeagueAdminPermissionsResult;
|
||||
expect(presented.league).toBe(league);
|
||||
const presented = expect(presented.league).toBe(league);
|
||||
expect(presented.permissions).toEqual({
|
||||
canManageSchedule: true,
|
||||
canManageMembers: true,
|
||||
@@ -177,6 +161,5 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('repo failed');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user