refactor racing use cases
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetEntitySponsorshipPricingUseCase } from './GetEntitySponsorshipPricingUseCase';
|
||||
import {
|
||||
GetEntitySponsorshipPricingUseCase,
|
||||
type GetEntitySponsorshipPricingInput,
|
||||
type GetEntitySponsorshipPricingResult,
|
||||
} 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';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetEntitySponsorshipPricingUseCase', () => {
|
||||
let mockSponsorshipPricingRepo: ISponsorshipPricingRepository;
|
||||
@@ -13,6 +19,9 @@ describe('GetEntitySponsorshipPricingUseCase', () => {
|
||||
let mockFindByEntity: Mock;
|
||||
let mockFindPendingByEntity: Mock;
|
||||
let mockFindBySeasonId: Mock;
|
||||
let output: UseCaseOutputPort<GetEntitySponsorshipPricingResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockFindByEntity = vi.fn();
|
||||
@@ -65,24 +74,35 @@ describe('GetEntitySponsorshipPricingUseCase', () => {
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = { present: vi.fn() } as unknown as typeof output;
|
||||
});
|
||||
|
||||
it('should return null when no pricing found', async () => {
|
||||
it('should return PRICING_NOT_CONFIGURED 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,
|
||||
output,
|
||||
);
|
||||
|
||||
const dto = { entityType: 'season' as const, entityId: 'season1' };
|
||||
const dto: GetEntitySponsorshipPricingInput = {
|
||||
entityType: 'season',
|
||||
entityId: 'season1',
|
||||
};
|
||||
|
||||
mockFindByEntity.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(dto);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.value).toBe(null);
|
||||
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 () => {
|
||||
@@ -91,9 +111,13 @@ describe('GetEntitySponsorshipPricingUseCase', () => {
|
||||
mockSponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
|
||||
mockSeasonSponsorshipRepo as unknown as ISeasonSponsorshipRepository,
|
||||
mockLogger,
|
||||
output,
|
||||
);
|
||||
|
||||
const dto = { entityType: 'season' as const, entityId: 'season1' };
|
||||
const dto: GetEntitySponsorshipPricingInput = {
|
||||
entityType: 'season',
|
||||
entityId: 'season1',
|
||||
};
|
||||
const pricing = {
|
||||
acceptingApplications: true,
|
||||
customRequirements: 'Some requirements',
|
||||
@@ -118,34 +142,29 @@ describe('GetEntitySponsorshipPricingUseCase', () => {
|
||||
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,
|
||||
},
|
||||
});
|
||||
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 () => {
|
||||
@@ -154,9 +173,13 @@ describe('GetEntitySponsorshipPricingUseCase', () => {
|
||||
mockSponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
|
||||
mockSeasonSponsorshipRepo as unknown as ISeasonSponsorshipRepository,
|
||||
mockLogger,
|
||||
output,
|
||||
);
|
||||
|
||||
const dto = { entityType: 'season' as const, entityId: 'season1' };
|
||||
const dto: GetEntitySponsorshipPricingInput = {
|
||||
entityType: 'season',
|
||||
entityId: 'season1',
|
||||
};
|
||||
const error = new Error('Repository error');
|
||||
|
||||
mockFindByEntity.mockRejectedValue(error);
|
||||
@@ -164,7 +187,12 @@ describe('GetEntitySponsorshipPricingUseCase', () => {
|
||||
const result = await useCase.execute(dto);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
|
||||
expect(result.unwrapErr().details.message).toBe('Repository error');
|
||||
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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user