refactor
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
||||
import { InMemorySeasonSponsorshipRepository } from './InMemorySeasonSponsorshipRepository';
|
||||
import { SeasonSponsorship, type SponsorshipTier } from '@core/racing/domain/entities/season/SeasonSponsorship';
|
||||
import { Money } from '@core/racing/domain/value-objects/Money';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
describe('InMemorySeasonSponsorshipRepository', () => {
|
||||
let repository: InMemorySeasonSponsorshipRepository;
|
||||
let mockLogger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
mockLogger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
repository = new InMemorySeasonSponsorshipRepository(mockLogger);
|
||||
});
|
||||
|
||||
const createTestSeasonSponsorship = (
|
||||
id: string,
|
||||
seasonId: string,
|
||||
sponsorId: string,
|
||||
tier: SponsorshipTier = 'main',
|
||||
leagueId?: string
|
||||
) => {
|
||||
return SeasonSponsorship.create({
|
||||
id,
|
||||
seasonId,
|
||||
sponsorId,
|
||||
tier,
|
||||
pricing: Money.create(1000),
|
||||
...(leagueId ? { leagueId } : {}),
|
||||
});
|
||||
};
|
||||
|
||||
describe('constructor', () => {
|
||||
it('should initialize with a logger', () => {
|
||||
expect(repository).toBeDefined();
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('InMemorySeasonSponsorshipRepository initialized.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findById', () => {
|
||||
it('should return null if sponsorship not found', async () => {
|
||||
const result = await repository.findById('nonexistent');
|
||||
expect(result).toBeNull();
|
||||
expect(mockLogger.debug).toHaveBeenCalledWith('Finding season sponsorship by id: nonexistent');
|
||||
expect(mockLogger.warn).toHaveBeenCalledWith('Season sponsorship with id nonexistent not found.');
|
||||
});
|
||||
|
||||
it('should return the sponsorship if found', async () => {
|
||||
const sponsorship = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
await repository.create(sponsorship);
|
||||
|
||||
const result = await repository.findById('1');
|
||||
expect(result).toEqual(sponsorship);
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('Found season sponsorship: 1.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findBySeasonId', () => {
|
||||
it('should return sponsorships filtered by season ID', async () => {
|
||||
const sponsorship1 = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
const sponsorship2 = createTestSeasonSponsorship('2', 'season2', 'sponsor2');
|
||||
const sponsorship3 = createTestSeasonSponsorship('3', 'season1', 'sponsor3');
|
||||
await repository.create(sponsorship1);
|
||||
await repository.create(sponsorship2);
|
||||
await repository.create(sponsorship3);
|
||||
|
||||
const result = await repository.findBySeasonId('season1');
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result).toContain(sponsorship1);
|
||||
expect(result).toContain(sponsorship3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByLeagueId', () => {
|
||||
it('should return sponsorships filtered by league ID', async () => {
|
||||
const sponsorship1 = createTestSeasonSponsorship('1', 'season1', 'sponsor1', 'main', 'league1');
|
||||
const sponsorship2 = createTestSeasonSponsorship('2', 'season2', 'sponsor2', 'main', 'league2');
|
||||
const sponsorship3 = createTestSeasonSponsorship('3', 'season1', 'sponsor3', 'main', 'league1');
|
||||
await repository.create(sponsorship1);
|
||||
await repository.create(sponsorship2);
|
||||
await repository.create(sponsorship3);
|
||||
|
||||
const result = await repository.findByLeagueId('league1');
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result).toContain(sponsorship1);
|
||||
expect(result).toContain(sponsorship3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findBySponsorId', () => {
|
||||
it('should return sponsorships filtered by sponsor ID', async () => {
|
||||
const sponsorship1 = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
const sponsorship2 = createTestSeasonSponsorship('2', 'season2', 'sponsor2');
|
||||
const sponsorship3 = createTestSeasonSponsorship('3', 'season1', 'sponsor1');
|
||||
await repository.create(sponsorship1);
|
||||
await repository.create(sponsorship2);
|
||||
await repository.create(sponsorship3);
|
||||
|
||||
const result = await repository.findBySponsorId('sponsor1');
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result).toContain(sponsorship1);
|
||||
expect(result).toContain(sponsorship3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findBySeasonAndTier', () => {
|
||||
it('should return sponsorships filtered by season ID and tier', async () => {
|
||||
const sponsorship1 = createTestSeasonSponsorship('1', 'season1', 'sponsor1', 'main');
|
||||
const sponsorship2 = createTestSeasonSponsorship('2', 'season1', 'sponsor2', 'secondary');
|
||||
const sponsorship3 = createTestSeasonSponsorship('3', 'season2', 'sponsor3', 'main');
|
||||
await repository.create(sponsorship1);
|
||||
await repository.create(sponsorship2);
|
||||
await repository.create(sponsorship3);
|
||||
|
||||
const result = await repository.findBySeasonAndTier('season1', 'main');
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result).toContain(sponsorship1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create a new sponsorship', async () => {
|
||||
const sponsorship = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
const result = await repository.create(sponsorship);
|
||||
expect(result).toEqual(sponsorship);
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('SeasonSponsorship 1 created successfully.');
|
||||
});
|
||||
|
||||
it('should throw error if sponsorship already exists', async () => {
|
||||
const sponsorship = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
await repository.create(sponsorship);
|
||||
await expect(repository.create(sponsorship)).rejects.toThrow('SeasonSponsorship with this ID already exists');
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update an existing sponsorship', async () => {
|
||||
const sponsorship = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
await repository.create(sponsorship);
|
||||
|
||||
const updatedSponsorship = sponsorship.activate();
|
||||
await repository.update(updatedSponsorship);
|
||||
const found = await repository.findById('1');
|
||||
expect(found?.status).toBe('active');
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('SeasonSponsorship 1 updated successfully.');
|
||||
});
|
||||
|
||||
it('should throw error if sponsorship does not exist', async () => {
|
||||
const sponsorship = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
await expect(repository.update(sponsorship)).rejects.toThrow('SeasonSponsorship not found');
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete an existing sponsorship', async () => {
|
||||
const sponsorship = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
await repository.create(sponsorship);
|
||||
|
||||
await repository.delete('1');
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('SeasonSponsorship 1 deleted successfully.');
|
||||
const found = await repository.findById('1');
|
||||
expect(found).toBeNull();
|
||||
});
|
||||
|
||||
it('should not throw if sponsorship does not exist', async () => {
|
||||
await repository.delete('nonexistent');
|
||||
expect(mockLogger.warn).toHaveBeenCalledWith('SeasonSponsorship with id nonexistent not found for deletion.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('exists', () => {
|
||||
it('should return true if sponsorship exists', async () => {
|
||||
const sponsorship = createTestSeasonSponsorship('1', 'season1', 'sponsor1');
|
||||
await repository.create(sponsorship);
|
||||
|
||||
const result = await repository.exists('1');
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false if sponsorship does not exist', async () => {
|
||||
const result = await repository.exists('nonexistent');
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user