75 lines
2.6 KiB
TypeScript
75 lines
2.6 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { PaymentViewModel } from './PaymentViewModel';
|
|
|
|
const createPaymentDto = (overrides: Partial<any> = {}): any => ({
|
|
id: 'pay-1',
|
|
type: 'membership_fee',
|
|
amount: 100.5,
|
|
platformFee: 10.5,
|
|
netAmount: 90,
|
|
payerId: 'sponsor-1',
|
|
payerType: 'sponsor',
|
|
leagueId: 'league-1',
|
|
seasonId: 'season-1',
|
|
status: 'completed',
|
|
createdAt: new Date('2024-01-01T10:00:00Z'),
|
|
completedAt: new Date('2024-01-01T11:00:00Z'),
|
|
...overrides,
|
|
});
|
|
|
|
describe('PaymentViewModel', () => {
|
|
it('maps DTO fields via Object.assign', () => {
|
|
const dto = createPaymentDto();
|
|
|
|
const vm = new PaymentViewModel(dto);
|
|
|
|
expect(vm.id).toBe('pay-1');
|
|
expect(vm.amount).toBe(100.5);
|
|
expect(vm.netAmount).toBe(90);
|
|
expect(vm.payerId).toBe('sponsor-1');
|
|
expect(vm.leagueId).toBe('league-1');
|
|
});
|
|
|
|
it('formats amount and netAmount as EUR currency strings', () => {
|
|
const vm = new PaymentViewModel(createPaymentDto({ amount: 50, netAmount: 40 }));
|
|
|
|
expect(vm.formattedAmount).toBe('€50.00');
|
|
expect(vm.formattedNetAmount).toBe('€40.00');
|
|
});
|
|
|
|
it('maps status to a statusColor', () => {
|
|
const completed = new PaymentViewModel(createPaymentDto({ status: 'completed' }));
|
|
const pending = new PaymentViewModel(createPaymentDto({ status: 'pending' }));
|
|
const failed = new PaymentViewModel(createPaymentDto({ status: 'failed' }));
|
|
const refunded = new PaymentViewModel(createPaymentDto({ status: 'refunded' }));
|
|
const other = new PaymentViewModel(createPaymentDto({ status: 'unknown' }));
|
|
|
|
expect(completed.statusColor).toBe('green');
|
|
expect(pending.statusColor).toBe('yellow');
|
|
expect(failed.statusColor).toBe('red');
|
|
expect(refunded.statusColor).toBe('orange');
|
|
expect(other.statusColor).toBe('gray');
|
|
});
|
|
|
|
it('formats createdAt and completedAt using locale formatting', () => {
|
|
const vm = new PaymentViewModel(createPaymentDto());
|
|
|
|
expect(typeof vm.formattedCreatedAt).toBe('string');
|
|
expect(typeof vm.formattedCompletedAt).toBe('string');
|
|
});
|
|
|
|
it('handles missing completedAt with fallback text', () => {
|
|
const vm = new PaymentViewModel(createPaymentDto({ completedAt: undefined }));
|
|
|
|
expect(vm.formattedCompletedAt).toBe('Not completed');
|
|
});
|
|
|
|
it('derives display labels for status, type and payer type', () => {
|
|
const vm = new PaymentViewModel(createPaymentDto({ status: 'pending', type: 'membership_fee', payerType: 'league' }));
|
|
|
|
expect(vm.statusDisplay).toBe('Pending');
|
|
expect(vm.typeDisplay).toBe('Membership Fee');
|
|
expect(vm.payerTypeDisplay).toBe('League');
|
|
});
|
|
});
|