-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathAccounts.integration.test.tsx
More file actions
215 lines (192 loc) · 7.12 KB
/
Accounts.integration.test.tsx
File metadata and controls
215 lines (192 loc) · 7.12 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import Accounts from '@/pages/Accounts';
const toastMock = jest.fn();
jest.mock('@/components/ui/use-toast', () => ({
useToast: () => ({ toast: toastMock }),
}));
jest.mock('@/components/ui/button', () => ({
Button: ({ children, ...props }: React.PropsWithChildren & React.ButtonHTMLAttributes<HTMLButtonElement>) => (
<button {...props}>{children}</button>
),
}));
jest.mock('@/components/ui/input', () => ({
Input: ({ ...props }: React.InputHTMLAttributes<HTMLInputElement>) => <input {...props} />,
}));
jest.mock('@/components/ui/label', () => ({
Label: ({ children, ...props }: React.PropsWithChildren & React.LabelHTMLAttributes<HTMLLabelElement>) => (
<label {...props}>{children}</label>
),
}));
jest.mock('@/components/ui/dialog', () => ({
Dialog: ({ children, open }: React.PropsWithChildren & { open?: boolean }) => (
<div data-testid="dialog" data-open={open}>{children}</div>
),
DialogContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
DialogHeader: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
DialogTitle: ({ children }: React.PropsWithChildren) => <h3>{children}</h3>,
DialogTrigger: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
}));
jest.mock('@/components/ui/financial-card', () => ({
FinancialCard: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLDivElement>) => (
<div {...props}>{children}</div>
),
FinancialCardHeader: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
FinancialCardTitle: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLHeadingElement>) => (
<h3 {...props}>{children}</h3>
),
FinancialCardDescription: ({ children }: React.PropsWithChildren) => <p>{children}</p>,
FinancialCardContent: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLDivElement>) => (
<div {...props}>{children}</div>
),
}));
const overviewMock = jest.fn();
jest.mock('@/api/accounts', () => ({
getAccountsOverview: (...args: unknown[]) => overviewMock(...args),
createAccount: jest.fn(),
updateAccount: jest.fn(),
deleteAccount: jest.fn(),
}));
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {});
});
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
function renderWithProviders(ui: React.ReactElement) {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
return render(
<QueryClientProvider client={queryClient}>{ui}</QueryClientProvider>,
);
}
const emptyOverview = {
accounts: [],
total_balance: 0,
totals_by_currency: {},
account_count: 0,
recent_expenses: [],
upcoming_bills: [],
};
describe('Accounts page', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders empty state when no accounts exist', async () => {
overviewMock.mockResolvedValue(emptyOverview);
renderWithProviders(<Accounts />);
await waitFor(() => {
expect(screen.getByText(/no financial accounts yet/i)).toBeInTheDocument();
});
});
it('renders accounts list with balances', async () => {
overviewMock.mockResolvedValue({
accounts: [
{
id: 1,
name: 'Main Checking',
account_type: 'CHECKING',
balance: 5000,
currency: 'USD',
institution: 'Chase Bank',
active: true,
created_at: '2026-01-01T00:00:00',
},
{
id: 2,
name: 'Savings',
account_type: 'SAVINGS',
balance: 10000,
currency: 'USD',
institution: null,
active: true,
created_at: '2026-01-01T00:00:00',
},
],
total_balance: 15000,
totals_by_currency: { USD: 15000 },
account_count: 2,
recent_expenses: [],
upcoming_bills: [],
});
renderWithProviders(<Accounts />);
await waitFor(() => {
expect(screen.getByText('Main Checking')).toBeInTheDocument();
});
expect(screen.getByText('Savings')).toBeInTheDocument();
expect(screen.getByText('Chase Bank')).toBeInTheDocument();
// Verify currency-aware formatting
expect(screen.getByText('$15,000.00')).toBeInTheDocument();
});
it('shows Add Account button', async () => {
overviewMock.mockResolvedValue(emptyOverview);
renderWithProviders(<Accounts />);
await waitFor(() => {
expect(screen.getByText(/add account/i)).toBeInTheDocument();
});
});
it('renders recent expenses and upcoming bills', async () => {
overviewMock.mockResolvedValue({
accounts: [
{ id: 1, name: 'Checking', account_type: 'CHECKING', balance: 1000, currency: 'USD', institution: null, active: true, created_at: null },
],
total_balance: 1000,
totals_by_currency: { USD: 1000 },
account_count: 1,
recent_expenses: [
{ id: 1, amount: 50, currency: 'USD', description: 'Groceries', date: '2026-03-20' },
],
upcoming_bills: [
{ id: 1, name: 'Internet', amount: 49.99, currency: 'USD', next_due_date: '2026-04-01' },
],
});
renderWithProviders(<Accounts />);
await waitFor(() => {
expect(screen.getByText('Groceries')).toBeInTheDocument();
});
expect(screen.getByText('Internet')).toBeInTheDocument();
});
it('renders error state on fetch failure', async () => {
overviewMock.mockRejectedValue(new Error('Network error'));
renderWithProviders(<Accounts />);
await waitFor(() => {
expect(screen.getByText(/failed to load accounts/i)).toBeInTheDocument();
});
});
it('renders multi-currency totals correctly', async () => {
overviewMock.mockResolvedValue({
accounts: [
{ id: 1, name: 'USD', account_type: 'CHECKING', balance: 1000, currency: 'USD', institution: null, active: true, created_at: null },
{ id: 2, name: 'EUR', account_type: 'SAVINGS', balance: 2000, currency: 'EUR', institution: null, active: true, created_at: null },
],
total_balance: 3000,
totals_by_currency: { USD: 1000, EUR: 2000 },
account_count: 2,
recent_expenses: [],
upcoming_bills: [],
});
renderWithProviders(<Accounts />);
await waitFor(() => {
expect(screen.getByText('USD')).toBeInTheDocument();
});
// Both currency totals should be displayed
expect(screen.getByText('EUR')).toBeInTheDocument();
});
it('shows delete confirmation dialog', async () => {
overviewMock.mockResolvedValue({
accounts: [
{ id: 1, name: 'Test Account', account_type: 'CHECKING', balance: 100, currency: 'USD', institution: null, active: true, created_at: null },
],
total_balance: 100,
totals_by_currency: { USD: 100 },
account_count: 1,
recent_expenses: [],
upcoming_bills: [],
});
renderWithProviders(<Accounts />);
await waitFor(() => {
expect(screen.getByText('Test Account')).toBeInTheDocument();
});
// Delete confirmation dialog title should be present in the DOM
expect(screen.getByText('Delete Account')).toBeInTheDocument();
});
});