From 7b4ba8f9f19389cbd65144b1b419a07f7a574f54 Mon Sep 17 00:00:00 2001 From: shontzu <108507236+shontzu-deriv@users.noreply.github.com> Date: Fri, 21 Jul 2023 16:48:01 +0800 Subject: [PATCH] shontzu/WALL-865/Incorrect-total-asset-amount (#9217) * fix: attemp #3 * chore: remove irrelevant check * chore: unit test * chore: unit test fail * chore: fixes based on review * chore: empty commit * chore: code revie fixes * chore: revert test case changes * chore: rename type to TUseTotalAccountBalance * chore: format doc and added test case for multiple accounts in different currencies --------- Co-authored-by: Ali(Ako) Hosseini --- .../__tests__/useTotalAccountBalance.spec.tsx | 79 +++++++++++++++++++ packages/hooks/src/useTotalAccountBalance.ts | 10 ++- 2 files changed, 86 insertions(+), 3 deletions(-) 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;