refactor
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
||||
import { InMemoryLeagueWalletRepository } from './InMemoryLeagueWalletRepository';
|
||||
import { LeagueWallet } from '@core/racing/domain/entities/league-wallet/LeagueWallet';
|
||||
import { Money } from '@core/racing/domain/value-objects/Money';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
describe('InMemoryLeagueWalletRepository', () => {
|
||||
let repository: InMemoryLeagueWalletRepository;
|
||||
let mockLogger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
mockLogger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
repository = new InMemoryLeagueWalletRepository(mockLogger);
|
||||
});
|
||||
|
||||
const createTestWallet = (id: string, leagueId: string, balanceAmount: number = 10000) => {
|
||||
const balance = Money.create(balanceAmount, 'USD');
|
||||
return LeagueWallet.create({
|
||||
id,
|
||||
leagueId,
|
||||
balance,
|
||||
transactionIds: [],
|
||||
});
|
||||
};
|
||||
|
||||
describe('constructor', () => {
|
||||
it('should initialize with a logger', () => {
|
||||
expect(repository).toBeDefined();
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('InMemoryLeagueWalletRepository initialized.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findById', () => {
|
||||
it('should return null if wallet not found', async () => {
|
||||
const result = await repository.findById('nonexistent');
|
||||
expect(result).toBeNull();
|
||||
expect(mockLogger.debug).toHaveBeenCalledWith('Finding league wallet by id: nonexistent');
|
||||
expect(mockLogger.warn).toHaveBeenCalledWith('League wallet with id nonexistent not found.');
|
||||
});
|
||||
|
||||
it('should return the wallet if found', async () => {
|
||||
const wallet = createTestWallet('1', 'league1');
|
||||
await repository.create(wallet);
|
||||
|
||||
const result = await repository.findById('1');
|
||||
expect(result).toEqual(wallet);
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('Found league wallet: 1.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByLeagueId', () => {
|
||||
it('should return null if no wallet found for league id', async () => {
|
||||
const result = await repository.findByLeagueId('nonexistent');
|
||||
expect(result).toBeNull();
|
||||
expect(mockLogger.warn).toHaveBeenCalledWith('No league wallet found for league id: nonexistent.');
|
||||
});
|
||||
|
||||
it('should return the wallet if found by league id', async () => {
|
||||
const wallet = createTestWallet('1', 'league1');
|
||||
await repository.create(wallet);
|
||||
|
||||
const result = await repository.findByLeagueId('league1');
|
||||
expect(result).toEqual(wallet);
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('Found league wallet for league id: league1.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should create a new wallet', async () => {
|
||||
const wallet = createTestWallet('1', 'league1');
|
||||
const result = await repository.create(wallet);
|
||||
expect(result).toEqual(wallet);
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('LeagueWallet 1 created successfully.');
|
||||
});
|
||||
|
||||
it('should throw error if wallet already exists', async () => {
|
||||
const wallet = createTestWallet('1', 'league1');
|
||||
await repository.create(wallet);
|
||||
await expect(repository.create(wallet)).rejects.toThrow('LeagueWallet with this ID already exists');
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update an existing wallet', async () => {
|
||||
const wallet = createTestWallet('1', 'league1');
|
||||
await repository.create(wallet);
|
||||
|
||||
const updatedWallet = LeagueWallet.create({
|
||||
id: '1',
|
||||
leagueId: 'league1',
|
||||
balance: Money.create(20000, 'USD'),
|
||||
transactionIds: [],
|
||||
});
|
||||
const result = await repository.update(updatedWallet);
|
||||
expect(result).toEqual(updatedWallet);
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('LeagueWallet 1 updated successfully.');
|
||||
});
|
||||
|
||||
it('should throw error if wallet does not exist', async () => {
|
||||
const wallet = createTestWallet('1', 'league1');
|
||||
await expect(repository.update(wallet)).rejects.toThrow('LeagueWallet not found');
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete an existing wallet', async () => {
|
||||
const wallet = createTestWallet('1', 'league1');
|
||||
await repository.create(wallet);
|
||||
|
||||
await repository.delete('1');
|
||||
expect(mockLogger.info).toHaveBeenCalledWith('LeagueWallet 1 deleted successfully.');
|
||||
const found = await repository.findById('1');
|
||||
expect(found).toBeNull();
|
||||
});
|
||||
|
||||
it('should not throw if wallet does not exist', async () => {
|
||||
await repository.delete('nonexistent');
|
||||
expect(mockLogger.warn).toHaveBeenCalledWith('LeagueWallet with id nonexistent not found for deletion.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('exists', () => {
|
||||
it('should return true if wallet exists', async () => {
|
||||
const wallet = createTestWallet('1', 'league1');
|
||||
await repository.create(wallet);
|
||||
|
||||
const result = await repository.exists('1');
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false if wallet does not exist', async () => {
|
||||
const result = await repository.exists('nonexistent');
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user