From 6b0bfd1660d5b04da7d68a98fc776fde876e2b8e Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Fri, 28 Apr 2023 19:25:29 +0330 Subject: [PATCH 01/40] feat: adding new tests [WIP] --- .../src/__tests__/useCFDAllAccounts.spec.tsx | 89 +++++++++++++++++++ .../src/__tests__/useCFDDemoAccounts.spec.tsx | 68 ++++++++++++++ .../src/__tests__/useCFDRealAccounts.spec.tsx | 68 ++++++++++++++ .../useCurrencyExchangeRate.spec.tsx | 46 ++++++++++ .../src/__tests__/useGetCfdAccounts.spec.tsx | 77 ++++++++++++++++ .../__tests__/usePlatformAccounts.spec.tsx | 77 ++++++++++++++++ .../__tests__/usePlatformDemoAccount.spec.tsx | 77 ++++++++++++++++ .../usePlatformRealAccounts.spec.tsx | 77 ++++++++++++++++ .../__tests__/useTotalAccountBalance.spec.tsx | 77 ++++++++++++++++ .../__tests__/useTotalAssetCurrency.spec.tsx | 77 ++++++++++++++++ 10 files changed, 733 insertions(+) create mode 100644 packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx create mode 100644 packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx create mode 100644 packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx create mode 100644 packages/hooks/src/__tests__/useCurrencyExchangeRate.spec.tsx create mode 100644 packages/hooks/src/__tests__/useGetCfdAccounts.spec.tsx create mode 100644 packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx create mode 100644 packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx create mode 100644 packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx create mode 100644 packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx create mode 100644 packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx diff --git a/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx new file mode 100644 index 000000000000..8ecc10107c25 --- /dev/null +++ b/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx @@ -0,0 +1,89 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useCFDAllAccounts from '../useCFDAllAccounts'; + +describe('useCFDAllAccounts', () => { + test('should return empty array when client has no cfd accounts', async () => { + const mock = mockStore({ + client: {}, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDAllAccounts(), { wrapper }); + + expect(result.current.length).toBe(0); + }); + + test('should return proper data when client has mt5 accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'real', + balance: 1000, + currency: 'USD', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDAllAccounts(), { wrapper }); + + expect(result.current.length).toBe(1); + }); + + test('should return proper data when client has dxtrade accounts', async () => { + const mock = mockStore({ + client: { + dxtrade_accounts_list: [ + { + account_type: 'real', + balance: 1000, + currency: 'USD', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDAllAccounts(), { wrapper }); + + expect(result.current.length).toBe(1); + }); + + test('should return proper data when client has both mt5 and dxtrade accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'real', + balance: 1000, + currency: 'USD', + }, + ], + dxtrade_accounts_list: [ + { + account_type: 'real', + balance: 1000, + currency: 'USD', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDAllAccounts(), { wrapper }); + + expect(result.current.length).toBe(2); + }); +}); diff --git a/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx new file mode 100644 index 000000000000..6f4d0aae0d9f --- /dev/null +++ b/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx @@ -0,0 +1,68 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useCFDDemoAccounts from '../useCFDDemoAccounts'; + +describe('useCFDDemoAccounts', () => { + test('should return empty array when user has no cfd accounts', async () => { + const mock = mockStore({}); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDDemoAccounts(), { wrapper }); + + expect(result.current?.length).toBe(0); + }); + + test('should return empty array when user has no cfd demo accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'real', + }, + ], + dxtrade_accounts_list: [ + { + account_type: 'real', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDDemoAccounts(), { wrapper }); + + expect(result.current?.length).toBe(0); + }); + + test('should return proper data when user has cfd demo accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'demo', + }, + ], + dxtrade_accounts_list: [ + { + account_type: 'real', + }, + { + account_type: 'demo', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDDemoAccounts(), { wrapper }); + + expect(result.current?.length).toBe(2); + }); +}); diff --git a/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx new file mode 100644 index 000000000000..acd6f13852cf --- /dev/null +++ b/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx @@ -0,0 +1,68 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useCFDRealAccounts from '../useCFDRealAccounts'; + +describe('useCFDRealAccounts', () => { + test('should return empty array when user has no cfd accounts', async () => { + const mock = mockStore({}); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDRealAccounts(), { wrapper }); + + expect(result.current?.length).toBe(0); + }); + + test('should return empty array when user has no cfd real accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'demo', + }, + ], + dxtrade_accounts_list: [ + { + account_type: 'demo', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDRealAccounts(), { wrapper }); + + expect(result.current?.length).toBe(0); + }); + + test('should return proper data when user has cfd real accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'real', + }, + ], + dxtrade_accounts_list: [ + { + account_type: 'real', + }, + { + account_type: 'demo', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCFDRealAccounts(), { wrapper }); + + expect(result.current?.length).toBe(2); + }); +}); diff --git a/packages/hooks/src/__tests__/useCurrencyExchangeRate.spec.tsx b/packages/hooks/src/__tests__/useCurrencyExchangeRate.spec.tsx new file mode 100644 index 000000000000..5673f1adeaef --- /dev/null +++ b/packages/hooks/src/__tests__/useCurrencyExchangeRate.spec.tsx @@ -0,0 +1,46 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useCurrencyExchangeRate from '../useCurrencyExchangeRate'; + +describe('useCurrencyExchangeRate', () => { + test('should return 1 if currency is not found', async () => { + const mock = mockStore({ + exchange_rates: { + data: { + base_currency: 'USD', + rates: { + EUR: 1.3, + GBP: 1.4, + ETH: 0.0001, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCurrencyExchangeRate('JPY'), { wrapper }); + expect(result.current).toBe(1); + }); + + test('should return correct rate for the given currency other than USD', async () => { + const mock = mockStore({ + exchange_rates: { + data: { + rates: { + EUR: 1.3, + GBP: 1.5, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useCurrencyExchangeRate('EUR'), { wrapper }); + expect(result.current).toBe(1.3); + }); +}); diff --git a/packages/hooks/src/__tests__/useGetCfdAccounts.spec.tsx b/packages/hooks/src/__tests__/useGetCfdAccounts.spec.tsx new file mode 100644 index 000000000000..2979e8cca36e --- /dev/null +++ b/packages/hooks/src/__tests__/useGetCfdAccounts.spec.tsx @@ -0,0 +1,77 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; + +describe('useRealTotalAssetCurrency', () => { + test('should return default currency when user has no account', async () => { + const mock = mockStore({ + client: { + account_list: [], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('USD'); + }); + + test('should return proper currency when user has non_crypto account', async () => { + const mock = mockStore({ + client: { + accounts: { + loginid: { + currency: 'EUR', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('EUR'); + }); + + // test('should return empty string when user has not set currency for non_crypto account', async () => {} + + // test('should return true if is_virtual is true', async () => { + // const mock = mockStore({ + // client: { + // is_virtual: true, + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); + + // test('should return true account title is not Real ', async () => { + // const mock = mockStore({ + // client: { + // account_list: [ + // { + // title: 'Demo', + // is_virtual: false, + // }, + // ], + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); +}); diff --git a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx new file mode 100644 index 000000000000..c93fdebe323d --- /dev/null +++ b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx @@ -0,0 +1,77 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useTotalAssetCurrency from '../useTotalAssetCurrency'; + +describe('useTotalAssetCurrency', () => { + test('should return default currency when user has no account', async () => { + const mock = mockStore({ + client: { + account_list: [], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('USD'); + }); + + test('should return proper currency when user has non_crypto account', async () => { + const mock = mockStore({ + client: { + accounts: { + loginid: { + currency: 'EUR', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('EUR'); + }); + + // test('should return empty string when user has not set currency for non_crypto account', async () => {} + + // test('should return true if is_virtual is true', async () => { + // const mock = mockStore({ + // client: { + // is_virtual: true, + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); + + // test('should return true account title is not Real ', async () => { + // const mock = mockStore({ + // client: { + // account_list: [ + // { + // title: 'Demo', + // is_virtual: false, + // }, + // ], + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); +}); diff --git a/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx b/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx new file mode 100644 index 000000000000..2979e8cca36e --- /dev/null +++ b/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx @@ -0,0 +1,77 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; + +describe('useRealTotalAssetCurrency', () => { + test('should return default currency when user has no account', async () => { + const mock = mockStore({ + client: { + account_list: [], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('USD'); + }); + + test('should return proper currency when user has non_crypto account', async () => { + const mock = mockStore({ + client: { + accounts: { + loginid: { + currency: 'EUR', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('EUR'); + }); + + // test('should return empty string when user has not set currency for non_crypto account', async () => {} + + // test('should return true if is_virtual is true', async () => { + // const mock = mockStore({ + // client: { + // is_virtual: true, + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); + + // test('should return true account title is not Real ', async () => { + // const mock = mockStore({ + // client: { + // account_list: [ + // { + // title: 'Demo', + // is_virtual: false, + // }, + // ], + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); +}); diff --git a/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx new file mode 100644 index 000000000000..2979e8cca36e --- /dev/null +++ b/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx @@ -0,0 +1,77 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; + +describe('useRealTotalAssetCurrency', () => { + test('should return default currency when user has no account', async () => { + const mock = mockStore({ + client: { + account_list: [], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('USD'); + }); + + test('should return proper currency when user has non_crypto account', async () => { + const mock = mockStore({ + client: { + accounts: { + loginid: { + currency: 'EUR', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('EUR'); + }); + + // test('should return empty string when user has not set currency for non_crypto account', async () => {} + + // test('should return true if is_virtual is true', async () => { + // const mock = mockStore({ + // client: { + // is_virtual: true, + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); + + // test('should return true account title is not Real ', async () => { + // const mock = mockStore({ + // client: { + // account_list: [ + // { + // title: 'Demo', + // is_virtual: false, + // }, + // ], + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); +}); diff --git a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx new file mode 100644 index 000000000000..2979e8cca36e --- /dev/null +++ b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx @@ -0,0 +1,77 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; + +describe('useRealTotalAssetCurrency', () => { + test('should return default currency when user has no account', async () => { + const mock = mockStore({ + client: { + account_list: [], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('USD'); + }); + + test('should return proper currency when user has non_crypto account', async () => { + const mock = mockStore({ + client: { + accounts: { + loginid: { + currency: 'EUR', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('EUR'); + }); + + // test('should return empty string when user has not set currency for non_crypto account', async () => {} + + // test('should return true if is_virtual is true', async () => { + // const mock = mockStore({ + // client: { + // is_virtual: true, + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); + + // test('should return true account title is not Real ', async () => { + // const mock = mockStore({ + // client: { + // account_list: [ + // { + // title: 'Demo', + // is_virtual: false, + // }, + // ], + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); +}); diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx new file mode 100644 index 000000000000..2979e8cca36e --- /dev/null +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -0,0 +1,77 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; + +describe('useRealTotalAssetCurrency', () => { + test('should return default currency when user has no account', async () => { + const mock = mockStore({ + client: { + account_list: [], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('USD'); + }); + + test('should return proper currency when user has non_crypto account', async () => { + const mock = mockStore({ + client: { + accounts: { + loginid: { + currency: 'EUR', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('EUR'); + }); + + // test('should return empty string when user has not set currency for non_crypto account', async () => {} + + // test('should return true if is_virtual is true', async () => { + // const mock = mockStore({ + // client: { + // is_virtual: true, + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); + + // test('should return true account title is not Real ', async () => { + // const mock = mockStore({ + // client: { + // account_list: [ + // { + // title: 'Demo', + // is_virtual: false, + // }, + // ], + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(true); + // }); +}); From a5d5ceecb347306090cc1e26a69404347ba6230a Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Fri, 28 Apr 2023 19:26:46 +0330 Subject: [PATCH 02/40] fix: updated exchange_store provider for test files[co-authored by farzin-deriv] --- packages/stores/src/mockStore.ts | 3 ++- packages/stores/src/storeProvider.tsx | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/stores/src/mockStore.ts b/packages/stores/src/mockStore.ts index 4cc88799a073..b6d7aaf20e3d 100644 --- a/packages/stores/src/mockStore.ts +++ b/packages/stores/src/mockStore.ts @@ -1,8 +1,9 @@ import merge from 'lodash.merge'; import { TStores } from '../types'; -const mock = (): TStores => { +const mock = (): TStores & { is_mock: boolean } => { return { + is_mock: true, client: { accounts: { loginid: { diff --git a/packages/stores/src/storeProvider.tsx b/packages/stores/src/storeProvider.tsx index 3c0b18a1b872..e7708e3bb6b2 100644 --- a/packages/stores/src/storeProvider.tsx +++ b/packages/stores/src/storeProvider.tsx @@ -5,18 +5,21 @@ import { ExchangeRatesProvider } from './providers'; import type { TCoreStores, TStores } from '../types'; const StoreProvider = ({ children, store }: PropsWithChildren<{ store: TCoreStores }>) => { - const memoizedValue: TStores = useMemo( - () => ({ + const memoizedValue: TStores = useMemo(() => { + // If the store is mocked for testing purposes, then return the mocked value. + if ('is_mock' in store && store.is_mock) return store as unknown as TStores; + + // Otherwise, instantiate store and return it. + return { ...store, exchange_rates: new ExchangeRatesStore(), - }), - [store] - ); + }; + }, [store]); useEffect(() => { return () => { Object.values(memoizedValue).forEach(value => { - if ('unmount' in value) value.unmount(); + if (typeof value === 'object' && 'unmount' in value) value.unmount(); }); }; }, [memoizedValue]); From a76a9d4cf0cb4b3d77d1dc907c7413aebb9ad48a Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 1 May 2023 00:00:28 +0330 Subject: [PATCH 03/40] feat: new tests added [WIP] --- .../__tests__/usePlatformAccounts.spec.tsx | 29 ++------ .../__tests__/usePlatformDemoAccount.spec.tsx | 64 +++++------------ .../usePlatformRealAccounts.spec.tsx | 71 +++---------------- .../__tests__/useTotalAccountBalance.spec.tsx | 70 +++++------------- 4 files changed, 48 insertions(+), 186 deletions(-) diff --git a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx index c93fdebe323d..5cc212859e5d 100644 --- a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx @@ -1,9 +1,9 @@ import * as React from 'react'; import { mockStore, StoreProvider } from '@deriv/stores'; import { renderHook } from '@testing-library/react-hooks'; -import useTotalAssetCurrency from '../useTotalAssetCurrency'; +import usePlatformAccounts from '../usePlatformAccounts'; -describe('useTotalAssetCurrency', () => { +describe('usePlatformAccounts', () => { test('should return default currency when user has no account', async () => { const mock = mockStore({ client: { @@ -14,32 +14,11 @@ describe('useTotalAssetCurrency', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useTotalAssetCurrency(), { wrapper }); + const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); - expect(result.current).toBe('USD'); + expect(result.current.demo).toBe(0); }); - test('should return proper currency when user has non_crypto account', async () => { - const mock = mockStore({ - client: { - accounts: { - loginid: { - currency: 'EUR', - }, - }, - }, - }); - - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => useTotalAssetCurrency(), { wrapper }); - - expect(result.current).toBe('EUR'); - }); - - // test('should return empty string when user has not set currency for non_crypto account', async () => {} - // test('should return true if is_virtual is true', async () => { // const mock = mockStore({ // client: { diff --git a/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx b/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx index 2979e8cca36e..560ccc0b80ea 100644 --- a/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx +++ b/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx @@ -1,30 +1,35 @@ import * as React from 'react'; import { mockStore, StoreProvider } from '@deriv/stores'; import { renderHook } from '@testing-library/react-hooks'; -import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; +import usePlatformDemoAccount from '../usePlatformDemoAccount'; -describe('useRealTotalAssetCurrency', () => { - test('should return default currency when user has no account', async () => { +describe('usePlatformDemoAccount', () => { + test('should return null when user has no platform demo accounts', async () => { const mock = mockStore({ client: { - account_list: [], + accounts: { + VR1234: { + is_virtual: 0, + }, + }, }, }); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + const { result } = renderHook(() => usePlatformDemoAccount(), { wrapper }); - expect(result.current).toBe('USD'); + expect(result.current).toBe(undefined); }); - test('should return proper currency when user has non_crypto account', async () => { + test('should return proper data when user has platform demo account', async () => { const mock = mockStore({ client: { accounts: { - loginid: { - currency: 'EUR', + VR1234: { + is_virtual: 1, + loginid: 'VR1234', }, }, }, @@ -33,45 +38,8 @@ describe('useRealTotalAssetCurrency', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + const { result } = renderHook(() => usePlatformDemoAccount(), { wrapper }); - expect(result.current).toBe('EUR'); + expect(result.current?.loginid).toBe('VR1234'); }); - - // test('should return empty string when user has not set currency for non_crypto account', async () => {} - - // test('should return true if is_virtual is true', async () => { - // const mock = mockStore({ - // client: { - // is_virtual: true, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); - - // test('should return true account title is not Real ', async () => { - // const mock = mockStore({ - // client: { - // account_list: [ - // { - // title: 'Demo', - // is_virtual: false, - // }, - // ], - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); }); diff --git a/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx index 2979e8cca36e..be2c762e3ed3 100644 --- a/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx @@ -1,77 +1,28 @@ import * as React from 'react'; import { mockStore, StoreProvider } from '@deriv/stores'; import { renderHook } from '@testing-library/react-hooks'; -import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; +import usePlatformRealAccounts from '../usePlatformRealAccounts'; -describe('useRealTotalAssetCurrency', () => { - test('should return default currency when user has no account', async () => { +describe('usePlatformRealAccounts', () => { + test('should return null when user has no platform demo accounts', async () => { const mock = mockStore({ - client: { - account_list: [], + traders_hub: { + is_eu_user: true, }, - }); - - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - expect(result.current).toBe('USD'); - }); - - test('should return proper currency when user has non_crypto account', async () => { - const mock = mockStore({ client: { - accounts: { - loginid: { - currency: 'EUR', + active_accounts: [ + { + landing_company_shortcode: 'svg', }, - }, + ], }, }); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + const { result } = renderHook(() => usePlatformRealAccounts(), { wrapper }); - expect(result.current).toBe('EUR'); + expect(result.current.length).toBe(1); }); - - // test('should return empty string when user has not set currency for non_crypto account', async () => {} - - // test('should return true if is_virtual is true', async () => { - // const mock = mockStore({ - // client: { - // is_virtual: true, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); - - // test('should return true account title is not Real ', async () => { - // const mock = mockStore({ - // client: { - // account_list: [ - // { - // title: 'Demo', - // is_virtual: false, - // }, - // ], - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); }); diff --git a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx index 2979e8cca36e..0fd25cdc7079 100644 --- a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx @@ -1,77 +1,41 @@ import * as React from 'react'; import { mockStore, StoreProvider } from '@deriv/stores'; import { renderHook } from '@testing-library/react-hooks'; -import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; +import useTotalAccountBalance from '../useTotalAccountBalance'; -describe('useRealTotalAssetCurrency', () => { - test('should return default currency when user has no account', async () => { - const mock = mockStore({ - client: { - account_list: [], - }, - }); +describe('useTotalAccountBalance', () => { + test('should return zero when user has no account', async () => { + const mock = mockStore({}); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper }); - expect(result.current).toBe('USD'); + expect(result.current.balance).toBe(0); }); - test('should return proper currency when user has non_crypto account', async () => { + test('should return total balance correctly when user has multiple accounts', async () => { const mock = mockStore({ client: { - accounts: { - loginid: { + active_accounts: [ + { + currency: 'USD', + balance: 100, + }, + { currency: 'EUR', + balance: 200, }, - }, + ], }, }); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper }); - expect(result.current).toBe('EUR'); + expect(result.current.balance).toBe(300); }); - - // test('should return empty string when user has not set currency for non_crypto account', async () => {} - - // test('should return true if is_virtual is true', async () => { - // const mock = mockStore({ - // client: { - // is_virtual: true, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); - - // test('should return true account title is not Real ', async () => { - // const mock = mockStore({ - // client: { - // account_list: [ - // { - // title: 'Demo', - // is_virtual: false, - // }, - // ], - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); }); From bcebab8aa1553bdb590b43e6037ed311a287db1d Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 1 May 2023 09:51:50 +0330 Subject: [PATCH 04/40] feat: useTotalCurrency's test added --- .../__tests__/useTotalAccountBalance.spec.tsx | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx index 0fd25cdc7079..06fcffbbe914 100644 --- a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx @@ -5,7 +5,11 @@ import useTotalAccountBalance from '../useTotalAccountBalance'; describe('useTotalAccountBalance', () => { test('should return zero when user has no account', async () => { - const mock = mockStore({}); + const mock = mockStore({ + client: { + active_accounts: [], + }, + }); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} @@ -17,11 +21,19 @@ describe('useTotalAccountBalance', () => { test('should return total balance correctly when user has multiple accounts', async () => { const mock = mockStore({ + exchange_rates: { + data: { + rates: { + EUR: 2, + AUD: 3, + }, + }, + }, client: { active_accounts: [ { - currency: 'USD', - balance: 100, + currency: 'AUD', + balance: 300, }, { currency: 'EUR', @@ -36,6 +48,6 @@ describe('useTotalAccountBalance', () => { ); const { result } = renderHook(() => useTotalAccountBalance(mock.client.active_accounts), { wrapper }); - expect(result.current.balance).toBe(300); + expect(result.current.balance).toBe(200); }); }); From a23fe81c00ad4ce530e65c8ccb29c3e182a97c2a Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 1 May 2023 11:02:53 +0330 Subject: [PATCH 05/40] feat: test for useGetCfdAccounts added --- .../src/__tests__/useCFDAccounts.spec.tsx | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 packages/hooks/src/__tests__/useCFDAccounts.spec.tsx diff --git a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx new file mode 100644 index 000000000000..d26d478b3e38 --- /dev/null +++ b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx @@ -0,0 +1,97 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import useGetCfdAccounts from '../useCfdAccounts'; + +describe('useGetCfdAccounts', () => { + test('should return empty array when client has no cfd accounts', async () => { + const mock = mockStore({ + client: {}, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useGetCfdAccounts(), { wrapper }); + + expect(result.current.all.length).toBe(0); + expect(result.current.demo.length).toBe(0); + expect(result.current.real.length).toBe(0); + }); + + test('should return proper data when client only has cfd demo accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'demo', + balance: 1000, + currency: 'USD', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useGetCfdAccounts(), { wrapper }); + + expect(result.current.all.length).toBe(1); + expect(result.current.demo.length).toBe(1); + expect(result.current.real.length).toBe(0); + }); + + test('should return proper data when client only has cfd real accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'real', + balance: 1000, + currency: 'USD', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useGetCfdAccounts(), { wrapper }); + + expect(result.current.all.length).toBe(1); + expect(result.current.demo.length).toBe(0); + expect(result.current.real.length).toBe(1); + }); + + test('should return proper data when client only has both cfd real and demo accounts', async () => { + const mock = mockStore({ + client: { + mt5_login_list: [ + { + account_type: 'real', + balance: 1000, + currency: 'USD', + }, + ], + dxtrade_accounts_list: [ + { + account_type: 'demo', + balance: 1000, + currency: 'USD', + }, + ], + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useGetCfdAccounts(), { wrapper }); + + expect(result.current.all.length).toBe(2); + expect(result.current.demo.length).toBe(1); + expect(result.current.real.length).toBe(1); + }); +}); From 4a10f43dc7ea9cf56fe12073e4736267b9ade9ac Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 1 May 2023 19:37:06 +0330 Subject: [PATCH 06/40] fix: adding remaining tests [WIP] --- .../__tests__/usePlatformAccounts.spec.tsx | 56 ------- .../usePlatformRealAccounts.spec.tsx | 28 ---- .../__tests__/useTotalAssetCurrency.spec.tsx | 144 +++++++++++++----- packages/hooks/src/usePlatformAccounts.x.tsx | 19 +++ .../hooks/src/usePlatformRealAccounts.x.tsx | 26 ++++ packages/hooks/src/useTotalAssetCurrency.ts | 14 ++ packages/stores/src/mockStore.ts | 12 +- 7 files changed, 165 insertions(+), 134 deletions(-) delete mode 100644 packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx delete mode 100644 packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx create mode 100644 packages/hooks/src/usePlatformAccounts.x.tsx create mode 100644 packages/hooks/src/usePlatformRealAccounts.x.tsx diff --git a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx deleted file mode 100644 index 5cc212859e5d..000000000000 --- a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx +++ /dev/null @@ -1,56 +0,0 @@ -import * as React from 'react'; -import { mockStore, StoreProvider } from '@deriv/stores'; -import { renderHook } from '@testing-library/react-hooks'; -import usePlatformAccounts from '../usePlatformAccounts'; - -describe('usePlatformAccounts', () => { - test('should return default currency when user has no account', async () => { - const mock = mockStore({ - client: { - account_list: [], - }, - }); - - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); - - expect(result.current.demo).toBe(0); - }); - - // test('should return true if is_virtual is true', async () => { - // const mock = mockStore({ - // client: { - // is_virtual: true, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); - - // test('should return true account title is not Real ', async () => { - // const mock = mockStore({ - // client: { - // account_list: [ - // { - // title: 'Demo', - // is_virtual: false, - // }, - // ], - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); -}); diff --git a/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx deleted file mode 100644 index be2c762e3ed3..000000000000 --- a/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import * as React from 'react'; -import { mockStore, StoreProvider } from '@deriv/stores'; -import { renderHook } from '@testing-library/react-hooks'; -import usePlatformRealAccounts from '../usePlatformRealAccounts'; - -describe('usePlatformRealAccounts', () => { - test('should return null when user has no platform demo accounts', async () => { - const mock = mockStore({ - traders_hub: { - is_eu_user: true, - }, - client: { - active_accounts: [ - { - landing_company_shortcode: 'svg', - }, - ], - }, - }); - - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => usePlatformRealAccounts(), { wrapper }); - - expect(result.current.length).toBe(1); - }); -}); diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index 2979e8cca36e..50cb2007f241 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -4,46 +4,65 @@ import { renderHook } from '@testing-library/react-hooks'; import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; describe('useRealTotalAssetCurrency', () => { - test('should return default currency when user has no account', async () => { - const mock = mockStore({ - client: { - account_list: [], - }, - }); + // test('should return default currency when user has no account', async () => { + // const mock = mockStore({}); - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + // expect(result.current).toBe('USD'); + // }); - expect(result.current).toBe('USD'); - }); + // test('should return default currency when user has no real account', async () => { + // const mock = mockStore({ + // client: { + // accounts: { + // acc1: { + // currency: 'JPY', + // is_virtual: 1, + // }, + // }, + // }, + // }); - test('should return proper currency when user has non_crypto account', async () => { - const mock = mockStore({ - client: { - accounts: { - loginid: { - currency: 'EUR', - }, - }, - }, - }); + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + // expect(result.current).toBe('USD'); + // }); - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + // test('should return proper currency when user has non_crypto account', async () => { + // const mock = mockStore({ + // client: { + // is_crypto: false, + // accounts: { + // non_crypto_acc: { + // currency: 'AUD', + // is_virtual: 0, + // }, + // }, + // }, + // }); - expect(result.current).toBe('EUR'); - }); + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // test('should return empty string when user has not set currency for non_crypto account', async () => {} + // expect(result.current).toBe('AUD'); + // }); - // test('should return true if is_virtual is true', async () => { + // test('should return empty string when user has non_crypto account with no currency set', async () => { // const mock = mockStore({ // client: { - // is_virtual: true, + // is_crypto: false, + // accounts: { + // non_crypto_acc: { + // is_virtual: 0, + // }, + // }, // }, // }); @@ -52,18 +71,19 @@ describe('useRealTotalAssetCurrency', () => { // ); // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe(true); + // expect(result.current).toBe(''); // }); - // test('should return true account title is not Real ', async () => { + // test('should return proper currency when user only has crypto account', async () => { // const mock = mockStore({ // client: { - // account_list: [ - // { - // title: 'Demo', - // is_virtual: false, + // is_crypto: true, + // accounts: { + // crypto_acc: { + // currency: 'USDC', + // is_virtual: 0, // }, - // ], + // }, // }, // }); @@ -72,6 +92,52 @@ describe('useRealTotalAssetCurrency', () => { // ); // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe(true); + // expect(result.current).toBe('USDC'); // }); + + // test('should return empty string when user only has crypto account with no currency set', async () => { + // const mock = mockStore({ + // client: { + // is_crypto: true, + // accounts: { + // crypto_acc: { + // is_virtual: 0, + // }, + // }, + // }, + // }); + + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + // expect(result.current).toBe(''); + // }); + + test('should return proper currency when eu user only has crypto account', async () => { + const mock = mockStore({ + client: { + is_crypto: false, + is_eu: false, + accounts: { + crypto_acc: { + currency: 'BTC', + is_virtual: 0, + }, + non_crypto_acc: { + currency: 'EUR', + is_virtual: 1, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('USDC'); + }); }); diff --git a/packages/hooks/src/usePlatformAccounts.x.tsx b/packages/hooks/src/usePlatformAccounts.x.tsx new file mode 100644 index 000000000000..f51ff2043562 --- /dev/null +++ b/packages/hooks/src/usePlatformAccounts.x.tsx @@ -0,0 +1,19 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import usePlatformAccounts from './usePlatformAccounts'; + +describe('usePlatformAccounts', () => { + // test('should return default currency when user has no account', async () => { + // const mock = mockStore({ + // client: { + // account_list: [], + // }, + // }); + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); + // expect(result.current.demo).toBe(0); + // }); +}); diff --git a/packages/hooks/src/usePlatformRealAccounts.x.tsx b/packages/hooks/src/usePlatformRealAccounts.x.tsx new file mode 100644 index 000000000000..03474a625b1a --- /dev/null +++ b/packages/hooks/src/usePlatformRealAccounts.x.tsx @@ -0,0 +1,26 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import usePlatformRealAccounts from './usePlatformRealAccounts'; + +describe('usePlatformRealAccounts', () => { + // test('should return null when user has no platform demo accounts', async () => { + // const mock = mockStore({ + // traders_hub: { + // is_eu_user: true, + // }, + // client: { + // active_accounts: [ + // { + // landing_company_shortcode: 'svg', + // }, + // ], + // }, + // }); + // const wrapper = ({ children }: { children: JSX.Element }) => ( + // {children} + // ); + // const { result } = renderHook(() => usePlatformRealAccounts(), { wrapper }); + // expect(result.current.length).toBe(1); + // }); +}); diff --git a/packages/hooks/src/useTotalAssetCurrency.ts b/packages/hooks/src/useTotalAssetCurrency.ts index 82606970444d..71ba7c8a0e77 100644 --- a/packages/hooks/src/useTotalAssetCurrency.ts +++ b/packages/hooks/src/useTotalAssetCurrency.ts @@ -23,3 +23,17 @@ const useRealTotalAssetCurrency = () => { }; export default useRealTotalAssetCurrency; + +// list different scenarios for the useRealTotalAssetCurrency hook +// 1. if the user has a fiat account, return the currency of the fiat account +// 2. if the user has a crypto account, return the currency of the crypto account +// 3. if the user has both fiat and crypto accounts, return the currency of the fiat account +// 4. if the user has no accounts, return the default currency +// 5. if the user is an EU user, return the currency of the fiat account +// 6. if the user is not an EU user, return the currency of the first account in the list +// 7. if the user is not an EU user and has no accounts, return the currency of the first account in the list +// 8. if the user is not an EU user and has no accounts and is not crypto, return the currency of the first account in the list +// 9. if the user is not an EU user and has no accounts and is crypto, return the currency of the first account in the list +// 10. if the user is not an EU user and has no accounts and is crypto and has no currency, return the currency of the first account in the list +// 11. if the user is not an EU user and has no accounts and is crypto and has no currency and is not crypto, return the currency of the first account in the list +// 12. if the user is not an EU user and has no accounts and is crypto and has no currency and is crypto, return the currency of the first account in the list diff --git a/packages/stores/src/mockStore.ts b/packages/stores/src/mockStore.ts index e3e14d697c1c..98a571799670 100644 --- a/packages/stores/src/mockStore.ts +++ b/packages/stores/src/mockStore.ts @@ -5,17 +5,7 @@ const mock = (): TStores & { is_mock: boolean } => { return { is_mock: true, client: { - accounts: { - loginid: { - account_type: 'trading', - created_at: 1674633682, - currency: 'USD', - is_disabled: 0, - is_virtual: 0, - excluded_until: 0, - landing_company_name: 'svg', - }, - }, + accounts: {}, active_account_landing_company: '', account_limits: { daily_transfers: { From a03c915b2a2d7f5c9bfe00c146efa092c30dd77a Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Sun, 7 May 2023 12:20:39 +0330 Subject: [PATCH 07/40] feat: new tests added --- ...Rate.spec.tsx => useExchangeRate.spec.tsx} | 14 +-- .../usePlatformRealAccounts.spec.tsx | 93 +++++++++++++++++++ 2 files changed, 101 insertions(+), 6 deletions(-) rename packages/hooks/src/__tests__/{useCurrencyExchangeRate.spec.tsx => useExchangeRate.spec.tsx} (74%) create mode 100644 packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx diff --git a/packages/hooks/src/__tests__/useCurrencyExchangeRate.spec.tsx b/packages/hooks/src/__tests__/useExchangeRate.spec.tsx similarity index 74% rename from packages/hooks/src/__tests__/useCurrencyExchangeRate.spec.tsx rename to packages/hooks/src/__tests__/useExchangeRate.spec.tsx index 5673f1adeaef..19a574ea65eb 100644 --- a/packages/hooks/src/__tests__/useCurrencyExchangeRate.spec.tsx +++ b/packages/hooks/src/__tests__/useExchangeRate.spec.tsx @@ -1,9 +1,9 @@ import * as React from 'react'; import { mockStore, StoreProvider } from '@deriv/stores'; import { renderHook } from '@testing-library/react-hooks'; -import useCurrencyExchangeRate from '../useCurrencyExchangeRate'; +import useExchangeRate from '../useExchangeRate'; -describe('useCurrencyExchangeRate', () => { +describe('useExchangeRate', () => { test('should return 1 if currency is not found', async () => { const mock = mockStore({ exchange_rates: { @@ -21,8 +21,9 @@ describe('useCurrencyExchangeRate', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useCurrencyExchangeRate('JPY'), { wrapper }); - expect(result.current).toBe(1); + const { result } = renderHook(() => useExchangeRate(), { wrapper }); + const rate = result.current.getRate('JPY'); + expect(rate).toBe(1); }); test('should return correct rate for the given currency other than USD', async () => { @@ -40,7 +41,8 @@ describe('useCurrencyExchangeRate', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useCurrencyExchangeRate('EUR'), { wrapper }); - expect(result.current).toBe(1.3); + const { result } = renderHook(() => useExchangeRate(), { wrapper }); + const rate = result.current.getRate('EUR'); + expect(rate).toBe(1.3); }); }); diff --git a/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx new file mode 100644 index 000000000000..2388882fae81 --- /dev/null +++ b/packages/hooks/src/__tests__/usePlatformRealAccounts.spec.tsx @@ -0,0 +1,93 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import usePlatformRealAccounts from '../usePlatformRealAccounts'; + +describe('usePlatformRealAccounts', () => { + test('should return null when user has no platform real accounts', async () => { + const mock = mockStore({ + client: { + accounts: { + VR1234: { + is_virtual: 1, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => usePlatformRealAccounts(), { wrapper }); + + expect(result.current.length).toBe(0); + }); + + test('should return svg accounts when user has real account and switch to non-eu accounts', async () => { + const mock = mockStore({ + traders_hub: { + is_eu_user: false, + }, + client: { + accounts: { + CR1234: { + is_virtual: 0, + loginid: 'VR1234', + landing_company_shortcode: 'svg', + }, + MF1234: { + is_virtual: 0, + loginid: 'VR1234', + landing_company_shortcode: 'maltainvest', + }, + VR1235: { + is_virtual: 1, + loginid: 'VR1234', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => usePlatformRealAccounts(), { wrapper }); + + expect(result.current.length).toBe(1); + expect(result.current[0].landing_company_shortcode).toBe('svg'); + }); + + test('should return maltainvest accounts when user has real account and switch to eu accounts', async () => { + const mock = mockStore({ + traders_hub: { + is_eu_user: true, + }, + client: { + accounts: { + CR1234: { + is_virtual: 0, + loginid: 'VR1234', + landing_company_shortcode: 'svg', + }, + MF1234: { + is_virtual: 0, + loginid: 'VR1234', + landing_company_shortcode: 'maltainvest', + }, + VR1235: { + is_virtual: 1, + loginid: 'VR1234', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => usePlatformRealAccounts(), { wrapper }); + + expect(result.current.length).toBe(1); + expect(result.current[0].landing_company_shortcode).toBe('maltainvest'); + }); +}); From 83f2e71e6132954223de5aa12c896a5afb9cd97e Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Sun, 7 May 2023 12:41:20 +0330 Subject: [PATCH 08/40] feat: new test added --- .../__tests__/usePlatformAccounts.spec.tsx | 95 +++++++++++++++++++ .../__tests__/usePlatformDemoAccount.spec.tsx | 2 +- packages/hooks/src/usePlatformAccounts.x.tsx | 19 ---- .../hooks/src/usePlatformRealAccounts.x.tsx | 26 ----- 4 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx delete mode 100644 packages/hooks/src/usePlatformAccounts.x.tsx delete mode 100644 packages/hooks/src/usePlatformRealAccounts.x.tsx diff --git a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx new file mode 100644 index 000000000000..22af1547620f --- /dev/null +++ b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx @@ -0,0 +1,95 @@ +import * as React from 'react'; +import { mockStore, StoreProvider } from '@deriv/stores'; +import { renderHook } from '@testing-library/react-hooks'; +import usePlatformAccounts from '../usePlatformAccounts'; + +describe('usePlatformRealAccounts', () => { + test('should return proper data when user has no platform demo and real accounts', async () => { + const mock = mockStore({}); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); + + expect(result.current.demo).toBe(undefined); + expect(result.current.real.length).toBe(0); + }); + + test('should return proper data when user only has platform demo account', async () => { + const mock = mockStore({ + client: { + accounts: { + VR1234: { + is_virtual: 1, + loginid: 'VR1234', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); + + expect(result.current.demo?.loginid).toBe('VR1234'); + expect(result.current.real.length).toBe(0); + }); + + test('should return proper data when user only has platform real account', async () => { + const mock = mockStore({ + client: { + accounts: { + CR1234: { + is_virtual: 0, + loginid: 'CR1234', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); + + expect(result.current.demo?.loginid).toBe(undefined); + expect(result.current.real.length).toBe(1); + }); + + test('should return proper data when user has both real and demo accounts', async () => { + const mock = mockStore({ + traders_hub: { + is_eu_user: false, + }, + client: { + accounts: { + CR1234: { + is_virtual: 0, + loginid: 'VR1234', + landing_company_shortcode: 'svg', + }, + MF1234: { + is_virtual: 0, + loginid: 'VR1235', + landing_company_shortcode: 'maltainvest', + }, + VR1235: { + is_virtual: 1, + loginid: 'VR1236', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); + + expect(result.current.demo?.loginid).toBe('VR1236'); + expect(result.current.real.length).toBe(1); + expect(result.current.real[0].landing_company_shortcode).toBe('svg'); + }); +}); diff --git a/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx b/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx index 560ccc0b80ea..4522df569850 100644 --- a/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx +++ b/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx @@ -4,7 +4,7 @@ import { renderHook } from '@testing-library/react-hooks'; import usePlatformDemoAccount from '../usePlatformDemoAccount'; describe('usePlatformDemoAccount', () => { - test('should return null when user has no platform demo accounts', async () => { + test('should return undefined when user has no platform demo accounts', async () => { const mock = mockStore({ client: { accounts: { diff --git a/packages/hooks/src/usePlatformAccounts.x.tsx b/packages/hooks/src/usePlatformAccounts.x.tsx deleted file mode 100644 index f51ff2043562..000000000000 --- a/packages/hooks/src/usePlatformAccounts.x.tsx +++ /dev/null @@ -1,19 +0,0 @@ -import * as React from 'react'; -import { mockStore, StoreProvider } from '@deriv/stores'; -import { renderHook } from '@testing-library/react-hooks'; -import usePlatformAccounts from './usePlatformAccounts'; - -describe('usePlatformAccounts', () => { - // test('should return default currency when user has no account', async () => { - // const mock = mockStore({ - // client: { - // account_list: [], - // }, - // }); - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); - // expect(result.current.demo).toBe(0); - // }); -}); diff --git a/packages/hooks/src/usePlatformRealAccounts.x.tsx b/packages/hooks/src/usePlatformRealAccounts.x.tsx deleted file mode 100644 index 03474a625b1a..000000000000 --- a/packages/hooks/src/usePlatformRealAccounts.x.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import * as React from 'react'; -import { mockStore, StoreProvider } from '@deriv/stores'; -import { renderHook } from '@testing-library/react-hooks'; -import usePlatformRealAccounts from './usePlatformRealAccounts'; - -describe('usePlatformRealAccounts', () => { - // test('should return null when user has no platform demo accounts', async () => { - // const mock = mockStore({ - // traders_hub: { - // is_eu_user: true, - // }, - // client: { - // active_accounts: [ - // { - // landing_company_shortcode: 'svg', - // }, - // ], - // }, - // }); - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => usePlatformRealAccounts(), { wrapper }); - // expect(result.current.length).toBe(1); - // }); -}); From cc55e68974e1226bca8eb5fbbc955f3ca5457653 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Sun, 7 May 2023 13:22:30 +0330 Subject: [PATCH 09/40] fix: fixing failed tests [WIP] --- .../__tests__/useTotalAssetCurrency.spec.tsx | 93 ++++++++++--------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index 50cb2007f241..ba218102d970 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -5,18 +5,23 @@ import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; describe('useRealTotalAssetCurrency', () => { // test('should return default currency when user has no account', async () => { - // const mock = mockStore({}); + // const mock = mockStore({ + // client: { + // default_currency: 'EUR', + // }, + // }); // const wrapper = ({ children }: { children: JSX.Element }) => ( // {children} // ); // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe('USD'); + // expect(result.current).toBe('EUR'); // }); // test('should return default currency when user has no real account', async () => { // const mock = mockStore({ // client: { + // default_currency: 'EUR', // accounts: { // acc1: { // currency: 'JPY', @@ -30,16 +35,40 @@ describe('useRealTotalAssetCurrency', () => { // {children} // ); // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe('USD'); + // expect(result.current).toBe('EUR'); // }); - // test('should return proper currency when user has non_crypto account', async () => { + test('should return proper currency when user has non_crypto account', async () => { + const mock = mockStore({ + client: { + is_crypto: jest.fn(), + accounts: { + crypto_acc: { + currency: 'BTC', + is_virtual: 0, + }, + non_crypto_acc: { + currency: 'AUD', + is_virtual: 0, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('AUD'); + }); + + // test('should return empty string when user has non_crypto account with no currency set', async () => { // const mock = mockStore({ // client: { // is_crypto: false, // accounts: { // non_crypto_acc: { - // currency: 'AUD', // is_virtual: 0, // }, // }, @@ -51,15 +80,16 @@ describe('useRealTotalAssetCurrency', () => { // ); // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe('AUD'); + // expect(result.current).toBe(''); // }); - // test('should return empty string when user has non_crypto account with no currency set', async () => { + // test('should return proper currency when user only has crypto account', async () => { // const mock = mockStore({ // client: { - // is_crypto: false, + // is_crypto: true, // accounts: { - // non_crypto_acc: { + // crypto_acc: { + // currency: 'USDC', // is_virtual: 0, // }, // }, @@ -71,16 +101,15 @@ describe('useRealTotalAssetCurrency', () => { // ); // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe(''); + // expect(result.current).toBe('USDC'); // }); - // test('should return proper currency when user only has crypto account', async () => { + // test('should return empty string when user only has crypto account with no currency set', async () => { // const mock = mockStore({ // client: { // is_crypto: true, // accounts: { // crypto_acc: { - // currency: 'USDC', // is_virtual: 0, // }, // }, @@ -92,17 +121,23 @@ describe('useRealTotalAssetCurrency', () => { // ); // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe('USDC'); + // expect(result.current).toBe(''); // }); - // test('should return empty string when user only has crypto account with no currency set', async () => { + // test('should return proper currency when eu user only has crypto account', async () => { // const mock = mockStore({ // client: { - // is_crypto: true, + // is_crypto: false, + // is_eu: false, // accounts: { // crypto_acc: { + // currency: 'BTC', // is_virtual: 0, // }, + // non_crypto_acc: { + // currency: 'EUR', + // is_virtual: 1, + // }, // }, // }, // }); @@ -112,32 +147,6 @@ describe('useRealTotalAssetCurrency', () => { // ); // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe(''); + // expect(result.current).toBe('USDC'); // }); - - test('should return proper currency when eu user only has crypto account', async () => { - const mock = mockStore({ - client: { - is_crypto: false, - is_eu: false, - accounts: { - crypto_acc: { - currency: 'BTC', - is_virtual: 0, - }, - non_crypto_acc: { - currency: 'EUR', - is_virtual: 1, - }, - }, - }, - }); - - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - expect(result.current).toBe('USDC'); - }); }); From 0655e7523345db660e5bfe39c41711c340aa7e2c Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 8 May 2023 00:56:40 +0330 Subject: [PATCH 10/40] feat: all tsts added --- .../__tests__/usePlatformAccounts.spec.tsx | 3 - .../__tests__/useTotalAssetCurrency.spec.tsx | 330 +++++++++++------- packages/hooks/src/useTotalAssetCurrency.ts | 14 - 3 files changed, 210 insertions(+), 137 deletions(-) diff --git a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx index 22af1547620f..ef2b3782fe13 100644 --- a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx @@ -60,9 +60,6 @@ describe('usePlatformRealAccounts', () => { test('should return proper data when user has both real and demo accounts', async () => { const mock = mockStore({ - traders_hub: { - is_eu_user: false, - }, client: { accounts: { CR1234: { diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index ba218102d970..211bc280c341 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -4,44 +4,44 @@ import { renderHook } from '@testing-library/react-hooks'; import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; describe('useRealTotalAssetCurrency', () => { - // test('should return default currency when user has no account', async () => { - // const mock = mockStore({ - // client: { - // default_currency: 'EUR', - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe('EUR'); - // }); - - // test('should return default currency when user has no real account', async () => { - // const mock = mockStore({ - // client: { - // default_currency: 'EUR', - // accounts: { - // acc1: { - // currency: 'JPY', - // is_virtual: 1, - // }, - // }, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - // expect(result.current).toBe('EUR'); - // }); + test('should return default currency when user has no account', async () => { + const mock = mockStore({ + client: { + default_currency: 'EUR', + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + expect(result.current).toBe('EUR'); + }); + + test('should return default currency when user has no real account', async () => { + const mock = mockStore({ + client: { + default_currency: 'EUR', + accounts: { + acc1: { + currency: 'JPY', + is_virtual: 1, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + expect(result.current).toBe('EUR'); + }); test('should return proper currency when user has non_crypto account', async () => { const mock = mockStore({ client: { - is_crypto: jest.fn(), + is_crypto: (currency: string) => currency === 'BTC', accounts: { crypto_acc: { currency: 'BTC', @@ -63,90 +63,180 @@ describe('useRealTotalAssetCurrency', () => { expect(result.current).toBe('AUD'); }); - // test('should return empty string when user has non_crypto account with no currency set', async () => { - // const mock = mockStore({ - // client: { - // is_crypto: false, - // accounts: { - // non_crypto_acc: { - // is_virtual: 0, - // }, - // }, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(''); - // }); - - // test('should return proper currency when user only has crypto account', async () => { - // const mock = mockStore({ - // client: { - // is_crypto: true, - // accounts: { - // crypto_acc: { - // currency: 'USDC', - // is_virtual: 0, - // }, - // }, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe('USDC'); - // }); - - // test('should return empty string when user only has crypto account with no currency set', async () => { - // const mock = mockStore({ - // client: { - // is_crypto: true, - // accounts: { - // crypto_acc: { - // is_virtual: 0, - // }, - // }, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(''); - // }); - - // test('should return proper currency when eu user only has crypto account', async () => { - // const mock = mockStore({ - // client: { - // is_crypto: false, - // is_eu: false, - // accounts: { - // crypto_acc: { - // currency: 'BTC', - // is_virtual: 0, - // }, - // non_crypto_acc: { - // currency: 'EUR', - // is_virtual: 1, - // }, - // }, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe('USDC'); - // }); + test('should return empty string when user has non_crypto account with no currency set', async () => { + const mock = mockStore({ + client: { + is_crypto: (currency: string) => currency === 'BTC', + accounts: { + crypto_acc: { + currency: 'BTC', + is_virtual: 0, + }, + non_crypto_acc: { + is_virtual: 0, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe(''); + }); + + test("should return the first account's currency when user only has crypto account", async () => { + const mock = mockStore({ + traders_hub: { + is_eu_user: false, + }, + client: { + is_crypto: (currency: string) => ['BTC', 'ETH'].includes(currency) || true, + accounts: { + eth_acc: { + currency: 'ETH', + is_virtual: 0, + }, + btc_acc: { + currency: 'BTC', + is_virtual: 0, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('ETH'); + }); + + test('should return the current selected currency when user only has crypto account and is_crypto() is false', async () => { + const mock = mockStore({ + traders_hub: { + is_eu_user: false, + }, + client: { + is_crypto: (currency: string) => ['BTC', 'ETH'].includes(currency) || false, + currency: 'USDC', + accounts: { + eth_acc: { + currency: 'ETH', + is_virtual: 0, + }, + usdc_acc: { + currency: 'USDC', + is_virtual: 0, + }, + btc_acc: { + currency: 'BTC', + is_virtual: 0, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('USDC'); + }); + + test('should return undefined when user only has crypto account with no currency set', async () => { + const mock = mockStore({ + client: { + is_crypto: (currency: string) => ['BTC', 'ETH'].includes(currency) || true, + accounts: { + crypto_acc: { + is_virtual: 0, + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe(undefined); + }); + + test("should return MF account's currency when user switch to eu account type", async () => { + const mock = mockStore({ + traders_hub: { + is_eu_user: true, + }, + client: { + current_fiat_currency: 'EUR', + is_crypto: (currency: string) => ['BTC', 'ETH'].includes(currency) || true, + accounts: { + btc_acc: { + currency: 'BTC', + is_virtual: 0, + landing_company_shortcode: 'svg', + }, + eth_acc: { + currency: 'ETH', + is_virtual: 0, + landing_company_shortcode: 'svg', + }, + MF1234: { + currency: 'EUR', + is_virtual: 0, + landing_company_shortcode: 'maltainvest', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('EUR'); + }); + + test("should return default currency when user switch to eu account type but MF account's currency is not set", async () => { + const mock = mockStore({ + traders_hub: { + is_eu_user: true, + }, + client: { + default_currency: 'GBP', + is_crypto: (currency: string) => ['BTC', 'ETH'].includes(currency) || true, + accounts: { + btc_acc: { + currency: 'BTC', + is_virtual: 0, + landing_company_shortcode: 'svg', + }, + eth_acc: { + currency: 'ETH', + is_virtual: 0, + landing_company_shortcode: 'svg', + }, + MF1234: { + currency: 'EUR', + is_virtual: 0, + landing_company_shortcode: 'maltainvest', + }, + }, + }, + }); + + const wrapper = ({ children }: { children: JSX.Element }) => ( + {children} + ); + const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); + + expect(result.current).toBe('GBP'); + }); }); diff --git a/packages/hooks/src/useTotalAssetCurrency.ts b/packages/hooks/src/useTotalAssetCurrency.ts index d3beda1f6593..55862934023d 100644 --- a/packages/hooks/src/useTotalAssetCurrency.ts +++ b/packages/hooks/src/useTotalAssetCurrency.ts @@ -21,17 +21,3 @@ const useRealTotalAssetCurrency = () => { }; export default useRealTotalAssetCurrency; - -// list different scenarios for the useRealTotalAssetCurrency hook -// 1. if the user has a fiat account, return the currency of the fiat account -// 2. if the user has a crypto account, return the currency of the crypto account -// 3. if the user has both fiat and crypto accounts, return the currency of the fiat account -// 4. if the user has no accounts, return the default currency -// 5. if the user is an EU user, return the currency of the fiat account -// 6. if the user is not an EU user, return the currency of the first account in the list -// 7. if the user is not an EU user and has no accounts, return the currency of the first account in the list -// 8. if the user is not an EU user and has no accounts and is not crypto, return the currency of the first account in the list -// 9. if the user is not an EU user and has no accounts and is crypto, return the currency of the first account in the list -// 10. if the user is not an EU user and has no accounts and is crypto and has no currency, return the currency of the first account in the list -// 11. if the user is not an EU user and has no accounts and is crypto and has no currency and is not crypto, return the currency of the first account in the list -// 12. if the user is not an EU user and has no accounts and is crypto and has no currency and is crypto, return the currency of the first account in the list From 58b8a658f577f49b90112f7f937183e75a82792a Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 10:21:51 +0330 Subject: [PATCH 11/40] Update packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx b/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx index 4522df569850..680f32547cd7 100644 --- a/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx +++ b/packages/hooks/src/__tests__/usePlatformDemoAccount.spec.tsx @@ -40,6 +40,6 @@ describe('usePlatformDemoAccount', () => { ); const { result } = renderHook(() => usePlatformDemoAccount(), { wrapper }); - expect(result.current?.loginid).toBe('VR1234'); + expect(result.current?.loginid).toBe(mock.client.accounts.VR1234.loginid); }); }); From b21bb6aec97f8fead0a026b2cf5e621fa793f1c8 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 10:22:16 +0330 Subject: [PATCH 12/40] Update packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx index ef2b3782fe13..0cdd21b2871b 100644 --- a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx @@ -33,7 +33,7 @@ describe('usePlatformRealAccounts', () => { ); const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); - expect(result.current.demo?.loginid).toBe('VR1234'); + expect(result.current.demo?.loginid).toBe(mock.client.accounts.VR1234.loginid); expect(result.current.real.length).toBe(0); }); From f3fb37e9db47b35590975497f65e2fae6ac4658e Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 10:22:44 +0330 Subject: [PATCH 13/40] Update packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx index 0cdd21b2871b..d46a4cc10bac 100644 --- a/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/usePlatformAccounts.spec.tsx @@ -85,7 +85,7 @@ describe('usePlatformRealAccounts', () => { ); const { result } = renderHook(() => usePlatformAccounts(), { wrapper }); - expect(result.current.demo?.loginid).toBe('VR1236'); + expect(result.current.demo?.loginid).toBe(mock.client.accounts.VR1235.loginid); expect(result.current.real.length).toBe(1); expect(result.current.real[0].landing_company_shortcode).toBe('svg'); }); From 804a20bae832a92cb6289cd38037021842def6a3 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 10:23:07 +0330 Subject: [PATCH 14/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index 211bc280c341..9c7af2723496 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -15,7 +15,7 @@ describe('useRealTotalAssetCurrency', () => { {children} ); const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - expect(result.current).toBe('EUR'); + expect(result.current).toBe(mock.client.default_currency); }); test('should return default currency when user has no real account', async () => { From 19c30ede786bd2120fdb41660b79fd924b4cc1d2 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 10:23:25 +0330 Subject: [PATCH 15/40] Update packages/hooks/src/__tests__/useCFDAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDAccounts.spec.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx index d26d478b3e38..d3fe052e867e 100644 --- a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx @@ -4,10 +4,8 @@ import { renderHook } from '@testing-library/react-hooks'; import useGetCfdAccounts from '../useCfdAccounts'; describe('useGetCfdAccounts', () => { - test('should return empty array when client has no cfd accounts', async () => { - const mock = mockStore({ - client: {}, - }); + test('should return empty array when client has no CFD accounts', async () => { + const mock = mockStore({}); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} From 07fdd7a179a146b59b850a281f6c613123eabe38 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 10:23:34 +0330 Subject: [PATCH 16/40] Update packages/hooks/src/__tests__/useCFDAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx index d3fe052e867e..e40fabb2e4c3 100644 --- a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx @@ -17,7 +17,7 @@ describe('useGetCfdAccounts', () => { expect(result.current.real.length).toBe(0); }); - test('should return proper data when client only has cfd demo accounts', async () => { + test('should return proper data when client only has CFD demo accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From 28375617d9dc1c8c43e75adc2dad98afd99be9a1 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 10:23:41 +0330 Subject: [PATCH 17/40] Update packages/hooks/src/__tests__/useCFDAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx index e40fabb2e4c3..7d4be408732a 100644 --- a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx @@ -40,7 +40,7 @@ describe('useGetCfdAccounts', () => { expect(result.current.real.length).toBe(0); }); - test('should return proper data when client only has cfd real accounts', async () => { + test('should return proper data when client only has CFD real accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From 9c094a5ec1517b663e529d89872d219be23d86e5 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 10:27:18 +0330 Subject: [PATCH 18/40] Update packages/hooks/src/__tests__/useCFDAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx index 7d4be408732a..dec22d850b1f 100644 --- a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx @@ -63,7 +63,7 @@ describe('useGetCfdAccounts', () => { expect(result.current.real.length).toBe(1); }); - test('should return proper data when client only has both cfd real and demo accounts', async () => { + test('should return proper data when client only has both CFD real and demo accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From e6dea3f69e95e2804591ca47fe5ea80aae62b8c0 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 8 May 2023 10:29:52 +0330 Subject: [PATCH 19/40] fix: resolved pr comments --- .../src/__tests__/useGetCfdAccounts.spec.tsx | 77 ------------------- packages/stores/types.ts | 2 +- 2 files changed, 1 insertion(+), 78 deletions(-) delete mode 100644 packages/hooks/src/__tests__/useGetCfdAccounts.spec.tsx diff --git a/packages/hooks/src/__tests__/useGetCfdAccounts.spec.tsx b/packages/hooks/src/__tests__/useGetCfdAccounts.spec.tsx deleted file mode 100644 index 2979e8cca36e..000000000000 --- a/packages/hooks/src/__tests__/useGetCfdAccounts.spec.tsx +++ /dev/null @@ -1,77 +0,0 @@ -import * as React from 'react'; -import { mockStore, StoreProvider } from '@deriv/stores'; -import { renderHook } from '@testing-library/react-hooks'; -import useRealTotalAssetCurrency from '../useTotalAssetCurrency'; - -describe('useRealTotalAssetCurrency', () => { - test('should return default currency when user has no account', async () => { - const mock = mockStore({ - client: { - account_list: [], - }, - }); - - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - expect(result.current).toBe('USD'); - }); - - test('should return proper currency when user has non_crypto account', async () => { - const mock = mockStore({ - client: { - accounts: { - loginid: { - currency: 'EUR', - }, - }, - }, - }); - - const wrapper = ({ children }: { children: JSX.Element }) => ( - {children} - ); - const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - expect(result.current).toBe('EUR'); - }); - - // test('should return empty string when user has not set currency for non_crypto account', async () => {} - - // test('should return true if is_virtual is true', async () => { - // const mock = mockStore({ - // client: { - // is_virtual: true, - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); - - // test('should return true account title is not Real ', async () => { - // const mock = mockStore({ - // client: { - // account_list: [ - // { - // title: 'Demo', - // is_virtual: false, - // }, - // ], - // }, - // }); - - // const wrapper = ({ children }: { children: JSX.Element }) => ( - // {children} - // ); - // const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - - // expect(result.current).toBe(true); - // }); -}); diff --git a/packages/stores/types.ts b/packages/stores/types.ts index e114d70cce3c..76fbce289412 100644 --- a/packages/stores/types.ts +++ b/packages/stores/types.ts @@ -90,7 +90,7 @@ type TNotification = type TAccountStatus = Omit & Partial>; type TClientStore = { - accounts: { [k: string]: TAccount }; + accounts: { [k: string]: TActiveAccount }; active_accounts: TActiveAccount[]; active_account_landing_company: string; account_limits: { From 834cbd2a2c66e91aafe30f19b2fa77b13f400efb Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 11:46:36 +0330 Subject: [PATCH 20/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index 9c7af2723496..85527dcb071b 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -35,7 +35,7 @@ describe('useRealTotalAssetCurrency', () => { {children} ); const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - expect(result.current).toBe('EUR'); + expect(result.current).toBe(mock.client.default_currency); }); test('should return proper currency when user has non_crypto account', async () => { From 4dd73d54aeb871a16e7319aed53cc993d896d135 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 11:59:51 +0330 Subject: [PATCH 21/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index 85527dcb071b..6eb9c4a5e7c9 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -145,7 +145,7 @@ describe('useRealTotalAssetCurrency', () => { ); const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - expect(result.current).toBe('USDC'); + expect(result.current).toBe(mock.client.currency); }); test('should return undefined when user only has crypto account with no currency set', async () => { From 5416f1f44a3a2646eb6772e1eb582e5263f21902 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:00:05 +0330 Subject: [PATCH 22/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index 6eb9c4a5e7c9..e7ee8afa863a 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -60,7 +60,7 @@ describe('useRealTotalAssetCurrency', () => { ); const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - expect(result.current).toBe('AUD'); + expect(result.current).toBe(mock.client.accounts.non_crypto_acc.currency); }); test('should return empty string when user has non_crypto account with no currency set', async () => { From fe51f34c26b23dc431b9e42ef2e08582e24aa287 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:00:24 +0330 Subject: [PATCH 23/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index e7ee8afa863a..d69d2f2827e9 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -112,7 +112,8 @@ describe('useRealTotalAssetCurrency', () => { ); const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - expect(result.current).toBe('ETH'); + const first_account_currency = mock.client.accounts[Object.keys(mock.client.accounts)[0]].currency; + expect(result.current).toBe(first_account_currency); }); test('should return the current selected currency when user only has crypto account and is_crypto() is false', async () => { From c5bdd0cbf481cc780848f894118b83cf41f502cf Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:01:01 +0330 Subject: [PATCH 24/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index d69d2f2827e9..b7897e465a7a 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -118,9 +118,6 @@ describe('useRealTotalAssetCurrency', () => { test('should return the current selected currency when user only has crypto account and is_crypto() is false', async () => { const mock = mockStore({ - traders_hub: { - is_eu_user: false, - }, client: { is_crypto: (currency: string) => ['BTC', 'ETH'].includes(currency) || false, currency: 'USDC', From 0030a6310e89fbe85bcb65cdd8792e0c62806b59 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:01:15 +0330 Subject: [PATCH 25/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index b7897e465a7a..c14452e309b0 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -89,9 +89,6 @@ describe('useRealTotalAssetCurrency', () => { test("should return the first account's currency when user only has crypto account", async () => { const mock = mockStore({ - traders_hub: { - is_eu_user: false, - }, client: { is_crypto: (currency: string) => ['BTC', 'ETH'].includes(currency) || true, accounts: { From 4e421d289bb5ea6730480e5e53f01590f998374d Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:01:30 +0330 Subject: [PATCH 26/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index c14452e309b0..77d29fb20c10 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -196,7 +196,7 @@ describe('useRealTotalAssetCurrency', () => { ); const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - expect(result.current).toBe('EUR'); + expect(result.current).toBe(mock.client.accounts.MF1234.currency); }); test("should return default currency when user switch to eu account type but MF account's currency is not set", async () => { From 52aeae54c2c540eabb42bd559b10464c2a79f938 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:02:46 +0330 Subject: [PATCH 27/40] Update packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx index 77d29fb20c10..1d2ddaf4c034 100644 --- a/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAssetCurrency.spec.tsx @@ -232,6 +232,6 @@ describe('useRealTotalAssetCurrency', () => { ); const { result } = renderHook(() => useRealTotalAssetCurrency(), { wrapper }); - expect(result.current).toBe('GBP'); + expect(result.current).toBe(mock.client.default_currency); }); }); From 5cf0016fe5913a4fdba4a836bc9d8bb6c8bd94ba Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:03:05 +0330 Subject: [PATCH 28/40] Update packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx index acd6f13852cf..391b0ca21571 100644 --- a/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx @@ -39,7 +39,7 @@ describe('useCFDRealAccounts', () => { expect(result.current?.length).toBe(0); }); - test('should return proper data when user has cfd real accounts', async () => { + test('should return proper data when user has CFD real accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From b99b328039246a046fb111558c1f353ffba3790f Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:03:45 +0330 Subject: [PATCH 29/40] Update packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- .../hooks/src/__tests__/useTotalAccountBalance.spec.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx index 06fcffbbe914..943a2d1e1a47 100644 --- a/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx +++ b/packages/hooks/src/__tests__/useTotalAccountBalance.spec.tsx @@ -5,11 +5,7 @@ import useTotalAccountBalance from '../useTotalAccountBalance'; describe('useTotalAccountBalance', () => { test('should return zero when user has no account', async () => { - const mock = mockStore({ - client: { - active_accounts: [], - }, - }); + const mock = mockStore({}); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} From 0a1eaaedcfc908eb46f2f8874d483fbb05ca7496 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:04:27 +0330 Subject: [PATCH 30/40] Update packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx index 8ecc10107c25..a3f5588dd631 100644 --- a/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx @@ -4,10 +4,8 @@ import { renderHook } from '@testing-library/react-hooks'; import useCFDAllAccounts from '../useCFDAllAccounts'; describe('useCFDAllAccounts', () => { - test('should return empty array when client has no cfd accounts', async () => { - const mock = mockStore({ - client: {}, - }); + test('should return empty array when client has no CFD accounts', async () => { + const mock = mockStore({}); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} From 8fe111efa19d65f5414d12f73d8ea572ee054c38 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:04:43 +0330 Subject: [PATCH 31/40] Update packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx index a3f5588dd631..4f7a44128c8c 100644 --- a/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx @@ -15,7 +15,7 @@ describe('useCFDAllAccounts', () => { expect(result.current.length).toBe(0); }); - test('should return proper data when client has mt5 accounts', async () => { + test('should return proper data when client has MT5 accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From eb1b1f612bdaa2bf064512cb2830b080dede574f Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:05:05 +0330 Subject: [PATCH 32/40] Update packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx index 4f7a44128c8c..74a602c08adb 100644 --- a/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDAllAccounts.spec.tsx @@ -57,7 +57,7 @@ describe('useCFDAllAccounts', () => { expect(result.current.length).toBe(1); }); - test('should return proper data when client has both mt5 and dxtrade accounts', async () => { + test('should return proper data when client has both MT5 and dxtrade accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From 1df7e8a2bb3f95ac48de66193ffd9feed489eaa1 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:05:27 +0330 Subject: [PATCH 33/40] Update packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx index 6f4d0aae0d9f..91453c23f0e7 100644 --- a/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx @@ -4,7 +4,7 @@ import { renderHook } from '@testing-library/react-hooks'; import useCFDDemoAccounts from '../useCFDDemoAccounts'; describe('useCFDDemoAccounts', () => { - test('should return empty array when user has no cfd accounts', async () => { + test('should return empty array when user has no CFD accounts', async () => { const mock = mockStore({}); const wrapper = ({ children }: { children: JSX.Element }) => ( From e230aa05c1bbb99989e6e7e2a6094700ec38ff43 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:05:41 +0330 Subject: [PATCH 34/40] Update packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx index 91453c23f0e7..5bb40d561fe5 100644 --- a/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx @@ -15,7 +15,7 @@ describe('useCFDDemoAccounts', () => { expect(result.current?.length).toBe(0); }); - test('should return empty array when user has no cfd demo accounts', async () => { + test('should return empty array when user has no CFD demo accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From 65250c04cfb01538467dd14823e2000ca9321690 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:06:08 +0330 Subject: [PATCH 35/40] Update packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx index 5bb40d561fe5..d2015000c2bb 100644 --- a/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDDemoAccounts.spec.tsx @@ -39,7 +39,7 @@ describe('useCFDDemoAccounts', () => { expect(result.current?.length).toBe(0); }); - test('should return proper data when user has cfd demo accounts', async () => { + test('should return proper data when user has CFD demo accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From 93c7e44628ed45d5c1ad05eac5e4a3c244ca7912 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:06:20 +0330 Subject: [PATCH 36/40] Update packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx index 391b0ca21571..ed4a9493ab95 100644 --- a/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx @@ -4,7 +4,7 @@ import { renderHook } from '@testing-library/react-hooks'; import useCFDRealAccounts from '../useCFDRealAccounts'; describe('useCFDRealAccounts', () => { - test('should return empty array when user has no cfd accounts', async () => { + test('should return empty array when user has no CFD accounts', async () => { const mock = mockStore({}); const wrapper = ({ children }: { children: JSX.Element }) => ( From f2d01cedf85fb0fe97cc83bff145754153d7b4a2 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast <100833613+shayan-deriv@users.noreply.github.com> Date: Mon, 8 May 2023 12:06:40 +0330 Subject: [PATCH 37/40] Update packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx Co-authored-by: Farzin Mirzaie <72082844+farzin-deriv@users.noreply.github.com> --- packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx index ed4a9493ab95..c796f02d09fe 100644 --- a/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDRealAccounts.spec.tsx @@ -15,7 +15,7 @@ describe('useCFDRealAccounts', () => { expect(result.current?.length).toBe(0); }); - test('should return empty array when user has no cfd real accounts', async () => { + test('should return empty array when user has no CFD real accounts', async () => { const mock = mockStore({ client: { mt5_login_list: [ From 7721872572363e3f0c1be88284b1de60da124bb3 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 8 May 2023 12:53:28 +0330 Subject: [PATCH 38/40] fix: fixed ts error in useTotalAccountBalance hook --- packages/hooks/src/useTotalAccountBalance.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hooks/src/useTotalAccountBalance.ts b/packages/hooks/src/useTotalAccountBalance.ts index 741f4e83c600..5ab91f88ecaf 100644 --- a/packages/hooks/src/useTotalAccountBalance.ts +++ b/packages/hooks/src/useTotalAccountBalance.ts @@ -15,8 +15,8 @@ const useTotalAccountBalance = (accounts: { balance?: number; currency?: string 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 rate = getRate(account.currency || total_assets_real_currency); + const base_rate = getRate(total_assets_real_currency || ''); + const rate = getRate(account.currency || total_assets_real_currency || ''); const exchange_rate = base_rate / rate; return total + (account.balance || 0) * exchange_rate; From 64c13c1ad3b472c0e7e66561723ec1ad02bb0d81 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 8 May 2023 13:34:43 +0330 Subject: [PATCH 39/40] fix: resolved pr comment --- .../src/components/main-title-bar/asset-summary.tsx | 6 +++--- packages/hooks/src/__tests__/useCFDAccounts.spec.tsx | 12 ++++++------ packages/hooks/src/index.ts | 2 +- .../src/{useCfdAccounts.ts => useCFDAccounts.ts} | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) rename packages/hooks/src/{useCfdAccounts.ts => useCFDAccounts.ts} (89%) diff --git a/packages/appstore/src/components/main-title-bar/asset-summary.tsx b/packages/appstore/src/components/main-title-bar/asset-summary.tsx index 32cce8d3c4dd..ca0954b074eb 100644 --- a/packages/appstore/src/components/main-title-bar/asset-summary.tsx +++ b/packages/appstore/src/components/main-title-bar/asset-summary.tsx @@ -6,14 +6,14 @@ import BalanceText from 'Components/elements/text/balance-text'; import { observer, useStore } from '@deriv/stores'; import './asset-summary.scss'; import TotalAssetsLoader from 'Components/pre-loader/total-assets-loader'; -import { useTotalAccountBalance, useCfdAccounts, usePlatformAccounts } from '@deriv/hooks'; +import { useTotalAccountBalance, useCFDAccounts, usePlatformAccounts } from '@deriv/hooks'; const AssetSummary = observer(() => { const { traders_hub, client } = useStore(); const { selected_account_type, is_eu_user, no_CR_account, no_MF_account } = traders_hub; const { is_logging_in, is_switching, default_currency } = client; const { real: platform_real_accounts, demo: platform_demo_account } = usePlatformAccounts(); - const { real: cfd_real_accounts, demo: cfd_demo_accounts } = useCfdAccounts(); + const { real: cfd_real_accounts, demo: cfd_demo_accounts } = useCFDAccounts(); const platform_real_balance = useTotalAccountBalance(platform_real_accounts); const cfd_real_balance = useTotalAccountBalance(cfd_real_accounts); const cfd_demo_balance = useTotalAccountBalance(cfd_demo_accounts); @@ -54,7 +54,7 @@ const AssetSummary = observer(() => { { +describe('useGetCFDAccounts', () => { test('should return empty array when client has no CFD accounts', async () => { const mock = mockStore({}); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useGetCfdAccounts(), { wrapper }); + const { result } = renderHook(() => useGetCFDAccounts(), { wrapper }); expect(result.current.all.length).toBe(0); expect(result.current.demo.length).toBe(0); @@ -33,7 +33,7 @@ describe('useGetCfdAccounts', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useGetCfdAccounts(), { wrapper }); + const { result } = renderHook(() => useGetCFDAccounts(), { wrapper }); expect(result.current.all.length).toBe(1); expect(result.current.demo.length).toBe(1); @@ -56,7 +56,7 @@ describe('useGetCfdAccounts', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useGetCfdAccounts(), { wrapper }); + const { result } = renderHook(() => useGetCFDAccounts(), { wrapper }); expect(result.current.all.length).toBe(1); expect(result.current.demo.length).toBe(0); @@ -86,7 +86,7 @@ describe('useGetCfdAccounts', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useGetCfdAccounts(), { wrapper }); + const { result } = renderHook(() => useGetCFDAccounts(), { wrapper }); expect(result.current.all.length).toBe(2); expect(result.current.demo.length).toBe(1); diff --git a/packages/hooks/src/index.ts b/packages/hooks/src/index.ts index 245cd16c5a0b..f4555a7a9cfe 100644 --- a/packages/hooks/src/index.ts +++ b/packages/hooks/src/index.ts @@ -1,9 +1,9 @@ export { default as useAccountTransferVisible } from './useAccountTransferVisible'; +export { default as useCFDAccounts } from './useCFDAccounts'; export { default as useCFDAllAccounts } from './useCFDAllAccounts'; export { default as useCFDDemoAccounts } from './useCFDDemoAccounts'; export { default as useCFDRealAccounts } from './useCFDRealAccounts'; export { default as useCashierLocked } from './useCashierLocked'; -export { default as useCfdAccounts } from './useCfdAccounts'; export { default as useCountdown } from './useCountdown'; export { default as useDepositLocked } from './useDepositLocked'; export { default as useExchangeRate } from './useExchangeRate'; diff --git a/packages/hooks/src/useCfdAccounts.ts b/packages/hooks/src/useCFDAccounts.ts similarity index 89% rename from packages/hooks/src/useCfdAccounts.ts rename to packages/hooks/src/useCFDAccounts.ts index 8641de8ff8dd..62971ec26b1e 100644 --- a/packages/hooks/src/useCfdAccounts.ts +++ b/packages/hooks/src/useCFDAccounts.ts @@ -7,7 +7,7 @@ import useCFDRealAccounts from './useCFDRealAccounts'; * and it returns different cfd account types which are demo, real, and all */ -const useGetCfdAccounts = () => { +const useGetCFDAccounts = () => { const all_cfd_accounts = useCFDAccounts(); const cfd_demo_accounts = useCFDDemoAccounts(); const cfd_real_accounts = useCFDRealAccounts(); @@ -19,4 +19,4 @@ const useGetCfdAccounts = () => { }; }; -export default useGetCfdAccounts; +export default useGetCFDAccounts; From fb525814ee7e9b48002a99b4486ce39da2256370 Mon Sep 17 00:00:00 2001 From: Shayan Khaleghparast Date: Mon, 8 May 2023 13:57:34 +0330 Subject: [PATCH 40/40] chore: renamed hook --- packages/hooks/src/__tests__/useCFDAccounts.spec.tsx | 12 ++++++------ packages/hooks/src/useCFDAccounts.ts | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx index 08067c8ab824..840740e75702 100644 --- a/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx +++ b/packages/hooks/src/__tests__/useCFDAccounts.spec.tsx @@ -1,16 +1,16 @@ import * as React from 'react'; import { mockStore, StoreProvider } from '@deriv/stores'; import { renderHook } from '@testing-library/react-hooks'; -import useGetCFDAccounts from '../useCFDAccounts'; +import useCFDAccounts from '../useCFDAccounts'; -describe('useGetCFDAccounts', () => { +describe('useCFDAccounts', () => { test('should return empty array when client has no CFD accounts', async () => { const mock = mockStore({}); const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useGetCFDAccounts(), { wrapper }); + const { result } = renderHook(() => useCFDAccounts(), { wrapper }); expect(result.current.all.length).toBe(0); expect(result.current.demo.length).toBe(0); @@ -33,7 +33,7 @@ describe('useGetCFDAccounts', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useGetCFDAccounts(), { wrapper }); + const { result } = renderHook(() => useCFDAccounts(), { wrapper }); expect(result.current.all.length).toBe(1); expect(result.current.demo.length).toBe(1); @@ -56,7 +56,7 @@ describe('useGetCFDAccounts', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useGetCFDAccounts(), { wrapper }); + const { result } = renderHook(() => useCFDAccounts(), { wrapper }); expect(result.current.all.length).toBe(1); expect(result.current.demo.length).toBe(0); @@ -86,7 +86,7 @@ describe('useGetCFDAccounts', () => { const wrapper = ({ children }: { children: JSX.Element }) => ( {children} ); - const { result } = renderHook(() => useGetCFDAccounts(), { wrapper }); + const { result } = renderHook(() => useCFDAccounts(), { wrapper }); expect(result.current.all.length).toBe(2); expect(result.current.demo.length).toBe(1); diff --git a/packages/hooks/src/useCFDAccounts.ts b/packages/hooks/src/useCFDAccounts.ts index 62971ec26b1e..5da45e4d102d 100644 --- a/packages/hooks/src/useCFDAccounts.ts +++ b/packages/hooks/src/useCFDAccounts.ts @@ -1,4 +1,4 @@ -import useCFDAccounts from './useCFDAllAccounts'; +import useCFDAllAccounts from './useCFDAllAccounts'; import useCFDDemoAccounts from './useCFDDemoAccounts'; import useCFDRealAccounts from './useCFDRealAccounts'; @@ -7,8 +7,8 @@ import useCFDRealAccounts from './useCFDRealAccounts'; * and it returns different cfd account types which are demo, real, and all */ -const useGetCFDAccounts = () => { - const all_cfd_accounts = useCFDAccounts(); +const useCFDAccounts = () => { + const all_cfd_accounts = useCFDAllAccounts(); const cfd_demo_accounts = useCFDDemoAccounts(); const cfd_real_accounts = useCFDRealAccounts(); @@ -19,4 +19,4 @@ const useGetCFDAccounts = () => { }; }; -export default useGetCFDAccounts; +export default useCFDAccounts;