118 lines
4.1 KiB
TypeScript
118 lines
4.1 KiB
TypeScript
import { describe, it, expect, vi, type Mock } from 'vitest';
|
|
import {
|
|
UpdateLeagueMemberRoleUseCase,
|
|
type UpdateLeagueMemberRoleInput,
|
|
type UpdateLeagueMemberRoleResult,
|
|
type UpdateLeagueMemberRoleErrorCode,
|
|
} from './UpdateLeagueMemberRoleUseCase';
|
|
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
|
import type { UseCaseOutputPort } from '@core/shared/application';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
describe('UpdateLeagueMemberRoleUseCase', () => {
|
|
it('updates league member role successfully', async () => {
|
|
const mockMembership = {
|
|
id: 'league-1:driver-1',
|
|
leagueId: 'league-1',
|
|
driverId: 'driver-1',
|
|
role: 'member',
|
|
status: 'active',
|
|
joinedAt: new Date(),
|
|
};
|
|
|
|
const mockLeagueMembershipRepository = {
|
|
getLeagueMembers: vi.fn().mockResolvedValue([mockMembership]),
|
|
saveMembership: vi.fn().mockResolvedValue(undefined),
|
|
} as unknown as ILeagueMembershipRepository;
|
|
|
|
const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
|
|
present: vi.fn(),
|
|
};
|
|
|
|
const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);
|
|
|
|
const input: UpdateLeagueMemberRoleInput = {
|
|
leagueId: 'league-1',
|
|
targetDriverId: 'driver-1',
|
|
newRole: 'admin',
|
|
};
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
|
|
expect(mockLeagueMembershipRepository.getLeagueMembers).toHaveBeenCalledWith('league-1');
|
|
expect(mockLeagueMembershipRepository.saveMembership).toHaveBeenCalledTimes(1);
|
|
|
|
expect(output.present).toHaveBeenCalledTimes(1);
|
|
const presented = (output.present as Mock).mock.calls[0]![0] as UpdateLeagueMemberRoleResult;
|
|
|
|
expect(presented.membership.id).toBe('league-1:driver-1');
|
|
expect(presented.membership.leagueId.toString()).toBe('league-1');
|
|
expect(presented.membership.driverId.toString()).toBe('driver-1');
|
|
expect(presented.membership.role.toString()).toBe('admin');
|
|
});
|
|
|
|
it('returns error if membership not found', async () => {
|
|
const mockLeagueMembershipRepository = {
|
|
getLeagueMembers: vi.fn().mockResolvedValue([]),
|
|
} as unknown as ILeagueMembershipRepository;
|
|
|
|
const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
|
|
present: vi.fn(),
|
|
};
|
|
|
|
const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);
|
|
|
|
const input: UpdateLeagueMemberRoleInput = {
|
|
leagueId: 'league-1',
|
|
targetDriverId: 'driver-1',
|
|
newRole: 'admin',
|
|
};
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.unwrapErr() as ApplicationErrorCode<
|
|
UpdateLeagueMemberRoleErrorCode,
|
|
{ message: string }
|
|
>;
|
|
|
|
expect(error.code).toBe('MEMBERSHIP_NOT_FOUND');
|
|
expect(error.details.message).toBe('League membership not found');
|
|
expect(output.present).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('handles repository errors', async () => {
|
|
const mockLeagueMembershipRepository = {
|
|
getLeagueMembers: vi
|
|
.fn()
|
|
.mockRejectedValue(new Error('Database connection failed')),
|
|
} as unknown as ILeagueMembershipRepository;
|
|
|
|
const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
|
|
present: vi.fn(),
|
|
};
|
|
|
|
const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);
|
|
|
|
const input: UpdateLeagueMemberRoleInput = {
|
|
leagueId: 'league-1',
|
|
targetDriverId: 'driver-1',
|
|
newRole: 'admin',
|
|
};
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.unwrapErr() as ApplicationErrorCode<
|
|
UpdateLeagueMemberRoleErrorCode,
|
|
{ message: string }
|
|
>;
|
|
|
|
expect(error.code).toBe('REPOSITORY_ERROR');
|
|
expect(error.details.message).toBe('Failed to update league member role');
|
|
expect(output.present).not.toHaveBeenCalled();
|
|
});
|
|
}); |