159 lines
5.6 KiB
TypeScript
159 lines
5.6 KiB
TypeScript
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
|
import { InMemoryLeagueRepository } from './InMemoryLeagueRepository';
|
|
import { League } from '@core/racing/domain/entities/League';
|
|
import type { Logger } from '@core/shared/application';
|
|
|
|
describe('InMemoryLeagueRepository', () => {
|
|
let repository: InMemoryLeagueRepository;
|
|
let mockLogger: Logger;
|
|
|
|
beforeEach(() => {
|
|
mockLogger = {
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
};
|
|
repository = new InMemoryLeagueRepository(mockLogger);
|
|
});
|
|
|
|
const createTestLeague = (id: string, name: string, ownerId: string) => {
|
|
return League.create({
|
|
id,
|
|
name,
|
|
description: 'Test description',
|
|
ownerId,
|
|
});
|
|
};
|
|
|
|
describe('constructor', () => {
|
|
it('should initialize with a logger', () => {
|
|
expect(repository).toBeDefined();
|
|
expect(mockLogger.info).toHaveBeenCalledWith('InMemoryLeagueRepository initialized');
|
|
});
|
|
});
|
|
|
|
describe('findById', () => {
|
|
it('should return null if league not found', async () => {
|
|
const result = await repository.findById('nonexistent');
|
|
expect(result).toBeNull();
|
|
expect(mockLogger.debug).toHaveBeenCalledWith('Attempting to find league with ID: nonexistent.');
|
|
expect(mockLogger.warn).toHaveBeenCalledWith('League with ID: nonexistent not found.');
|
|
});
|
|
|
|
it('should return the league if found', async () => {
|
|
const league = createTestLeague('1', 'Test League', 'owner1');
|
|
await repository.create(league);
|
|
|
|
const result = await repository.findById('1');
|
|
expect(result).toEqual(league);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Successfully found league with ID: 1.');
|
|
});
|
|
});
|
|
|
|
describe('findByOwnerId', () => {
|
|
it('should return leagues filtered by owner ID', async () => {
|
|
const league1 = createTestLeague('1', 'League 1', 'owner1');
|
|
const league2 = createTestLeague('2', 'League 2', 'owner2');
|
|
const league3 = createTestLeague('3', 'League 3', 'owner1');
|
|
await repository.create(league1);
|
|
await repository.create(league2);
|
|
await repository.create(league3);
|
|
|
|
const result = await repository.findByOwnerId('owner1');
|
|
expect(result).toHaveLength(2);
|
|
expect(result).toContain(league1);
|
|
expect(result).toContain(league3);
|
|
});
|
|
});
|
|
|
|
describe('searchByName', () => {
|
|
it('should return leagues matching the query in name (case insensitive)', async () => {
|
|
const league1 = createTestLeague('1', 'Ferrari League', 'owner1');
|
|
const league2 = createTestLeague('2', 'BMW League', 'owner2');
|
|
await repository.create(league1);
|
|
await repository.create(league2);
|
|
|
|
const result = await repository.searchByName('ferrari');
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0]).toEqual(league1);
|
|
});
|
|
});
|
|
|
|
describe('findAll', () => {
|
|
it('should return all leagues', async () => {
|
|
const league1 = createTestLeague('1', 'League 1', 'owner1');
|
|
const league2 = createTestLeague('2', 'League 2', 'owner2');
|
|
await repository.create(league1);
|
|
await repository.create(league2);
|
|
|
|
const result = await repository.findAll();
|
|
expect(result).toHaveLength(2);
|
|
expect(result).toContain(league1);
|
|
expect(result).toContain(league2);
|
|
});
|
|
});
|
|
|
|
describe('create', () => {
|
|
it('should create a new league', async () => {
|
|
const league = createTestLeague('1', 'Test League', 'owner1');
|
|
const result = await repository.create(league);
|
|
expect(result).toEqual(league);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('League 1 created successfully.');
|
|
});
|
|
|
|
it('should throw error if league already exists', async () => {
|
|
const league = createTestLeague('1', 'Test League', 'owner1');
|
|
await repository.create(league);
|
|
await expect(repository.create(league)).rejects.toThrow('League with ID 1 already exists');
|
|
});
|
|
});
|
|
|
|
describe('update', () => {
|
|
it('should update an existing league', async () => {
|
|
const league = createTestLeague('1', 'Test League', 'owner1');
|
|
await repository.create(league);
|
|
|
|
const updatedLeague = league.update({ name: 'Updated League' });
|
|
const result = await repository.update(updatedLeague);
|
|
expect(result).toEqual(updatedLeague);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('League 1 updated successfully.');
|
|
});
|
|
|
|
it('should throw error if league does not exist', async () => {
|
|
const league = createTestLeague('1', 'Test League', 'owner1');
|
|
await expect(repository.update(league)).rejects.toThrow('League with ID 1 not found');
|
|
});
|
|
});
|
|
|
|
describe('delete', () => {
|
|
it('should delete an existing league', async () => {
|
|
const league = createTestLeague('1', 'Test League', 'owner1');
|
|
await repository.create(league);
|
|
|
|
await repository.delete('1');
|
|
expect(mockLogger.info).toHaveBeenCalledWith('League 1 deleted successfully.');
|
|
const found = await repository.findById('1');
|
|
expect(found).toBeNull();
|
|
});
|
|
|
|
it('should throw error if league does not exist', async () => {
|
|
await expect(repository.delete('nonexistent')).rejects.toThrow('League with ID nonexistent not found');
|
|
});
|
|
});
|
|
|
|
describe('exists', () => {
|
|
it('should return true if league exists', async () => {
|
|
const league = createTestLeague('1', 'Test League', 'owner1');
|
|
await repository.create(league);
|
|
|
|
const result = await repository.exists('1');
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it('should return false if league does not exist', async () => {
|
|
const result = await repository.exists('nonexistent');
|
|
expect(result).toBe(false);
|
|
});
|
|
});
|
|
}); |