|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { Accounts } from '@/pages/Accounts'; |
| 5 | + |
| 6 | +jest.mock('@/components/ui/button', () => ({ |
| 7 | + Button: ({ children, ...props }: React.PropsWithChildren & React.ButtonHTMLAttributes<HTMLButtonElement>) => ( |
| 8 | + <button {...props}>{children}</button> |
| 9 | + ), |
| 10 | +})); |
| 11 | +jest.mock('@/components/ui/input', () => ({ |
| 12 | + Input: ({ ...props }: React.InputHTMLAttributes<HTMLInputElement>) => <input {...props} />, |
| 13 | +})); |
| 14 | +jest.mock('@/components/ui/label', () => ({ |
| 15 | + Label: ({ children, ...props }: React.PropsWithChildren & React.LabelHTMLAttributes<HTMLLabelElement>) => ( |
| 16 | + <label {...props}>{children}</label> |
| 17 | + ), |
| 18 | +})); |
| 19 | +jest.mock('@/components/ui/financial-card', () => ({ |
| 20 | + FinancialCard: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 21 | + FinancialCardHeader: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 22 | + FinancialCardContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 23 | + FinancialCardTitle: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 24 | + FinancialCardDescription: ({ children }: React.PropsWithChildren) => <div>{children}</div>, |
| 25 | +})); |
| 26 | + |
| 27 | +const toastMock = jest.fn(); |
| 28 | +jest.mock('@/hooks/use-toast', () => ({ |
| 29 | + useToast: () => ({ toast: toastMock }), |
| 30 | +})); |
| 31 | + |
| 32 | +const getAccountsOverviewMock = jest.fn(); |
| 33 | +const createAccountMock = jest.fn(); |
| 34 | +jest.mock('@/api/accounts', () => ({ |
| 35 | + getAccountsOverview: (...args: unknown[]) => getAccountsOverviewMock(...args), |
| 36 | + createAccount: (...args: unknown[]) => createAccountMock(...args), |
| 37 | +})); |
| 38 | + |
| 39 | +describe('Accounts integration', () => { |
| 40 | + beforeEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + getAccountsOverviewMock.mockResolvedValue({ |
| 43 | + period: { month: '2026-05' }, |
| 44 | + summary: { |
| 45 | + account_count: 2, |
| 46 | + assets: 7000, |
| 47 | + liabilities: 300, |
| 48 | + net_worth: 6700, |
| 49 | + }, |
| 50 | + accounts: [ |
| 51 | + { |
| 52 | + id: 1, |
| 53 | + name: 'Main Checking', |
| 54 | + account_type: 'CHECKING', |
| 55 | + balance: 2000, |
| 56 | + currency: 'USD', |
| 57 | + institution: 'Acme Bank', |
| 58 | + active: true, |
| 59 | + monthly_income: 3000, |
| 60 | + monthly_expenses: 120, |
| 61 | + monthly_net_flow: 2880, |
| 62 | + transaction_count: 2, |
| 63 | + }, |
| 64 | + ], |
| 65 | + by_type: [{ account_type: 'CHECKING', count: 1, balance: 2000 }], |
| 66 | + by_currency: [{ currency: 'USD', assets: 7000, liabilities: 300, net_worth: 6700 }], |
| 67 | + recent_transactions: [ |
| 68 | + { |
| 69 | + id: 10, |
| 70 | + account_id: 1, |
| 71 | + account_name: 'Main Checking', |
| 72 | + description: 'Groceries', |
| 73 | + amount: 120, |
| 74 | + currency: 'USD', |
| 75 | + date: '2026-05-04', |
| 76 | + type: 'EXPENSE', |
| 77 | + }, |
| 78 | + ], |
| 79 | + }); |
| 80 | + createAccountMock.mockResolvedValue({ id: 3 }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('renders account overview data', async () => { |
| 84 | + render(<Accounts />); |
| 85 | + await waitFor(() => expect(getAccountsOverviewMock).toHaveBeenCalled()); |
| 86 | + |
| 87 | + expect(screen.getByRole('heading', { name: /accounts/i })).toBeInTheDocument(); |
| 88 | + expect(screen.getAllByText(/main checking/i).length).toBeGreaterThan(0); |
| 89 | + expect(screen.getByText(/acme bank/i)).toBeInTheDocument(); |
| 90 | + expect(screen.getByText(/groceries/i)).toBeInTheDocument(); |
| 91 | + expect(screen.getByText(/currency groups/i)).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('creates an account and reloads overview', async () => { |
| 95 | + const user = userEvent.setup(); |
| 96 | + render(<Accounts />); |
| 97 | + await waitFor(() => expect(getAccountsOverviewMock).toHaveBeenCalledTimes(1)); |
| 98 | + |
| 99 | + await user.type(screen.getByLabelText(/account name/i), 'Savings'); |
| 100 | + await user.selectOptions(screen.getByLabelText(/account type/i), 'SAVINGS'); |
| 101 | + await user.clear(screen.getByLabelText(/account balance/i)); |
| 102 | + await user.type(screen.getByLabelText(/account balance/i), '1500'); |
| 103 | + await user.clear(screen.getByLabelText(/account currency/i)); |
| 104 | + await user.type(screen.getByLabelText(/account currency/i), 'usd'); |
| 105 | + await user.type(screen.getByLabelText(/account institution/i), 'Credit Union'); |
| 106 | + await user.click(screen.getByRole('button', { name: /create account/i })); |
| 107 | + |
| 108 | + await waitFor(() => |
| 109 | + expect(createAccountMock).toHaveBeenCalledWith( |
| 110 | + expect.objectContaining({ |
| 111 | + name: 'Savings', |
| 112 | + account_type: 'SAVINGS', |
| 113 | + balance: 1500, |
| 114 | + currency: 'USD', |
| 115 | + institution: 'Credit Union', |
| 116 | + }), |
| 117 | + ), |
| 118 | + ); |
| 119 | + await waitFor(() => expect(getAccountsOverviewMock).toHaveBeenCalledTimes(2)); |
| 120 | + }); |
| 121 | +}); |
0 commit comments