Files
gridpilot.gg/apps/website/lib/builders/view-data/LeagueWalletViewDataBuilder.test.ts
Marc Mintel 046852703f
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 5m51s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data fixes
2026-01-24 12:14:08 +01:00

279 lines
7.4 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { LeagueWalletViewDataBuilder } from './LeagueWalletViewDataBuilder';
describe('LeagueWalletViewDataBuilder', () => {
describe('happy paths', () => {
it('should transform LeagueWalletInputDTO to LeagueWalletViewData correctly', () => {
const leagueWalletApiDto = {
leagueId: 'league-123',
balance: 5000,
currency: 'USD',
totalRevenue: 5000,
totalFees: 0,
totalWithdrawals: 0,
pendingPayouts: 0,
canWithdraw: true,
transactions: [
{
id: 'txn-1',
type: 'sponsorship',
amount: 1000,
fee: 0,
netAmount: 1000,
status: 'completed',
date: '2024-01-01T10:00:00Z',
description: 'Sponsorship payment',
},
],
};
const result = LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(result).toEqual({
leagueId: 'league-123',
balance: 5000,
formattedBalance: 'USD 5,000',
totalRevenue: 5000,
formattedTotalRevenue: 'USD 5,000',
totalFees: 0,
formattedTotalFees: 'USD 0',
totalWithdrawals: 0,
pendingPayouts: 0,
formattedPendingPayouts: 'USD 0',
currency: 'USD',
canWithdraw: true,
withdrawalBlockReason: undefined,
transactions: [
{
id: 'txn-1',
type: 'sponsorship',
amount: 1000,
fee: 0,
netAmount: 1000,
status: 'completed',
date: '2024-01-01T10:00:00Z',
description: 'Sponsorship payment',
reference: undefined,
},
],
});
});
it('should handle empty transactions', () => {
const leagueWalletApiDto = {
leagueId: 'league-456',
balance: 0,
currency: 'USD',
totalRevenue: 0,
totalFees: 0,
totalWithdrawals: 0,
pendingPayouts: 0,
canWithdraw: true,
transactions: [],
};
const result = LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(result.transactions).toHaveLength(0);
expect(result.balance).toBe(0);
});
it('should handle multiple transactions', () => {
const leagueWalletApiDto = {
leagueId: 'league-789',
balance: 10000,
currency: 'USD',
totalRevenue: 10000,
totalFees: 0,
totalWithdrawals: 0,
pendingPayouts: 0,
canWithdraw: true,
transactions: [
{
id: 'txn-1',
type: 'sponsorship',
amount: 5000,
fee: 0,
netAmount: 5000,
status: 'completed',
date: '2024-01-01T10:00:00Z',
description: 'Sponsorship payment',
},
{
id: 'txn-2',
type: 'withdrawal',
amount: -1000,
fee: 0,
netAmount: -1000,
status: 'completed',
date: '2024-01-02T10:00:00Z',
description: 'Payout',
},
],
};
const result = LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(result.transactions).toHaveLength(2);
});
});
describe('data transformation', () => {
it('should preserve all DTO fields in the output', () => {
const leagueWalletApiDto = {
leagueId: 'league-101',
balance: 7500,
currency: 'EUR',
totalRevenue: 7500,
totalFees: 0,
totalWithdrawals: 0,
pendingPayouts: 0,
canWithdraw: true,
transactions: [
{
id: 'txn-1',
type: 'deposit',
amount: 2500,
fee: 0,
netAmount: 2500,
status: 'completed',
date: '2024-01-01T10:00:00Z',
description: 'Test transaction',
},
],
};
const result = LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(result.balance).toBe(leagueWalletApiDto.balance);
expect(result.currency).toBe(leagueWalletApiDto.currency);
});
it('should not modify the input DTO', () => {
const leagueWalletApiDto = {
leagueId: 'league-102',
balance: 5000,
currency: 'USD',
totalRevenue: 5000,
totalFees: 0,
totalWithdrawals: 0,
pendingPayouts: 0,
canWithdraw: true,
transactions: [],
};
const originalDto = JSON.parse(JSON.stringify(leagueWalletApiDto));
LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(leagueWalletApiDto).toEqual(originalDto);
});
});
describe('edge cases', () => {
it('should handle negative balance', () => {
const leagueWalletApiDto = {
leagueId: 'league-103',
balance: -500,
currency: 'USD',
totalRevenue: 0,
totalFees: 0,
totalWithdrawals: 500,
pendingPayouts: 0,
canWithdraw: false,
transactions: [
{
id: 'txn-1',
type: 'withdrawal',
amount: -500,
fee: 0,
netAmount: -500,
status: 'completed',
date: '2024-01-01T10:00:00Z',
description: 'Overdraft',
},
],
};
const result = LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(result.balance).toBe(-500);
});
it('should handle pending transactions', () => {
const leagueWalletApiDto = {
leagueId: 'league-104',
balance: 1000,
currency: 'USD',
totalRevenue: 1000,
totalFees: 0,
totalWithdrawals: 0,
pendingPayouts: 0,
canWithdraw: true,
transactions: [
{
id: 'txn-1',
type: 'sponsorship',
amount: 500,
fee: 0,
netAmount: 500,
status: 'pending',
date: '2024-01-01T10:00:00Z',
description: 'Pending payment',
},
],
};
const result = LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(result.transactions[0].status).toBe('pending');
});
it('should handle failed transactions', () => {
const leagueWalletApiDto = {
leagueId: 'league-105',
balance: 1000,
currency: 'USD',
totalRevenue: 1000,
totalFees: 0,
totalWithdrawals: 0,
pendingPayouts: 0,
canWithdraw: true,
transactions: [
{
id: 'txn-1',
type: 'sponsorship',
amount: 500,
fee: 0,
netAmount: 500,
status: 'failed',
date: '2024-01-01T10:00:00Z',
description: 'Failed payment',
},
],
};
const result = LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(result.transactions[0].status).toBe('failed');
});
it('should handle different currencies', () => {
const leagueWalletApiDto = {
leagueId: 'league-106',
balance: 1000,
currency: 'EUR',
totalRevenue: 1000,
totalFees: 0,
totalWithdrawals: 0,
pendingPayouts: 0,
canWithdraw: true,
transactions: [],
};
const result = LeagueWalletViewDataBuilder.build(leagueWalletApiDto as any);
expect(result.currency).toBe('EUR');
});
});
});