420 lines
14 KiB
TypeScript
420 lines
14 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { RosterAdminMutation } from './RosterAdminMutation';
|
|
import { LeagueService } from '@/lib/services/leagues/LeagueService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/leagues/LeagueService', () => {
|
|
return {
|
|
LeagueService: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/gateways/api/leagues/LeaguesApiClient', () => {
|
|
return {
|
|
LeaguesApiClient: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/infrastructure/logging/ConsoleErrorReporter', () => {
|
|
return {
|
|
ConsoleErrorReporter: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/infrastructure/logging/ConsoleLogger', () => {
|
|
return {
|
|
ConsoleLogger: vi.fn(),
|
|
};
|
|
});
|
|
|
|
describe('RosterAdminMutation', () => {
|
|
let mutation: RosterAdminMutation;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockServiceInstance = {
|
|
approveJoinRequest: vi.fn(),
|
|
rejectJoinRequest: vi.fn(),
|
|
updateMemberRole: vi.fn(),
|
|
removeMember: vi.fn(),
|
|
};
|
|
// Use mockImplementation to return the instance
|
|
(LeagueService as any).mockImplementation(function() {
|
|
return mockServiceInstance;
|
|
});
|
|
mutation = new RosterAdminMutation();
|
|
});
|
|
|
|
describe('approveJoinRequest', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully approve join request', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
mockServiceInstance.approveJoinRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.approveJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.approveJoinRequest).toHaveBeenCalledWith(leagueId, joinRequestId);
|
|
expect(mockServiceInstance.approveJoinRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during approval', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
const serviceError = new Error('Service error');
|
|
mockServiceInstance.approveJoinRequest.mockRejectedValue(serviceError);
|
|
|
|
// Act
|
|
const result = await mutation.approveJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to approve join request');
|
|
expect(mockServiceInstance.approveJoinRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning error result', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
|
mockServiceInstance.approveJoinRequest.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.approveJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to approve join request');
|
|
expect(mockServiceInstance.approveJoinRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid input', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
mockServiceInstance.approveJoinRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.approveJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.approveJoinRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('rejectJoinRequest', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully reject join request', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
mockServiceInstance.rejectJoinRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.rejectJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.rejectJoinRequest).toHaveBeenCalledWith(leagueId, joinRequestId);
|
|
expect(mockServiceInstance.rejectJoinRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during rejection', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
const serviceError = new Error('Service error');
|
|
mockServiceInstance.rejectJoinRequest.mockRejectedValue(serviceError);
|
|
|
|
// Act
|
|
const result = await mutation.rejectJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to reject join request');
|
|
expect(mockServiceInstance.rejectJoinRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning error result', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
|
mockServiceInstance.rejectJoinRequest.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.rejectJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to reject join request');
|
|
expect(mockServiceInstance.rejectJoinRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid input', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
mockServiceInstance.rejectJoinRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.rejectJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.rejectJoinRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('updateMemberRole', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully update member role to admin', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
const role = 'admin';
|
|
mockServiceInstance.updateMemberRole.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.updateMemberRole(leagueId, driverId, role);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.updateMemberRole).toHaveBeenCalledWith(leagueId, driverId, role);
|
|
expect(mockServiceInstance.updateMemberRole).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should successfully update member role to member', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
const role = 'member';
|
|
mockServiceInstance.updateMemberRole.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.updateMemberRole(leagueId, driverId, role);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.updateMemberRole).toHaveBeenCalledWith(leagueId, driverId, role);
|
|
expect(mockServiceInstance.updateMemberRole).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during role update', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
const role = 'admin';
|
|
const serviceError = new Error('Service error');
|
|
mockServiceInstance.updateMemberRole.mockRejectedValue(serviceError);
|
|
|
|
// Act
|
|
const result = await mutation.updateMemberRole(leagueId, driverId, role);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to update member role');
|
|
expect(mockServiceInstance.updateMemberRole).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning error result', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
const role = 'admin';
|
|
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
|
mockServiceInstance.updateMemberRole.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.updateMemberRole(leagueId, driverId, role);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to update member role');
|
|
expect(mockServiceInstance.updateMemberRole).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid input', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
const role = 'admin';
|
|
mockServiceInstance.updateMemberRole.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.updateMemberRole(leagueId, driverId, role);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.updateMemberRole).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('removeMember', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully remove member', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
mockServiceInstance.removeMember.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.removeMember(leagueId, driverId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.removeMember).toHaveBeenCalledWith(leagueId, driverId);
|
|
expect(mockServiceInstance.removeMember).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during member removal', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
const serviceError = new Error('Service error');
|
|
mockServiceInstance.removeMember.mockRejectedValue(serviceError);
|
|
|
|
// Act
|
|
const result = await mutation.removeMember(leagueId, driverId);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to remove member');
|
|
expect(mockServiceInstance.removeMember).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning error result', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
|
mockServiceInstance.removeMember.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.removeMember(leagueId, driverId);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('Failed to remove member');
|
|
expect(mockServiceInstance.removeMember).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid input', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
mockServiceInstance.removeMember.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.removeMember(leagueId, driverId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.removeMember).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('service instantiation', () => {
|
|
it('should create LeagueService instance', () => {
|
|
// Arrange & Act
|
|
const mutation = new RosterAdminMutation();
|
|
|
|
// Assert
|
|
expect(mutation).toBeInstanceOf(RosterAdminMutation);
|
|
});
|
|
});
|
|
|
|
describe('result shape', () => {
|
|
it('should return void on successful approval', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
mockServiceInstance.approveJoinRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.approveJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
|
|
it('should return void on successful rejection', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const joinRequestId = 'join-456';
|
|
mockServiceInstance.rejectJoinRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.rejectJoinRequest(leagueId, joinRequestId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
|
|
it('should return void on successful role update', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
const role = 'admin';
|
|
mockServiceInstance.updateMemberRole.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.updateMemberRole(leagueId, driverId, role);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
|
|
it('should return void on successful member removal', async () => {
|
|
// Arrange
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-456';
|
|
mockServiceInstance.removeMember.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.removeMember(leagueId, driverId);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
});
|