website refactor
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
WithdrawFromLeagueWalletUseCase,
|
||||
type WithdrawFromLeagueWalletErrorCode,
|
||||
type WithdrawFromLeagueWalletInput,
|
||||
type WithdrawFromLeagueWalletResult,
|
||||
} from './WithdrawFromLeagueWalletUseCase';
|
||||
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
|
||||
import type { LeagueWalletRepository } from '../../domain/repositories/LeagueWalletRepository';
|
||||
import type { TransactionRepository } from '../../domain/repositories/TransactionRepository';
|
||||
import type { Logger } from '@core/shared/domain/Logger';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import { League } from '../../domain/entities/League';
|
||||
import { LeagueWallet } from '../../domain/entities/league-wallet/LeagueWallet';
|
||||
import { Money } from '../../domain/value-objects/Money';
|
||||
import {
|
||||
WithdrawFromLeagueWalletUseCase,
|
||||
type WithdrawFromLeagueWalletErrorCode,
|
||||
type WithdrawFromLeagueWalletInput
|
||||
} from './WithdrawFromLeagueWalletUseCase';
|
||||
|
||||
describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
let leagueRepository: { findById: Mock };
|
||||
@@ -25,13 +22,16 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
walletRepository = { findByLeagueId: vi.fn(), update: vi.fn() };
|
||||
transactionRepository = { create: vi.fn() };
|
||||
|
||||
logger = { error: vi.fn() } as unknown as Logger & { error: Mock };
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn()
|
||||
} as any;
|
||||
|
||||
};
|
||||
|
||||
useCase = new WithdrawFromLeagueWalletUseCase(leagueRepository as unknown as ILeagueRepository,
|
||||
walletRepository as unknown as ILeagueWalletRepository,
|
||||
transactionRepository as unknown as ITransactionRepository,
|
||||
useCase = new WithdrawFromLeagueWalletUseCase(leagueRepository as any,
|
||||
walletRepository as any,
|
||||
transactionRepository as any,
|
||||
logger);
|
||||
});
|
||||
|
||||
@@ -55,7 +55,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(err.details.message).toBe('League with id league-1 not found');
|
||||
});
|
||||
});
|
||||
|
||||
it('returns WALLET_NOT_FOUND when wallet is missing', async () => {
|
||||
const league = League.create({
|
||||
@@ -85,7 +85,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('WALLET_NOT_FOUND');
|
||||
expect(err.details.message).toBe('Wallet for league league-1 not found');
|
||||
});
|
||||
});
|
||||
|
||||
it('returns UNAUTHORIZED_WITHDRAWAL when requester is not owner', async () => {
|
||||
const league = League.create({
|
||||
@@ -121,7 +121,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('UNAUTHORIZED_WITHDRAWAL');
|
||||
expect(err.details.message).toBe('Only the league owner can withdraw from the league wallet');
|
||||
});
|
||||
});
|
||||
|
||||
it('returns INSUFFICIENT_FUNDS when wallet cannot withdraw amount', async () => {
|
||||
const league = League.create({
|
||||
@@ -157,7 +157,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
|
||||
expect(err.code).toBe('INSUFFICIENT_FUNDS');
|
||||
expect(err.details.message).toBe('Insufficient balance for withdrawal');
|
||||
});
|
||||
});
|
||||
|
||||
it('creates withdrawal transaction and updates wallet on success', async () => {
|
||||
vi.useFakeTimers();
|
||||
@@ -193,17 +193,10 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const presented = result.unwrap();
|
||||
|
||||
expect(transactionRepository.create).toHaveBeenCalledTimes(1);
|
||||
const createdTx = (transactionRepository.create as Mock).mock.calls[0]![0] as {
|
||||
id: { toString(): string };
|
||||
type: string;
|
||||
amount: { amount: number; currency: string };
|
||||
description: string | undefined;
|
||||
metadata: Record<string, unknown> | undefined;
|
||||
walletId: { toString(): string };
|
||||
};
|
||||
const createdTx = (transactionRepository.create as Mock).mock.calls[0]![0] as any;
|
||||
|
||||
const expectedTransactionId = `txn-${new Date('2025-01-01T00:00:00.000Z').getTime()}`;
|
||||
|
||||
@@ -222,7 +215,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
expect(updatedWallet.balance.currency).toBe('USD');
|
||||
expect(updatedWallet.getTransactionIds()).toContain(expectedTransactionId);
|
||||
|
||||
const presented = (expect(presented.leagueId).toBe('league-1');
|
||||
expect(presented.leagueId).toBe('league-1');
|
||||
expect(presented.amount.amount).toBe(250);
|
||||
expect(presented.amount.currency).toBe('USD');
|
||||
expect(presented.transactionId).toBe(expectedTransactionId);
|
||||
@@ -274,4 +267,4 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
expect(err.details.message).toBe('DB down');
|
||||
expect(logger.error).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user