|
| 1 | +import { useAccountStatus, useActiveTradingAccount, useCFDAccountsList, useQuery } from '@deriv/api-v2'; |
| 2 | +import { renderHook } from '@testing-library/react-hooks'; |
| 3 | +import useCurrencies from '../useCurrencies'; |
| 4 | +import useDisableFiatCurrencies from '../useDisableFiatCurrencies'; |
| 5 | + |
| 6 | +jest.mock('@deriv/api-v2', () => ({ |
| 7 | + useAccountStatus: jest.fn(), |
| 8 | + useActiveTradingAccount: jest.fn(), |
| 9 | + useCFDAccountsList: jest.fn(), |
| 10 | + useQuery: jest.fn(), |
| 11 | + useCurrencies: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('../useCurrencies', () => jest.fn()); |
| 15 | + |
| 16 | +describe('useDisableFiatCurrencies', () => { |
| 17 | + const mockData = { |
| 18 | + activeDerivTradingAccount: { balance: 1000 }, |
| 19 | + accountStatus: { is_deposit_attempt: true }, |
| 20 | + cfdAccountsList: { dxtrade: [], mt5: [] }, |
| 21 | + currentAccountCurrencyConfig: { type: 'fiat' }, |
| 22 | + addedFiatCurrency: { id: 'USD', type: 'fiat', isAdded: true }, |
| 23 | + statements: { statement: { count: 3, transactions: [{}, {}, {}] } }, |
| 24 | + }; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + (useActiveTradingAccount as jest.Mock).mockReturnValue({ data: mockData.activeDerivTradingAccount }); |
| 28 | + (useAccountStatus as jest.Mock).mockReturnValue({ data: mockData.accountStatus, isSuccess: true }); |
| 29 | + (useCFDAccountsList as jest.Mock).mockReturnValue({ data: mockData.cfdAccountsList }); |
| 30 | + (useQuery as jest.Mock).mockReturnValue({ data: mockData.statements }); |
| 31 | + (useCurrencies as jest.Mock).mockReturnValue({ |
| 32 | + currentAccountCurrencyConfig: mockData.currentAccountCurrencyConfig, |
| 33 | + addedFiatCurrency: mockData.addedFiatCurrency, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should disable fiat currencies based on account status and current account currency', () => { |
| 38 | + const { result } = renderHook(() => useDisableFiatCurrencies()); |
| 39 | + |
| 40 | + expect(result.current).toBe(true); |
| 41 | + |
| 42 | + (useCurrencies as jest.Mock).mockReturnValue({ |
| 43 | + currentAccountCurrencyConfig: { type: 'fiat' }, |
| 44 | + addedFiatCurrency: undefined, |
| 45 | + }); |
| 46 | + const { result: resultWithBalance } = renderHook(() => useDisableFiatCurrencies()); |
| 47 | + expect(resultWithBalance.current).toBe(true); |
| 48 | + |
| 49 | + (useCurrencies as jest.Mock).mockReturnValue({ |
| 50 | + currentAccountCurrencyConfig: { type: 'fiat' }, |
| 51 | + addedFiatCurrency: mockData.addedFiatCurrency, |
| 52 | + }); |
| 53 | + const { result: resultDepositAttempt } = renderHook(() => useDisableFiatCurrencies()); |
| 54 | + expect(resultDepositAttempt.current).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should enable fiat currencies if the current account currency is not fiat', () => { |
| 58 | + (useCurrencies as jest.Mock).mockReturnValue({ |
| 59 | + currentAccountCurrencyConfig: { type: 'crypto' }, |
| 60 | + addedFiatCurrency: undefined, |
| 61 | + }); |
| 62 | + (useAccountStatus as jest.Mock).mockReturnValue({ data: { is_deposit_attempt: false }, isSuccess: true }); |
| 63 | + const { result } = renderHook(() => useDisableFiatCurrencies()); |
| 64 | + expect(result.current).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should disable fiat currencies if there is no balance and the deposit attempt', () => { |
| 68 | + (useActiveTradingAccount as jest.Mock).mockReturnValue({ data: { balance: 0 } }); |
| 69 | + (useAccountStatus as jest.Mock).mockReturnValue({ data: { is_deposit_attempt: true }, isSuccess: true }); |
| 70 | + const { result } = renderHook(() => useDisableFiatCurrencies()); |
| 71 | + expect(result.current).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should disable fiat currencies if the current account currency is not fiat and the account is a deposit attempt but there are no MT5 accounts', () => { |
| 75 | + (useAccountStatus as jest.Mock).mockReturnValue({ data: { is_deposit_attempt: true }, isSuccess: true }); |
| 76 | + (useCFDAccountsList as jest.Mock).mockReturnValue({ data: { dxtrade: [{}], mt5: undefined } }); |
| 77 | + const { result } = renderHook(() => useDisableFiatCurrencies()); |
| 78 | + expect(result.current).toBe(true); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should disable fiat currencies if the current account currency is not fiat and the account is a deposit attempt but there are no transactions', () => { |
| 82 | + (useAccountStatus as jest.Mock).mockReturnValue({ data: { is_deposit_attempt: true }, isSuccess: true }); |
| 83 | + (useQuery as jest.Mock).mockReturnValue({ data: { statement: { count: 0, transactions: [] } } }); |
| 84 | + const { result } = renderHook(() => useDisableFiatCurrencies()); |
| 85 | + expect(result.current).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should enable fiat currencies if the current account currency is not fiat and the account is not a deposit attempt', () => { |
| 89 | + (useCurrencies as jest.Mock).mockReturnValue({ |
| 90 | + currentAccountCurrencyConfig: { type: 'crypto' }, |
| 91 | + addedFiatCurrency: undefined, |
| 92 | + }); |
| 93 | + (useAccountStatus as jest.Mock).mockReturnValue({ data: { is_deposit_attempt: false }, isSuccess: true }); |
| 94 | + const { result } = renderHook(() => useDisableFiatCurrencies()); |
| 95 | + expect(result.current).toBe(false); |
| 96 | + }); |
| 97 | +}); |
0 commit comments