Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m50s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
205 lines
7.6 KiB
TypeScript
205 lines
7.6 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { CreateSponsorUseCase } from '../../../../core/racing/application/use-cases/CreateSponsorUseCase';
|
|
import { Sponsor } from '../../../../core/racing/domain/entities/sponsor/Sponsor';
|
|
import { SponsorTestContext } from '../SponsorTestContext';
|
|
|
|
describe('Sponsor Signup Use Case Orchestration', () => {
|
|
let context: SponsorTestContext;
|
|
let createSponsorUseCase: CreateSponsorUseCase;
|
|
|
|
beforeEach(() => {
|
|
context = new SponsorTestContext();
|
|
createSponsorUseCase = new CreateSponsorUseCase(context.sponsorRepository, context.logger);
|
|
});
|
|
|
|
describe('CreateSponsorUseCase - Success Path', () => {
|
|
it('should create a new sponsor account with valid information', async () => {
|
|
const sponsorData = {
|
|
name: 'Test Company',
|
|
contactEmail: 'test@example.com',
|
|
websiteUrl: 'https://testcompany.com',
|
|
logoUrl: 'https://testcompany.com/logo.png',
|
|
};
|
|
|
|
const result = await createSponsorUseCase.execute(sponsorData);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
const createdSponsor = result.unwrap().sponsor;
|
|
|
|
expect(createdSponsor.id.toString()).toBeDefined();
|
|
expect(createdSponsor.name.toString()).toBe('Test Company');
|
|
expect(createdSponsor.contactEmail.toString()).toBe('test@example.com');
|
|
expect(createdSponsor.websiteUrl?.toString()).toBe('https://testcompany.com');
|
|
expect(createdSponsor.logoUrl?.toString()).toBe('https://testcompany.com/logo.png');
|
|
expect(createdSponsor.createdAt).toBeDefined();
|
|
|
|
const retrievedSponsor = await context.sponsorRepository.findById(createdSponsor.id.toString());
|
|
expect(retrievedSponsor).toBeDefined();
|
|
expect(retrievedSponsor?.name.toString()).toBe('Test Company');
|
|
});
|
|
|
|
it('should create a sponsor with minimal data', async () => {
|
|
const sponsorData = {
|
|
name: 'Minimal Company',
|
|
contactEmail: 'minimal@example.com',
|
|
};
|
|
|
|
const result = await createSponsorUseCase.execute(sponsorData);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
const createdSponsor = result.unwrap().sponsor;
|
|
|
|
expect(createdSponsor.name.toString()).toBe('Minimal Company');
|
|
expect(createdSponsor.contactEmail.toString()).toBe('minimal@example.com');
|
|
expect(createdSponsor.websiteUrl).toBeUndefined();
|
|
expect(createdSponsor.logoUrl).toBeUndefined();
|
|
});
|
|
|
|
it('should create a sponsor with optional fields only', async () => {
|
|
const sponsorData = {
|
|
name: 'Optional Fields Company',
|
|
contactEmail: 'optional@example.com',
|
|
websiteUrl: 'https://optional.com',
|
|
};
|
|
|
|
const result = await createSponsorUseCase.execute(sponsorData);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
const createdSponsor = result.unwrap().sponsor;
|
|
|
|
expect(createdSponsor.websiteUrl?.toString()).toBe('https://optional.com');
|
|
expect(createdSponsor.logoUrl).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('CreateSponsorUseCase - Validation', () => {
|
|
it('should reject sponsor creation with duplicate email', async () => {
|
|
const existingSponsor = Sponsor.create({
|
|
id: 'existing-sponsor',
|
|
name: 'Existing Company',
|
|
contactEmail: 'sponsor@example.com',
|
|
});
|
|
await context.sponsorRepository.create(existingSponsor);
|
|
|
|
const result = await createSponsorUseCase.execute({
|
|
name: 'New Company',
|
|
contactEmail: 'sponsor@example.com',
|
|
});
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.unwrapErr();
|
|
expect(error.code).toBe('REPOSITORY_ERROR');
|
|
});
|
|
|
|
it('should reject sponsor creation with invalid email format', async () => {
|
|
const result = await createSponsorUseCase.execute({
|
|
name: 'Test Company',
|
|
contactEmail: 'invalid-email',
|
|
});
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.unwrapErr();
|
|
expect(error.code).toBe('VALIDATION_ERROR');
|
|
expect(error.details.message).toContain('Invalid sponsor contact email format');
|
|
});
|
|
|
|
it('should reject sponsor creation with missing required fields', async () => {
|
|
const result = await createSponsorUseCase.execute({
|
|
name: '',
|
|
contactEmail: 'test@example.com',
|
|
});
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.unwrapErr();
|
|
expect(error.code).toBe('VALIDATION_ERROR');
|
|
expect(error.details.message).toContain('Sponsor name is required');
|
|
});
|
|
|
|
it('should reject sponsor creation with invalid website URL', async () => {
|
|
const result = await createSponsorUseCase.execute({
|
|
name: 'Test Company',
|
|
contactEmail: 'test@example.com',
|
|
websiteUrl: 'not-a-valid-url',
|
|
});
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.unwrapErr();
|
|
expect(error.code).toBe('VALIDATION_ERROR');
|
|
expect(error.details.message).toContain('Invalid sponsor website URL');
|
|
});
|
|
|
|
it('should reject sponsor creation with missing email', async () => {
|
|
const result = await createSponsorUseCase.execute({
|
|
name: 'Test Company',
|
|
contactEmail: '',
|
|
});
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const error = result.unwrapErr();
|
|
expect(error.code).toBe('VALIDATION_ERROR');
|
|
expect(error.details.message).toContain('Sponsor contact email is required');
|
|
});
|
|
});
|
|
|
|
describe('Sponsor Data Orchestration', () => {
|
|
it('should correctly create sponsor with all optional fields', async () => {
|
|
const sponsorData = {
|
|
name: 'Full Featured Company',
|
|
contactEmail: 'full@example.com',
|
|
websiteUrl: 'https://fullfeatured.com',
|
|
logoUrl: 'https://fullfeatured.com/logo.png',
|
|
};
|
|
|
|
const result = await createSponsorUseCase.execute(sponsorData);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
const createdSponsor = result.unwrap().sponsor;
|
|
|
|
expect(createdSponsor.name.toString()).toBe('Full Featured Company');
|
|
expect(createdSponsor.contactEmail.toString()).toBe('full@example.com');
|
|
expect(createdSponsor.websiteUrl?.toString()).toBe('https://fullfeatured.com');
|
|
expect(createdSponsor.logoUrl?.toString()).toBe('https://fullfeatured.com/logo.png');
|
|
expect(createdSponsor.createdAt).toBeDefined();
|
|
});
|
|
|
|
it('should generate unique IDs for each sponsor', async () => {
|
|
const sponsorData1 = {
|
|
name: 'Company 1',
|
|
contactEmail: 'company1@example.com',
|
|
};
|
|
const sponsorData2 = {
|
|
name: 'Company 2',
|
|
contactEmail: 'company2@example.com',
|
|
};
|
|
|
|
const result1 = await createSponsorUseCase.execute(sponsorData1);
|
|
const result2 = await createSponsorUseCase.execute(sponsorData2);
|
|
|
|
expect(result1.isOk()).toBe(true);
|
|
expect(result2.isOk()).toBe(true);
|
|
|
|
const sponsor1 = result1.unwrap().sponsor;
|
|
const sponsor2 = result2.unwrap().sponsor;
|
|
|
|
expect(sponsor1.id.toString()).not.toBe(sponsor2.id.toString());
|
|
});
|
|
|
|
it('should persist sponsor in repository after creation', async () => {
|
|
const sponsorData = {
|
|
name: 'Persistent Company',
|
|
contactEmail: 'persistent@example.com',
|
|
};
|
|
|
|
const result = await createSponsorUseCase.execute(sponsorData);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
const createdSponsor = result.unwrap().sponsor;
|
|
|
|
const retrievedSponsor = await context.sponsorRepository.findById(createdSponsor.id.toString());
|
|
expect(retrievedSponsor).toBeDefined();
|
|
expect(retrievedSponsor?.name.toString()).toBe('Persistent Company');
|
|
expect(retrievedSponsor?.contactEmail.toString()).toBe('persistent@example.com');
|
|
});
|
|
});
|
|
});
|