forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentTransactionsRail.test.tsx
More file actions
122 lines (107 loc) · 3.94 KB
/
Copy pathRecentTransactionsRail.test.tsx
File metadata and controls
122 lines (107 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { render, screen } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { RecentTransactionsRail } from './RecentTransactionsRail';
import type { Transaction } from '../types/creditLine';
type RichTransaction = Transaction & { lineName: string; lineId: string };
const mockTransactions: RichTransaction[] = [
{
id: 'TX-001',
type: 'Draw',
amount: 50000,
date: '2025-02-18T10:30:00Z',
note: 'Equipment purchase',
status: 'Completed',
lineName: 'Primary Business Line',
lineId: 'CL-2024-001',
},
{
id: 'TX-002',
type: 'Repay',
amount: 12500,
date: '2025-02-10T14:15:00Z',
note: 'Monthly repayment',
status: 'Completed',
lineName: 'Primary Business Line',
lineId: 'CL-2024-001',
},
{
id: 'TX-010',
type: 'Draw',
amount: 75000,
date: '2025-01-10T10:00:00Z',
note: 'Hiring new staff',
status: 'Completed',
lineName: 'Expansion Capital Line',
lineId: 'CL-2024-002',
},
];
const renderRail = (props: {
transactions?: RichTransaction[];
loading?: boolean;
}) => render(
<BrowserRouter>
<RecentTransactionsRail
transactions={props.transactions ?? []}
loading={props.loading ?? false}
/>
</BrowserRouter>,
);
describe('RecentTransactionsRail', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2025-02-20T12:00:00Z'));
});
afterEach(() => {
vi.useRealTimers();
});
it('renders with correct ARIA landmark and label', () => {
renderRail({});
const region = screen.getByRole('region', { name: /recent transactions/i });
expect(region).toBeInTheDocument();
});
it('renders skeleton placeholders when loading', () => {
const { container } = renderRail({ loading: true });
const skeletons = container.querySelectorAll('.skeleton');
expect(skeletons.length).toBeGreaterThan(0);
});
it('applies the recent-transactions-rail class to the root', () => {
const { container } = renderRail({ loading: true });
expect(container.querySelector('.recent-transactions-rail')).toBeInTheDocument();
});
it('shows empty state when there are no transactions', () => {
const { container } = renderRail({});
expect(container.querySelector('.rtr-empty')).toBeInTheDocument();
expect(screen.getByText('No transactions yet')).toBeInTheDocument();
});
it('renders transaction items', () => {
renderRail({ transactions: mockTransactions });
expect(screen.getByText('Equipment purchase')).toBeInTheDocument();
expect(screen.getByText('Monthly repayment')).toBeInTheDocument();
expect(screen.getByText('Hiring new staff')).toBeInTheDocument();
});
it('shows line name and relative time for each transaction', () => {
renderRail({ transactions: mockTransactions });
expect(screen.getAllByText(/Primary Business Line/)).toHaveLength(2);
expect(screen.getByText(/Expansion Capital Line/)).toBeInTheDocument();
});
it('prefixes draw amounts with a minus sign', () => {
renderRail({ transactions: [mockTransactions[0]] });
expect(screen.getByText(/\$50,000/).textContent).toMatch(/^\-/);
});
it('prefixes repay amounts with a plus sign', () => {
renderRail({ transactions: [mockTransactions[1]] });
expect(screen.getByText(/\$12,500/).textContent).toMatch(/^\+/);
});
it('renders a link to the full transaction history when transactions exist', () => {
renderRail({ transactions: mockTransactions.slice(0, 1) });
const link = screen.getByRole('link', { name: /view all transactions/i });
expect(link).toBeInTheDocument();
expect(link.getAttribute('href')).toBe('/transactions');
});
it('has aria-hidden on the activity icon', () => {
renderRail({ transactions: mockTransactions.slice(0, 1) });
const icon = document.querySelector('.activity-icon');
expect(icon?.getAttribute('aria-hidden')).toBe('true');
});
});