refactor
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetEntitySponsorshipPricingUseCase } from './GetEntitySponsorshipPricingUseCase';
|
||||
import type { ISponsorshipPricingRepository } from '../../domain/repositories/ISponsorshipPricingRepository';
|
||||
import type { ISponsorshipRequestRepository } from '../../domain/repositories/ISponsorshipRequestRepository';
|
||||
import type { ISeasonSponsorshipRepository } from '../../domain/repositories/ISeasonSponsorshipRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
describe('GetEntitySponsorshipPricingUseCase', () => {
|
||||
let mockSponsorshipPricingRepo: { findByEntity: Mock };
|
||||
let mockSponsorshipRequestRepo: { findPendingByEntity: Mock };
|
||||
let mockSeasonSponsorshipRepo: { findBySeasonId: Mock };
|
||||
let mockLogger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
mockSponsorshipPricingRepo = { findByEntity: vi.fn() };
|
||||
mockSponsorshipRequestRepo = { findPendingByEntity: vi.fn() };
|
||||
mockSeasonSponsorshipRepo = { findBySeasonId: vi.fn() };
|
||||
mockLogger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
it('should return null when no pricing found', async () => {
|
||||
const useCase = new GetEntitySponsorshipPricingUseCase(
|
||||
mockSponsorshipPricingRepo as unknown as ISponsorshipPricingRepository,
|
||||
mockSponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
|
||||
mockSeasonSponsorshipRepo as unknown as ISeasonSponsorshipRepository,
|
||||
mockLogger,
|
||||
);
|
||||
|
||||
const dto = { entityType: 'season' as const, entityId: 'season1' };
|
||||
|
||||
mockSponsorshipPricingRepo.findByEntity.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(dto);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toBe(null);
|
||||
});
|
||||
|
||||
it('should return pricing data when found', async () => {
|
||||
const useCase = new GetEntitySponsorshipPricingUseCase(
|
||||
mockSponsorshipPricingRepo as unknown as ISponsorshipPricingRepository,
|
||||
mockSponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
|
||||
mockSeasonSponsorshipRepo as unknown as ISeasonSponsorshipRepository,
|
||||
mockLogger,
|
||||
);
|
||||
|
||||
const dto = { entityType: 'season' as const, entityId: 'season1' };
|
||||
const pricing = {
|
||||
acceptingApplications: true,
|
||||
customRequirements: 'Some requirements',
|
||||
mainSlot: {
|
||||
price: { amount: 100, currency: 'USD', format: () => '$100' },
|
||||
benefits: ['Benefit 1'],
|
||||
available: true,
|
||||
maxSlots: 5,
|
||||
},
|
||||
secondarySlots: {
|
||||
price: { amount: 50, currency: 'USD', format: () => '$50' },
|
||||
benefits: ['Benefit 2'],
|
||||
available: true,
|
||||
maxSlots: 10,
|
||||
},
|
||||
};
|
||||
|
||||
mockSponsorshipPricingRepo.findByEntity.mockResolvedValue(pricing);
|
||||
mockSponsorshipRequestRepo.findPendingByEntity.mockResolvedValue([]);
|
||||
mockSeasonSponsorshipRepo.findBySeasonId.mockResolvedValue([]);
|
||||
|
||||
const result = await useCase.execute(dto);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toEqual({
|
||||
entityType: 'season',
|
||||
entityId: 'season1',
|
||||
acceptingApplications: true,
|
||||
customRequirements: 'Some requirements',
|
||||
mainSlot: {
|
||||
tier: 'main',
|
||||
price: 100,
|
||||
currency: 'USD',
|
||||
formattedPrice: '$100',
|
||||
benefits: ['Benefit 1'],
|
||||
available: true,
|
||||
maxSlots: 5,
|
||||
filledSlots: 0,
|
||||
pendingRequests: 0,
|
||||
},
|
||||
secondarySlot: {
|
||||
tier: 'secondary',
|
||||
price: 50,
|
||||
currency: 'USD',
|
||||
formattedPrice: '$50',
|
||||
benefits: ['Benefit 2'],
|
||||
available: true,
|
||||
maxSlots: 10,
|
||||
filledSlots: 0,
|
||||
pendingRequests: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should return error when repository throws', async () => {
|
||||
const useCase = new GetEntitySponsorshipPricingUseCase(
|
||||
mockSponsorshipPricingRepo as unknown as ISponsorshipPricingRepository,
|
||||
mockSponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
|
||||
mockSeasonSponsorshipRepo as unknown as ISeasonSponsorshipRepository,
|
||||
mockLogger,
|
||||
);
|
||||
|
||||
const dto = { entityType: 'season' as const, entityId: 'season1' };
|
||||
const error = new Error('Repository error');
|
||||
|
||||
mockSponsorshipPricingRepo.findByEntity.mockRejectedValue(error);
|
||||
|
||||
const result = await useCase.execute(dto);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toBe('Repository error');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user