|
| 1 | +import React from 'react'; |
| 2 | +import { Formik } from 'formik'; |
| 3 | +import { act, fireEvent, render, screen } from '@testing-library/react'; |
| 4 | +import { useWithdrawalCryptoContext } from '../../../../../provider'; |
| 5 | +import { validateCryptoInput, validateFiatInput } from '../../../../../utils'; |
| 6 | +import WithdrawalCryptoAmountConverter from '../WithdrawalCryptoAmountConverter'; |
| 7 | + |
| 8 | +jest.mock('../../../../../utils', () => ({ |
| 9 | + ...jest.requireActual('../../../../../utils'), |
| 10 | + validateCryptoInput: jest.fn(), |
| 11 | + validateFiatInput: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('../../../../../provider', () => ({ |
| 15 | + ...jest.requireActual('../../../../../provider'), |
| 16 | + useWithdrawalCryptoContext: jest.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +const mockUseWithdrawalCryptoContext = useWithdrawalCryptoContext as jest.MockedFunction< |
| 20 | + typeof useWithdrawalCryptoContext |
| 21 | +>; |
| 22 | +const mockValidateCryptoInput = validateCryptoInput as jest.Mock; |
| 23 | +const mockValidateFiatInput = validateFiatInput as jest.Mock; |
| 24 | + |
| 25 | +const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => { |
| 26 | + return ( |
| 27 | + <Formik |
| 28 | + initialErrors={{}} |
| 29 | + initialValues={{ |
| 30 | + cryptoAddress: '', |
| 31 | + cryptoAmount: '', |
| 32 | + fiatAmount: '', |
| 33 | + }} |
| 34 | + onSubmit={jest.fn()} |
| 35 | + > |
| 36 | + {children} |
| 37 | + </Formik> |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +describe('WithdrawalCryptoAmountConverter', () => { |
| 42 | + beforeEach(() => { |
| 43 | + mockUseWithdrawalCryptoContext.mockReturnValue({ |
| 44 | + accountLimits: { |
| 45 | + remainder: undefined, |
| 46 | + }, |
| 47 | + // @ts-expect-error - since this is a mock, we only need partial properties of the hook |
| 48 | + activeAccount: { |
| 49 | + currency: 'BTC', |
| 50 | + }, |
| 51 | + fractionalDigits: { |
| 52 | + crypto: 8, |
| 53 | + fiat: 2, |
| 54 | + }, |
| 55 | + getConvertedCryptoAmount: (fiatInput: number | string) => fiatInput as string, |
| 56 | + getConvertedFiatAmount: (cryptoInput: number | string) => cryptoInput as string, |
| 57 | + isClientVerified: false, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + afterEach(() => { |
| 62 | + jest.clearAllMocks(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should display error below crypto input field if crypto input is invalid', async () => { |
| 66 | + mockValidateCryptoInput.mockReturnValue('Crypto Input Error'); |
| 67 | + |
| 68 | + render(<WithdrawalCryptoAmountConverter />, { wrapper }); |
| 69 | + |
| 70 | + const cryptoInput = screen.getByTestId('dt_withdrawal_crypto_amount_input'); |
| 71 | + |
| 72 | + await act(async () => { |
| 73 | + await fireEvent.change(cryptoInput, { target: { value: '10' } }); |
| 74 | + }); |
| 75 | + expect(screen.getByText('Crypto Input Error')).toBeInTheDocument(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should change value of fiat input field when value of crypto input changes', async () => { |
| 79 | + mockValidateCryptoInput.mockReturnValue(''); |
| 80 | + |
| 81 | + render(<WithdrawalCryptoAmountConverter />, { wrapper }); |
| 82 | + |
| 83 | + const cryptoInput = screen.getByTestId('dt_withdrawal_crypto_amount_input'); |
| 84 | + const fiatInput = screen.getByTestId('dt_withdrawal_fiat_amount_input'); |
| 85 | + await act(async () => { |
| 86 | + await fireEvent.change(cryptoInput, { target: { value: '10' } }); |
| 87 | + }); |
| 88 | + expect(fiatInput).toHaveValue('10'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should empty fiat input field if crypto input field has errors', async () => { |
| 92 | + mockValidateCryptoInput.mockReturnValue('Crypto Input Error'); |
| 93 | + |
| 94 | + render(<WithdrawalCryptoAmountConverter />, { wrapper }); |
| 95 | + |
| 96 | + const cryptoInput = screen.getByTestId('dt_withdrawal_crypto_amount_input'); |
| 97 | + const fiatInput = screen.getByTestId('dt_withdrawal_fiat_amount_input'); |
| 98 | + await act(async () => { |
| 99 | + await fireEvent.change(cryptoInput, { target: { value: '10' } }); |
| 100 | + }); |
| 101 | + expect(fiatInput).toHaveValue(''); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should display error below fiat input field if fiat input is invalid', async () => { |
| 105 | + mockValidateFiatInput.mockReturnValue('Fiat Input Error'); |
| 106 | + |
| 107 | + render(<WithdrawalCryptoAmountConverter />, { wrapper }); |
| 108 | + |
| 109 | + const fiatInput = screen.getByTestId('dt_withdrawal_fiat_amount_input'); |
| 110 | + |
| 111 | + await act(async () => { |
| 112 | + await fireEvent.change(fiatInput, { target: { value: '10' } }); |
| 113 | + }); |
| 114 | + expect(screen.getByText('Fiat Input Error')).toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should change value of crypto input field when value of fiat input changes', async () => { |
| 118 | + mockValidateFiatInput.mockReturnValue(''); |
| 119 | + |
| 120 | + render(<WithdrawalCryptoAmountConverter />, { wrapper }); |
| 121 | + |
| 122 | + const cryptoInput = screen.getByTestId('dt_withdrawal_crypto_amount_input'); |
| 123 | + const fiatInput = screen.getByTestId('dt_withdrawal_fiat_amount_input'); |
| 124 | + await act(async () => { |
| 125 | + await fireEvent.change(fiatInput, { target: { value: '10' } }); |
| 126 | + }); |
| 127 | + expect(cryptoInput).toHaveValue('10'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should empty crypto input field if fiat input field has errors', async () => { |
| 131 | + mockValidateFiatInput.mockReturnValue('Fiat Input Error'); |
| 132 | + |
| 133 | + render(<WithdrawalCryptoAmountConverter />, { wrapper }); |
| 134 | + |
| 135 | + const cryptoInput = screen.getByTestId('dt_withdrawal_crypto_amount_input'); |
| 136 | + const fiatInput = screen.getByTestId('dt_withdrawal_fiat_amount_input'); |
| 137 | + await act(async () => { |
| 138 | + await fireEvent.change(fiatInput, { target: { value: '10' } }); |
| 139 | + }); |
| 140 | + expect(cryptoInput).toHaveValue(''); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle onFocus for crypto input field', async () => { |
| 144 | + render(<WithdrawalCryptoAmountConverter />, { wrapper }); |
| 145 | + |
| 146 | + const cryptoInput = screen.getByTestId('dt_withdrawal_crypto_amount_input'); |
| 147 | + await act(async () => { |
| 148 | + await fireEvent.focus(cryptoInput); |
| 149 | + }); |
| 150 | + |
| 151 | + expect(screen.queryByTestId('dt_withdrawal_crypto_amount_converter_arrow')).not.toHaveClass( |
| 152 | + 'wallets-withdrawal-crypto-amount-converter__arrow--rtl' |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should handle onFocus for fiat input field', async () => { |
| 157 | + render(<WithdrawalCryptoAmountConverter />, { wrapper }); |
| 158 | + |
| 159 | + const fiatInput = screen.getByTestId('dt_withdrawal_fiat_amount_input'); |
| 160 | + await act(async () => { |
| 161 | + await fireEvent.focus(fiatInput); |
| 162 | + }); |
| 163 | + |
| 164 | + expect(screen.getByTestId('dt_withdrawal_crypto_amount_converter_arrow')).toHaveClass( |
| 165 | + 'wallets-withdrawal-crypto-amount-converter__arrow--rtl' |
| 166 | + ); |
| 167 | + }); |
| 168 | +}); |
0 commit comments