refactor racing use cases
This commit is contained in:
@@ -1,13 +1,27 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetLeagueAdminPermissionsUseCase } from './GetLeagueAdminPermissionsUseCase';
|
||||
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
GetLeagueAdminPermissionsUseCase,
|
||||
type GetLeagueAdminPermissionsInput,
|
||||
type GetLeagueAdminPermissionsResult,
|
||||
type GetLeagueAdminPermissionsErrorCode,
|
||||
} 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';
|
||||
|
||||
describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
let mockLeagueRepo: ILeagueRepository;
|
||||
let mockMembershipRepo: ILeagueMembershipRepository;
|
||||
let mockFindById: Mock;
|
||||
let mockGetMembership: Mock;
|
||||
let output: UseCaseOutputPort<GetLeagueAdminPermissionsResult> & { present: Mock };
|
||||
const logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as any;
|
||||
|
||||
beforeEach(() => {
|
||||
mockFindById = vi.fn();
|
||||
@@ -22,6 +36,7 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
findByOwnerId: vi.fn(),
|
||||
searchByName: vi.fn(),
|
||||
} as ILeagueRepository;
|
||||
|
||||
mockMembershipRepo = {
|
||||
getMembership: mockGetMembership,
|
||||
getMembershipsForDriver: vi.fn(),
|
||||
@@ -33,80 +48,134 @@ describe('GetLeagueAdminPermissionsUseCase', () => {
|
||||
countByLeagueId: vi.fn(),
|
||||
getLeagueMembers: vi.fn(),
|
||||
} as ILeagueMembershipRepository;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetLeagueAdminPermissionsResult> & { present: Mock };
|
||||
});
|
||||
|
||||
const createUseCase = () => new GetLeagueAdminPermissionsUseCase(
|
||||
mockLeagueRepo,
|
||||
mockMembershipRepo,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
|
||||
const params = {
|
||||
const input: GetLeagueAdminPermissionsInput = {
|
||||
leagueId: 'league1',
|
||||
performerDriverId: 'driver1',
|
||||
};
|
||||
|
||||
it('should return no permissions when league not found', async () => {
|
||||
it('returns LEAGUE_NOT_FOUND when league does not exist and does not call output', async () => {
|
||||
mockFindById.mockResolvedValue(null);
|
||||
|
||||
const useCase = createUseCase();
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toEqual({ canRemoveMember: false, canUpdateRoles: false });
|
||||
expect(result.isErr()).toBe(true);
|
||||
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('should return no permissions when membership not found', async () => {
|
||||
it('returns USER_NOT_MEMBER when membership is missing and does not call output', async () => {
|
||||
mockFindById.mockResolvedValue({ id: 'league1' });
|
||||
mockGetMembership.mockResolvedValue(null);
|
||||
|
||||
const useCase = createUseCase();
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toEqual({ canRemoveMember: false, canUpdateRoles: false });
|
||||
expect(result.isErr()).toBe(true);
|
||||
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('should return no permissions when membership not active', async () => {
|
||||
it('returns USER_NOT_MEMBER when membership is not active and does not call output', async () => {
|
||||
mockFindById.mockResolvedValue({ id: 'league1' });
|
||||
mockGetMembership.mockResolvedValue({ status: 'inactive', role: 'admin' });
|
||||
|
||||
const useCase = createUseCase();
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toEqual({ canRemoveMember: false, canUpdateRoles: false });
|
||||
expect(result.isErr()).toBe(true);
|
||||
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('should return no permissions when role is member', async () => {
|
||||
it('returns USER_NOT_MEMBER when role is member and does not call output', async () => {
|
||||
mockFindById.mockResolvedValue({ id: 'league1' });
|
||||
mockGetMembership.mockResolvedValue({ status: 'active', role: 'member' });
|
||||
|
||||
const useCase = createUseCase();
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toEqual({ canRemoveMember: false, canUpdateRoles: false });
|
||||
expect(result.isErr()).toBe(true);
|
||||
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('should return permissions when role is admin', async () => {
|
||||
mockFindById.mockResolvedValue({ id: 'league1' });
|
||||
it('returns admin permissions for admin role and calls output once', async () => {
|
||||
const league = { id: 'league1' } as any;
|
||||
mockFindById.mockResolvedValue(league);
|
||||
mockGetMembership.mockResolvedValue({ status: 'active', role: 'admin' });
|
||||
|
||||
const useCase = createUseCase();
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toEqual({ canRemoveMember: true, canUpdateRoles: 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);
|
||||
expect(presented.permissions).toEqual({
|
||||
canManageSchedule: true,
|
||||
canManageMembers: true,
|
||||
canManageSponsorships: true,
|
||||
canManageScoring: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return permissions when role is owner', async () => {
|
||||
mockFindById.mockResolvedValue({ id: 'league1' });
|
||||
it('returns admin permissions for owner role and calls output once', async () => {
|
||||
const league = { id: 'league1' } as any;
|
||||
mockFindById.mockResolvedValue(league);
|
||||
mockGetMembership.mockResolvedValue({ status: 'active', role: 'owner' });
|
||||
|
||||
const useCase = createUseCase();
|
||||
const result = await useCase.execute(params);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toEqual({ canRemoveMember: true, canUpdateRoles: 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);
|
||||
expect(presented.permissions).toEqual({
|
||||
canManageSchedule: true,
|
||||
canManageMembers: true,
|
||||
canManageSponsorships: true,
|
||||
canManageScoring: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('wraps repository errors in REPOSITORY_ERROR and does not call output', async () => {
|
||||
const error = new Error('repo failed');
|
||||
mockFindById.mockRejectedValue(error);
|
||||
|
||||
const useCase = createUseCase();
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
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