160 lines
5.1 KiB
TypeScript
160 lines
5.1 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
|
import {
|
|
GetEntitySponsorshipPricingUseCase,
|
|
type GetEntitySponsorshipPricingInput,
|
|
type GetEntitySponsorshipPricingResult,
|
|
} from './GetEntitySponsorshipPricingUseCase';
|
|
import type { ISponsorshipPricingRepository } from '../../domain/repositories/ISponsorshipPricingRepository';
|
|
import type { Logger } from '@core/shared/application';
|
|
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
describe('GetEntitySponsorshipPricingUseCase', () => {
|
|
let mockSponsorshipPricingRepo: ISponsorshipPricingRepository;
|
|
let mockLogger: Logger;
|
|
let mockFindByEntity: Mock;
|
|
let mockFindPendingByEntity: Mock;
|
|
let mockFindBySeasonId: Mock;
|
|
let output: UseCaseOutputPort<GetEntitySponsorshipPricingResult> & {
|
|
present: Mock;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
mockFindByEntity = vi.fn();
|
|
mockFindPendingByEntity = vi.fn();
|
|
mockFindBySeasonId = vi.fn();
|
|
mockSponsorshipPricingRepo = {
|
|
findByEntity: mockFindByEntity,
|
|
findAll: vi.fn(),
|
|
create: vi.fn(),
|
|
update: vi.fn(),
|
|
delete: vi.fn(),
|
|
save: vi.fn(),
|
|
exists: vi.fn(),
|
|
findAcceptingApplications: vi.fn(),
|
|
} as ISponsorshipPricingRepository;
|
|
mockLogger = {
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
};
|
|
output = { present: vi.fn() } as unknown as typeof output;
|
|
});
|
|
|
|
it('should return PRICING_NOT_CONFIGURED when no pricing found', async () => {
|
|
const useCase = new GetEntitySponsorshipPricingUseCase(
|
|
mockSponsorshipPricingRepo as unknown as ISponsorshipPricingRepository,
|
|
mockLogger,
|
|
output,
|
|
);
|
|
|
|
const dto: GetEntitySponsorshipPricingInput = {
|
|
entityType: 'season',
|
|
entityId: 'season1',
|
|
};
|
|
|
|
mockFindByEntity.mockResolvedValue(null);
|
|
|
|
const result = await useCase.execute(dto);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const err = result.unwrapErr() as ApplicationErrorCode<
|
|
'PRICING_NOT_CONFIGURED',
|
|
{ message: string }
|
|
>;
|
|
expect(err.code).toBe('PRICING_NOT_CONFIGURED');
|
|
expect(err.details.message).toContain('No sponsorship pricing configured');
|
|
expect(output.present).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should return pricing data when found', async () => {
|
|
const useCase = new GetEntitySponsorshipPricingUseCase(
|
|
mockSponsorshipPricingRepo as unknown as ISponsorshipPricingRepository,
|
|
mockLogger,
|
|
output,
|
|
);
|
|
|
|
const dto: GetEntitySponsorshipPricingInput = {
|
|
entityType: 'season',
|
|
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,
|
|
},
|
|
};
|
|
|
|
mockFindByEntity.mockResolvedValue(pricing);
|
|
mockFindPendingByEntity.mockResolvedValue([]);
|
|
mockFindBySeasonId.mockResolvedValue([]);
|
|
|
|
const result = await useCase.execute(dto);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(output.present).toHaveBeenCalledTimes(1);
|
|
|
|
const presented = (output.present as Mock).mock.calls[0]?.[0] as GetEntitySponsorshipPricingResult;
|
|
|
|
expect(presented.entityType).toBe('season');
|
|
expect(presented.entityId).toBe('season1');
|
|
expect(presented.acceptingApplications).toBe(true);
|
|
expect(presented.customRequirements).toBe('Some requirements');
|
|
expect(presented.tiers).toHaveLength(2);
|
|
|
|
const mainTier = presented.tiers.find(tier => tier.name === 'main');
|
|
const secondaryTier = presented.tiers.find(tier => tier.name === 'secondary');
|
|
|
|
expect(mainTier).toBeDefined();
|
|
expect(mainTier?.price.amount).toBe(100);
|
|
expect(mainTier?.price.currency).toBe('USD');
|
|
expect(mainTier?.benefits).toEqual(['Benefit 1']);
|
|
|
|
expect(secondaryTier).toBeDefined();
|
|
expect(secondaryTier?.price.amount).toBe(50);
|
|
expect(secondaryTier?.price.currency).toBe('USD');
|
|
expect(secondaryTier?.benefits).toEqual(['Benefit 2']);
|
|
});
|
|
|
|
it('should return error when repository throws', async () => {
|
|
const useCase = new GetEntitySponsorshipPricingUseCase(
|
|
mockSponsorshipPricingRepo as unknown as ISponsorshipPricingRepository,
|
|
mockLogger,
|
|
output,
|
|
);
|
|
|
|
const dto: GetEntitySponsorshipPricingInput = {
|
|
entityType: 'season',
|
|
entityId: 'season1',
|
|
};
|
|
|
|
const error = new Error('Repository error');
|
|
|
|
mockFindByEntity.mockRejectedValue(error);
|
|
|
|
const result = await useCase.execute(dto);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const err = result.unwrapErr() as ApplicationErrorCode<
|
|
'REPOSITORY_ERROR',
|
|
{ message: string }
|
|
>;
|
|
expect(err.code).toBe('REPOSITORY_ERROR');
|
|
expect(err.details.message).toBe('Repository error');
|
|
expect(output.present).not.toHaveBeenCalled();
|
|
});
|
|
});
|