|
| 1 | +import { api } from '@/hooks'; |
| 2 | +import { useAccountList, useAuthData } from '@deriv-com/api-hooks'; |
| 3 | +import { renderHook } from '@testing-library/react'; |
| 4 | +import useActiveAccount from '../useActiveAccount'; |
| 5 | + |
| 6 | +jest.mock('@deriv-com/api-hooks', () => ({ |
| 7 | + useAccountList: jest.fn().mockReturnValue({ data: [] }), |
| 8 | + useAuthData: jest.fn().mockReturnValue({ activeLoginid: '' }), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('@/hooks', () => ({ |
| 12 | + api: { |
| 13 | + account: { |
| 14 | + useBalance: jest.fn().mockReturnValue({ data: {} }), |
| 15 | + }, |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +const mockAccountList = [ |
| 20 | + { |
| 21 | + account_category: 'trading', |
| 22 | + account_type: 'type', |
| 23 | + broker: 'CR', |
| 24 | + created_at: 1718953961, |
| 25 | + currency: 'USD', |
| 26 | + currency_type: 'fiat', |
| 27 | + is_disabled: 0, |
| 28 | + is_virtual: 0, |
| 29 | + landing_company_name: 'name', |
| 30 | + linked_to: [], |
| 31 | + loginid: 'CR00001', |
| 32 | + }, |
| 33 | + { |
| 34 | + account_category: 'trading', |
| 35 | + account_type: 'type', |
| 36 | + broker: 'VRTC', |
| 37 | + created_at: 1718953961, |
| 38 | + currency: 'USD', |
| 39 | + currency_type: 'fiat', |
| 40 | + is_disabled: 0, |
| 41 | + is_virtual: 1, |
| 42 | + landing_company_name: 'virtual_name', |
| 43 | + linked_to: [], |
| 44 | + loginid: 'VRTC00001', |
| 45 | + }, |
| 46 | +]; |
| 47 | + |
| 48 | +const mockBalanceData = { |
| 49 | + accounts: { |
| 50 | + CR00001: { |
| 51 | + balance: 10001, |
| 52 | + converted_amount: 10001, |
| 53 | + currency: 'USD', |
| 54 | + demo_account: 0, |
| 55 | + status: 1, |
| 56 | + type: 'deriv', |
| 57 | + }, |
| 58 | + VRTC00001: { |
| 59 | + balance: 10000, |
| 60 | + converted_amount: 10000, |
| 61 | + currency: 'USD', |
| 62 | + demo_account: 1, |
| 63 | + status: 1, |
| 64 | + type: 'deriv', |
| 65 | + }, |
| 66 | + }, |
| 67 | + balance: 10001, |
| 68 | + currency: 'USD', |
| 69 | + loginid: 'CR00001', |
| 70 | + total: { |
| 71 | + deriv: { |
| 72 | + amount: 10001, |
| 73 | + currency: 'USD', |
| 74 | + }, |
| 75 | + deriv_demo: { |
| 76 | + amount: 10000, |
| 77 | + currency: 'USD', |
| 78 | + }, |
| 79 | + mt5: { |
| 80 | + amount: 0, |
| 81 | + currency: 'USD', |
| 82 | + }, |
| 83 | + mt5_demo: { |
| 84 | + amount: 0, |
| 85 | + currency: 'USD', |
| 86 | + }, |
| 87 | + }, |
| 88 | +}; |
| 89 | +const mockUseAccountList = useAccountList as jest.Mock; |
| 90 | +const mockUseAuthData = useAuthData as jest.Mock; |
| 91 | +const mockUseBalance = api.account.useBalance as jest.Mock; |
| 92 | + |
| 93 | +describe('useActiveAccount', () => { |
| 94 | + it('should return undefined if no active account was found', () => { |
| 95 | + mockUseAccountList.mockReturnValue({ data: mockAccountList }); |
| 96 | + mockUseAuthData.mockReturnValue({ activeLoginid: 'VRTC00002' }); |
| 97 | + mockUseBalance.mockReturnValue({ data: mockBalanceData }); |
| 98 | + |
| 99 | + const { result } = renderHook(() => useActiveAccount()); |
| 100 | + |
| 101 | + expect(result.current.data).toBeUndefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return the active account with the correct balance', () => { |
| 105 | + mockUseAccountList.mockReturnValue({ data: mockAccountList }); |
| 106 | + mockUseAuthData.mockReturnValue({ activeLoginid: 'CR00001' }); |
| 107 | + mockUseBalance.mockReturnValue({ data: mockBalanceData }); |
| 108 | + |
| 109 | + const { result } = renderHook(() => useActiveAccount()); |
| 110 | + |
| 111 | + expect(result.current.data).toEqual({ |
| 112 | + account_category: 'trading', |
| 113 | + account_type: 'type', |
| 114 | + balance: 10001, |
| 115 | + broker: 'CR', |
| 116 | + created_at: 1718953961, |
| 117 | + currency: 'USD', |
| 118 | + currency_type: 'fiat', |
| 119 | + is_disabled: 0, |
| 120 | + is_virtual: 0, |
| 121 | + landing_company_name: 'name', |
| 122 | + linked_to: [], |
| 123 | + loginid: 'CR00001', |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should return balance as 0 if balance data is not available', () => { |
| 128 | + mockUseAccountList.mockReturnValue({ data: mockAccountList }); |
| 129 | + mockUseAuthData.mockReturnValue({ activeLoginid: 'CR00001' }); |
| 130 | + mockUseBalance.mockReturnValue({ data: {} }); |
| 131 | + |
| 132 | + const { result } = renderHook(() => useActiveAccount()); |
| 133 | + |
| 134 | + expect(result.current.data).toEqual({ |
| 135 | + account_category: 'trading', |
| 136 | + account_type: 'type', |
| 137 | + balance: 0, |
| 138 | + broker: 'CR', |
| 139 | + created_at: 1718953961, |
| 140 | + currency: 'USD', |
| 141 | + currency_type: 'fiat', |
| 142 | + is_disabled: 0, |
| 143 | + is_virtual: 0, |
| 144 | + landing_company_name: 'name', |
| 145 | + linked_to: [], |
| 146 | + loginid: 'CR00001', |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments