-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathAccountsOverview.integration.test.tsx
More file actions
178 lines (161 loc) · 7.11 KB
/
AccountsOverview.integration.test.tsx
File metadata and controls
178 lines (161 loc) · 7.11 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
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import AccountsOverview from '@/pages/AccountsOverview';
const toastMock = jest.fn();
jest.mock('@/hooks/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/badge', () => ({
Badge: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLSpanElement>) => (
<span {...props}>{children}</span>
),
}));
jest.mock('@/components/ui/progress', () => ({
Progress: ({ value, ...props }: { value?: number } & React.HTMLAttributes<HTMLDivElement>) => (
<div role="progressbar" aria-valuenow={value} {...props} />
),
}));
jest.mock('@/components/ui/financial-card', () => ({
FinancialCard: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLDivElement>) => <div {...props}>{children}</div>,
FinancialCardContent: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLDivElement>) => <div {...props}>{children}</div>,
FinancialCardDescription: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLDivElement>) => <div {...props}>{children}</div>,
FinancialCardHeader: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLDivElement>) => <div {...props}>{children}</div>,
FinancialCardTitle: ({ children, ...props }: React.PropsWithChildren & React.HTMLAttributes<HTMLDivElement>) => <div {...props}>{children}</div>,
}));
jest.mock('@/components/ui/dialog', () => ({
Dialog: ({ children, open }: React.PropsWithChildren & { open?: boolean }) => open ? <div>{children}</div> : null,
DialogContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
DialogHeader: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
DialogTitle: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
DialogDescription: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
DialogTrigger: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
DialogFooter: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
}));
jest.mock('@/components/ui/alert-dailog', () => ({
AlertDialog: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
AlertDialogTrigger: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
AlertDialogContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
AlertDialogHeader: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
AlertDialogTitle: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
AlertDialogDescription: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
AlertDialogFooter: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
AlertDialogCancel: ({ children }: React.PropsWithChildren) => <button>{children}</button>,
AlertDialogAction: ({ children, ...props }: React.PropsWithChildren & React.ButtonHTMLAttributes<HTMLButtonElement>) => <button {...props}>{children}</button>,
}));
jest.mock('@/lib/currency', () => ({
formatMoney: (amount: number) => `$${Number(amount || 0).toFixed(2)}`,
}));
const listAccountsMock = jest.fn();
const createAccountMock = jest.fn();
const updateAccountMock = jest.fn();
const deleteAccountMock = jest.fn();
const getAccountsOverviewMock = jest.fn();
jest.mock('@/api/accounts', () => ({
listAccounts: (...args: unknown[]) => listAccountsMock(...args),
createAccount: (...args: unknown[]) => createAccountMock(...args),
updateAccount: (...args: unknown[]) => updateAccountMock(...args),
deleteAccount: (...args: unknown[]) => deleteAccountMock(...args),
getAccountsOverview: (...args: unknown[]) => getAccountsOverviewMock(...args),
}));
const sampleAccount = {
id: 1,
name: 'Main Checking',
account_type: 'BANK',
institution: 'Chase',
balance: 5000,
currency: 'USD',
color: '#3B82F6',
active: true,
created_at: '2025-01-01T00:00:00',
};
const sampleOverview = {
total_balance: 15000,
net_worth: 13000,
total_assets: 15000,
total_liabilities: 2000,
account_count: 2,
type_breakdown: [
{
type: 'BANK',
total: 13000,
count: 1,
accounts: [sampleAccount],
},
{
type: 'CREDIT',
total: 2000,
count: 1,
accounts: [{
...sampleAccount,
id: 2,
name: 'Visa Card',
account_type: 'CREDIT',
balance: 2000,
color: '#EF4444',
}],
},
],
};
describe('AccountsOverview integration', () => {
beforeEach(() => {
jest.clearAllMocks();
listAccountsMock.mockResolvedValue([sampleAccount]);
getAccountsOverviewMock.mockResolvedValue(sampleOverview);
deleteAccountMock.mockResolvedValue({ message: 'deleted' });
});
it('renders accounts list with balance', async () => {
render(<AccountsOverview />);
await waitFor(() => expect(listAccountsMock).toHaveBeenCalled());
expect(await screen.findByText('Main Checking')).toBeInTheDocument();
expect(screen.getByText('$5000.00')).toBeInTheDocument();
});
it('shows empty state when no accounts', async () => {
listAccountsMock.mockResolvedValue([]);
getAccountsOverviewMock.mockResolvedValue({
total_balance: 0,
net_worth: 0,
total_assets: 0,
total_liabilities: 0,
account_count: 0,
type_breakdown: [],
});
render(<AccountsOverview />);
await waitFor(() => expect(listAccountsMock).toHaveBeenCalled());
expect(await screen.findByText(/no accounts yet/i)).toBeInTheDocument();
expect(screen.getByText(/add your first account/i)).toBeInTheDocument();
});
it('shows net worth and summary stats', async () => {
render(<AccountsOverview />);
await waitFor(() => expect(getAccountsOverviewMock).toHaveBeenCalled());
// Net worth
expect(await screen.findByText('$13000.00')).toBeInTheDocument();
// Total assets
expect(screen.getByText('$15000.00')).toBeInTheDocument();
// Total liabilities
expect(screen.getByText('$2000.00')).toBeInTheDocument();
});
it('shows type breakdown chart', async () => {
render(<AccountsOverview />);
await waitFor(() => expect(getAccountsOverviewMock).toHaveBeenCalled());
// Progress bars in breakdown
const progressBars = await screen.findAllByRole('progressbar');
expect(progressBars.length).toBeGreaterThan(0);
});
it('deletes an account when confirmed', async () => {
listAccountsMock.mockResolvedValue([sampleAccount]);
render(<AccountsOverview />);
await waitFor(() => expect(listAccountsMock).toHaveBeenCalled());
await screen.findByText('Main Checking');
// Click the delete confirmation button
const deleteButtons = screen.getAllByText('Delete');
await userEvent.click(deleteButtons[deleteButtons.length - 1]);
await waitFor(() => expect(deleteAccountMock).toHaveBeenCalledWith(1));
expect(toastMock).toHaveBeenCalledWith({ title: 'Account deleted' });
});
});