328 lines
11 KiB
TypeScript
328 lines
11 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { RejectSponsorshipRequestMutation } from './RejectSponsorshipRequestMutation';
|
|
import { SponsorshipRequestsService } from '@/lib/services/sponsors/SponsorshipRequestsService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/sponsors/SponsorshipRequestsService', () => {
|
|
return {
|
|
SponsorshipRequestsService: vi.fn(),
|
|
};
|
|
});
|
|
|
|
describe('RejectSponsorshipRequestMutation', () => {
|
|
let mutation: RejectSponsorshipRequestMutation;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockServiceInstance = {
|
|
rejectRequest: vi.fn(),
|
|
};
|
|
// Use mockImplementation to return the instance
|
|
(SponsorshipRequestsService as any).mockImplementation(function() {
|
|
return mockServiceInstance;
|
|
});
|
|
mutation = new RejectSponsorshipRequestMutation();
|
|
});
|
|
|
|
describe('execute', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully reject sponsorship request with valid input', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledWith(command);
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should successfully reject sponsorship request with null reason', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: null,
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledWith(command);
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should successfully reject sponsorship request with empty reason', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: '',
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledWith(command);
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during rejection', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
const serviceError = new Error('Service error');
|
|
mockServiceInstance.rejectRequest.mockRejectedValue(serviceError);
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('REJECT_SPONSORSHIP_REQUEST_FAILED');
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning error result', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('REJECT_SPONSORSHIP_REQUEST_FAILED');
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning validation error', async () => {
|
|
// Arrange
|
|
const command = { bio: 'Test bio', country: 'US' };
|
|
const domainError = { type: 'validationError', message: 'Invalid request ID' };
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command as any);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('REJECT_SPONSORSHIP_REQUEST_FAILED');
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning notFound error', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
const domainError = { type: 'notFound', message: 'Sponsorship request not found' };
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('REJECT_SPONSORSHIP_REQUEST_FAILED');
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning unauthorized error', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
const domainError = { type: 'unauthorized', message: 'Insufficient permissions' };
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('REJECT_SPONSORSHIP_REQUEST_FAILED');
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('error mapping', () => {
|
|
it('should map various domain errors to mutation errors', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
const testCases = [
|
|
{ domainError: { type: 'notFound' }, expectedError: 'REJECT_SPONSORSHIP_REQUEST_FAILED' },
|
|
{ domainError: { type: 'unauthorized' }, expectedError: 'REJECT_SPONSORSHIP_REQUEST_FAILED' },
|
|
{ domainError: { type: 'validationError' }, expectedError: 'REJECT_SPONSORSHIP_REQUEST_FAILED' },
|
|
{ domainError: { type: 'serverError' }, expectedError: 'REJECT_SPONSORSHIP_REQUEST_FAILED' },
|
|
{ domainError: { type: 'networkError' }, expectedError: 'REJECT_SPONSORSHIP_REQUEST_FAILED' },
|
|
{ domainError: { type: 'notImplemented' }, expectedError: 'REJECT_SPONSORSHIP_REQUEST_FAILED' },
|
|
{ domainError: { type: 'unknown' }, expectedError: 'REJECT_SPONSORSHIP_REQUEST_FAILED' },
|
|
];
|
|
|
|
for (const testCase of testCases) {
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.err(testCase.domainError));
|
|
|
|
const result = await mutation.execute(command);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe(testCase.expectedError);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid command input', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle empty requestId gracefully', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: '',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledWith(command);
|
|
});
|
|
|
|
it('should handle empty actorDriverId gracefully', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: '',
|
|
reason: 'Test reason',
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledWith(command);
|
|
});
|
|
|
|
it('should handle empty reason gracefully', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: '',
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.rejectRequest).toHaveBeenCalledWith(command);
|
|
});
|
|
});
|
|
|
|
describe('service instantiation', () => {
|
|
it('should create SponsorshipRequestsService instance', () => {
|
|
// Arrange & Act
|
|
const mutation = new RejectSponsorshipRequestMutation();
|
|
|
|
// Assert
|
|
expect(mutation).toBeInstanceOf(RejectSponsorshipRequestMutation);
|
|
});
|
|
});
|
|
|
|
describe('result shape', () => {
|
|
it('should return void on success', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: 'Test reason',
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
|
|
it('should return void on success with null reason', async () => {
|
|
// Arrange
|
|
const command = {
|
|
requestId: 'request-123',
|
|
actorDriverId: 'driver-456',
|
|
reason: null,
|
|
};
|
|
mockServiceInstance.rejectRequest.mockResolvedValue(Result.ok(undefined));
|
|
|
|
// Act
|
|
const result = await mutation.execute(command);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
});
|
|
});
|