Files
gridpilot.gg/core/racing/application/use-cases/RejectSponsorshipRequestUseCase.test.ts
2025-12-20 11:02:15 +01:00

149 lines
4.9 KiB
TypeScript

import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { RejectSponsorshipRequestUseCase } from './RejectSponsorshipRequestUseCase';
import type { ISponsorshipRequestRepository } from '../../domain/repositories/ISponsorshipRequestRepository';
describe('RejectSponsorshipRequestUseCase', () => {
let useCase: RejectSponsorshipRequestUseCase;
let sponsorshipRequestRepo: { findById: Mock; update: Mock };
let notificationPort: { notifySponsorshipRequestRejected: Mock };
beforeEach(() => {
sponsorshipRequestRepo = { findById: vi.fn(), update: vi.fn() };
notificationPort = { notifySponsorshipRequestRejected: vi.fn() };
useCase = new RejectSponsorshipRequestUseCase(
sponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
notificationPort as any,
);
});
it('should return not found error when request does not exist', async () => {
sponsorshipRequestRepo.findById.mockResolvedValue(null);
const result = await useCase.execute({
requestId: 'request-1',
respondedBy: 'driver-1',
reason: 'Not interested',
});
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'SPONSORSHIP_REQUEST_NOT_FOUND',
});
});
it('should return not pending error when request is not pending', async () => {
const mockRequest = {
id: 'request-1',
status: 'accepted',
isPending: vi.fn().mockReturnValue(false),
};
sponsorshipRequestRepo.findById.mockResolvedValue(mockRequest);
const result = await useCase.execute({
requestId: 'request-1',
respondedBy: 'driver-1',
reason: 'Not interested',
});
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'SPONSORSHIP_REQUEST_NOT_PENDING',
});
});
it('should reject the request successfully and notify sponsor with reason', async () => {
const respondedAt = new Date('2023-01-01T00:00:00Z');
const mockRequest = {
id: 'request-1',
sponsorId: 'sponsor-1',
entityType: 'season',
entityId: 'season-1',
tier: 'main',
offeredAmount: { amount: 1000, currency: 'USD' },
status: 'pending',
isPending: vi.fn().mockReturnValue(true),
reject: vi.fn().mockReturnValue({
id: 'request-1',
respondedAt,
rejectionReason: 'Not interested',
}),
};
sponsorshipRequestRepo.findById.mockResolvedValue(mockRequest);
sponsorshipRequestRepo.update.mockResolvedValue(undefined);
const result = await useCase.execute({
requestId: 'request-1',
respondedBy: 'driver-1',
reason: 'Not interested',
});
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual({
requestId: 'request-1',
status: 'rejected',
rejectedAt: respondedAt,
reason: 'Not interested',
});
expect(sponsorshipRequestRepo.update).toHaveBeenCalledWith(mockRequest.reject());
expect(notificationPort.notifySponsorshipRequestRejected).toHaveBeenCalledTimes(1);
expect(notificationPort.notifySponsorshipRequestRejected).toHaveBeenCalledWith({
requestId: 'request-1',
sponsorId: 'sponsor-1',
entityType: 'season',
entityId: 'season-1',
tier: 'main',
offeredAmountCents: 1000,
currency: 'USD',
rejectedAt: respondedAt,
rejectedBy: 'driver-1',
rejectionReason: 'Not interested',
});
});
it('should reject the request successfully and notify sponsor without reason', async () => {
const respondedAt = new Date('2023-01-01T00:00:00Z');
const mockRequest = {
id: 'request-1',
sponsorId: 'sponsor-1',
entityType: 'season',
entityId: 'season-1',
tier: 'main',
offeredAmount: { amount: 1000, currency: 'USD' },
status: 'pending',
isPending: vi.fn().mockReturnValue(true),
reject: vi.fn().mockReturnValue({
id: 'request-1',
respondedAt,
rejectionReason: undefined,
}),
};
sponsorshipRequestRepo.findById.mockResolvedValue(mockRequest);
sponsorshipRequestRepo.update.mockResolvedValue(undefined);
const result = await useCase.execute({
requestId: 'request-1',
respondedBy: 'driver-1',
});
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual({
requestId: 'request-1',
status: 'rejected',
rejectedAt: respondedAt,
});
expect(sponsorshipRequestRepo.update).toHaveBeenCalledWith(mockRequest.reject());
expect(notificationPort.notifySponsorshipRequestRejected).toHaveBeenCalledTimes(1);
expect(notificationPort.notifySponsorshipRequestRejected).toHaveBeenCalledWith({
requestId: 'request-1',
sponsorId: 'sponsor-1',
entityType: 'season',
entityId: 'season-1',
tier: 'main',
offeredAmountCents: 1000,
currency: 'USD',
rejectedAt: respondedAt,
rejectedBy: 'driver-1',
rejectionReason: undefined,
});
});
});