diff --git a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx index 943a2d1e1a47..1c02c5aa6262 100644 --- a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx @@ -15,6 +15,85 @@ describe('useTotalAccountBalance', () => { expect(result.current.balance).toBe(0); }); + test('should return total balance correctly when user has one account', () => { + const mock = mockStore({ + client: { + active_accounts: [ + { + balance: 10000, + currency: 'USD', + account_type: 'real', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper }); + expect(result.current.balance).toBe(10000); + }); + + test('should return total balance correctly when user has multiple accounts in same currency', () => { + const mock = mockStore({ + client: { + active_accounts: [ + { + balance: 10000, + currency: 'USD', + account_type: 'demo', + }, + { + balance: 10000, + currency: 'USD', + account_type: 'demo', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper }); + expect(result.current.balance).toBe(20000); + }); + + test('should return total balance correctly when user has multiple accounts in different currencies', () => { + const mock = mockStore({ + exchange_rates: { + data: { + rates: { + EUR: 2, + USD: 1, + }, + }, + }, + client: { + active_accounts: [ + { + balance: 10000, + currency: 'USD', + account_type: 'demo', + }, + { + balance: 10000, + currency: 'EUR', + account_type: 'demo', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper }); + expect(result.current.balance).not.toBe(20000); + expect(result.current.balance).toBe(15000); + }); + test('should return total balance correctly when user has multiple accounts', async () => { const mock = mockStore({ exchange_rates: { diff --git a/packages/hooks/src/useTotalAccountBalance.ts b/packages/hooks/src/useTotalAccountBalance.ts index 5ab91f88ecaf..44fdc175d1c4 100644 --- a/packages/hooks/src/useTotalAccountBalance.ts +++ b/packages/hooks/src/useTotalAccountBalance.ts @@ -1,21 +1,25 @@ import useRealTotalAssetCurrency from './useTotalAssetCurrency'; import useExchangeRate from './useExchangeRate'; - /** * we can use this hook to get the total balance of the given accounts list. * it loops through the accounts list and adds the balance of each account * to the total balance, it also converts the balance to the currency of the * first account in the list */ +type TUseTotalAccountBalance = { + balance?: number; + currency?: string; + account_type?: string; +}; -const useTotalAccountBalance = (accounts: { balance?: number; currency?: string }[]) => { +const useTotalAccountBalance = (accounts: TUseTotalAccountBalance[]) => { const total_assets_real_currency = useRealTotalAssetCurrency(); const { getRate } = useExchangeRate(); if (!accounts.length) return { balance: 0, currency: total_assets_real_currency }; const balance = accounts.reduce((total, account) => { - const base_rate = getRate(total_assets_real_currency || ''); + const base_rate = account?.account_type === 'demo' ? 1 : getRate(total_assets_real_currency || ''); const rate = getRate(account.currency || total_assets_real_currency || ''); const exchange_rate = base_rate / rate;