view data fixes
This commit is contained in:
386
apps/website/lib/mutations/leagues/ProtestReviewMutation.test.ts
Normal file
386
apps/website/lib/mutations/leagues/ProtestReviewMutation.test.ts
Normal file
@@ -0,0 +1,386 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { ProtestReviewMutation } from './ProtestReviewMutation';
|
||||
import { ProtestService } from '@/lib/services/protests/ProtestService';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@/lib/services/protests/ProtestService', () => {
|
||||
return {
|
||||
ProtestService: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
describe('ProtestReviewMutation', () => {
|
||||
let mutation: ProtestReviewMutation;
|
||||
let mockServiceInstance: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockServiceInstance = {
|
||||
applyPenalty: vi.fn(),
|
||||
requestDefense: vi.fn(),
|
||||
reviewProtest: vi.fn(),
|
||||
};
|
||||
// Use mockImplementation to return the instance
|
||||
(ProtestService as any).mockImplementation(function() {
|
||||
return mockServiceInstance;
|
||||
});
|
||||
mutation = new ProtestReviewMutation();
|
||||
});
|
||||
|
||||
describe('applyPenalty', () => {
|
||||
describe('happy paths', () => {
|
||||
it('should successfully apply penalty with valid input', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
penaltyType: 'time_penalty',
|
||||
penaltyValue: 30,
|
||||
stewardNotes: 'Test notes',
|
||||
raceId: 'race-456',
|
||||
accusedDriverId: 'driver-789',
|
||||
reason: 'Test reason',
|
||||
};
|
||||
mockServiceInstance.applyPenalty.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.applyPenalty(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(mockServiceInstance.applyPenalty).toHaveBeenCalledWith(input);
|
||||
expect(mockServiceInstance.applyPenalty).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('failure modes', () => {
|
||||
it('should handle service failure during penalty application', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
penaltyType: 'time_penalty',
|
||||
penaltyValue: 30,
|
||||
stewardNotes: 'Test notes',
|
||||
raceId: 'race-456',
|
||||
accusedDriverId: 'driver-789',
|
||||
reason: 'Test reason',
|
||||
};
|
||||
const serviceError = new Error('Service error');
|
||||
mockServiceInstance.applyPenalty.mockRejectedValue(serviceError);
|
||||
|
||||
// Act
|
||||
const result = await mutation.applyPenalty(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toEqual({
|
||||
type: 'serverError',
|
||||
message: 'Service error',
|
||||
});
|
||||
expect(mockServiceInstance.applyPenalty).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle service returning error result', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
penaltyType: 'time_penalty',
|
||||
penaltyValue: 30,
|
||||
stewardNotes: 'Test notes',
|
||||
raceId: 'race-456',
|
||||
accusedDriverId: 'driver-789',
|
||||
reason: 'Test reason',
|
||||
};
|
||||
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
||||
mockServiceInstance.applyPenalty.mockResolvedValue(Result.err(domainError));
|
||||
|
||||
// Act
|
||||
const result = await mutation.applyPenalty(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe(domainError);
|
||||
expect(mockServiceInstance.applyPenalty).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('input validation', () => {
|
||||
it('should accept valid input', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
penaltyType: 'time_penalty',
|
||||
penaltyValue: 30,
|
||||
stewardNotes: 'Test notes',
|
||||
raceId: 'race-456',
|
||||
accusedDriverId: 'driver-789',
|
||||
reason: 'Test reason',
|
||||
};
|
||||
mockServiceInstance.applyPenalty.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.applyPenalty(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockServiceInstance.applyPenalty).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('requestDefense', () => {
|
||||
describe('happy paths', () => {
|
||||
it('should successfully request defense with valid input', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
};
|
||||
mockServiceInstance.requestDefense.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.requestDefense(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(mockServiceInstance.requestDefense).toHaveBeenCalledWith(input);
|
||||
expect(mockServiceInstance.requestDefense).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('failure modes', () => {
|
||||
it('should handle service failure during defense request', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
};
|
||||
const serviceError = new Error('Service error');
|
||||
mockServiceInstance.requestDefense.mockRejectedValue(serviceError);
|
||||
|
||||
// Act
|
||||
const result = await mutation.requestDefense(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toEqual({
|
||||
type: 'serverError',
|
||||
message: 'Service error',
|
||||
});
|
||||
expect(mockServiceInstance.requestDefense).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle service returning error result', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
};
|
||||
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
||||
mockServiceInstance.requestDefense.mockResolvedValue(Result.err(domainError));
|
||||
|
||||
// Act
|
||||
const result = await mutation.requestDefense(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe(domainError);
|
||||
expect(mockServiceInstance.requestDefense).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('input validation', () => {
|
||||
it('should accept valid input', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
};
|
||||
mockServiceInstance.requestDefense.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.requestDefense(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockServiceInstance.requestDefense).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('reviewProtest', () => {
|
||||
describe('happy paths', () => {
|
||||
it('should successfully review protest with valid input', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
decision: 'approved',
|
||||
decisionNotes: 'Test notes',
|
||||
};
|
||||
mockServiceInstance.reviewProtest.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.reviewProtest(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(mockServiceInstance.reviewProtest).toHaveBeenCalledWith(input);
|
||||
expect(mockServiceInstance.reviewProtest).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('failure modes', () => {
|
||||
it('should handle service failure during protest review', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
decision: 'approved',
|
||||
decisionNotes: 'Test notes',
|
||||
};
|
||||
const serviceError = new Error('Service error');
|
||||
mockServiceInstance.reviewProtest.mockRejectedValue(serviceError);
|
||||
|
||||
// Act
|
||||
const result = await mutation.reviewProtest(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toEqual({
|
||||
type: 'serverError',
|
||||
message: 'Service error',
|
||||
});
|
||||
expect(mockServiceInstance.reviewProtest).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle service returning error result', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
decision: 'approved',
|
||||
decisionNotes: 'Test notes',
|
||||
};
|
||||
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
||||
mockServiceInstance.reviewProtest.mockResolvedValue(Result.err(domainError));
|
||||
|
||||
// Act
|
||||
const result = await mutation.reviewProtest(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe(domainError);
|
||||
expect(mockServiceInstance.reviewProtest).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('input validation', () => {
|
||||
it('should accept valid input', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
decision: 'approved',
|
||||
decisionNotes: 'Test notes',
|
||||
};
|
||||
mockServiceInstance.reviewProtest.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.reviewProtest(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockServiceInstance.reviewProtest).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle empty decision notes gracefully', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
decision: 'approved',
|
||||
decisionNotes: '',
|
||||
};
|
||||
mockServiceInstance.reviewProtest.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.reviewProtest(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockServiceInstance.reviewProtest).toHaveBeenCalledWith(input);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('service instantiation', () => {
|
||||
it('should create ProtestService instance', () => {
|
||||
// Arrange & Act
|
||||
const mutation = new ProtestReviewMutation();
|
||||
|
||||
// Assert
|
||||
expect(mutation).toBeInstanceOf(ProtestReviewMutation);
|
||||
});
|
||||
});
|
||||
|
||||
describe('result shape', () => {
|
||||
it('should return void on successful penalty application', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
penaltyType: 'time_penalty',
|
||||
penaltyValue: 30,
|
||||
stewardNotes: 'Test notes',
|
||||
raceId: 'race-456',
|
||||
accusedDriverId: 'driver-789',
|
||||
reason: 'Test reason',
|
||||
};
|
||||
mockServiceInstance.applyPenalty.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.applyPenalty(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return void on successful defense request', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
};
|
||||
mockServiceInstance.requestDefense.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.requestDefense(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return void on successful protest review', async () => {
|
||||
// Arrange
|
||||
const input = {
|
||||
protestId: 'protest-123',
|
||||
stewardId: 'steward-456',
|
||||
decision: 'approved',
|
||||
decisionNotes: 'Test notes',
|
||||
};
|
||||
mockServiceInstance.reviewProtest.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
// Act
|
||||
const result = await mutation.reviewProtest(input);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user