|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { ETH, USDT } from '@ant-design/web3-assets/tokens'; |
| 3 | +import { fireEvent, render } from '@testing-library/react'; |
| 4 | +import { describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import type { CryptoInputProps } from '..'; |
| 7 | +import { CryptoInput } from '..'; |
| 8 | + |
| 9 | +// Mock tokens |
| 10 | +const mockTokens = [ETH, USDT]; |
| 11 | + |
| 12 | +describe('CryptoInput component', () => { |
| 13 | + it('should render the component with placeholder text', () => { |
| 14 | + const { baseElement } = render(<CryptoInput tokenList={mockTokens} />); |
| 15 | + |
| 16 | + expect(baseElement.querySelector('.ant-input-number-input')?.getAttribute('placeholder')).toBe( |
| 17 | + 'Please enter amount', |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should display correct header', () => { |
| 22 | + // no header |
| 23 | + const { baseElement, rerender } = render(<CryptoInput tokenList={mockTokens} />); |
| 24 | + |
| 25 | + expect(baseElement.querySelector('.web3-crypto-input-wrapper')?.children?.length).toBe(2); |
| 26 | + expect(baseElement.querySelector('.web3-crypto-input-header')).toBeNull(); |
| 27 | + |
| 28 | + // custom header |
| 29 | + rerender( |
| 30 | + <CryptoInput |
| 31 | + tokenList={mockTokens} |
| 32 | + header={<div className="custom-header">Custom header</div>} |
| 33 | + />, |
| 34 | + ); |
| 35 | + |
| 36 | + expect(baseElement.querySelector('.web3-crypto-input-wrapper')?.children?.length).toBe(3); |
| 37 | + |
| 38 | + const headerEle = baseElement.querySelector('.web3-crypto-input-header'); |
| 39 | + |
| 40 | + expect(headerEle).not.toBeNull(); |
| 41 | + expect(headerEle!.textContent).toBe('Custom header'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should display correct footer', () => { |
| 45 | + // close footer |
| 46 | + const { baseElement, rerender } = render(<CryptoInput tokenList={mockTokens} footer={false} />); |
| 47 | + |
| 48 | + expect(baseElement.querySelector('.web3-crypto-input-wrapper')?.children?.length).toBe(1); |
| 49 | + |
| 50 | + // custom footer |
| 51 | + rerender( |
| 52 | + <CryptoInput |
| 53 | + tokenList={mockTokens} |
| 54 | + footer={<div className="custom-footer">Custom footer</div>} |
| 55 | + />, |
| 56 | + ); |
| 57 | + |
| 58 | + expect(baseElement.querySelector('.web3-crypto-input-wrapper')?.children?.length).toBe(2); |
| 59 | + expect(baseElement.querySelector('.custom-footer')).not.toBeNull(); |
| 60 | + expect(baseElement.querySelector('.custom-footer')?.textContent).toBe('Custom footer'); |
| 61 | + |
| 62 | + // default footer |
| 63 | + rerender(<CryptoInput tokenList={mockTokens} />); |
| 64 | + |
| 65 | + expect(baseElement.querySelector('.web3-crypto-input-footer')).not.toBeNull(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should display the token list when clicked', () => { |
| 69 | + const { baseElement } = render(<CryptoInput tokenList={mockTokens} />); |
| 70 | + |
| 71 | + fireEvent.mouseDown(baseElement.querySelector('.ant-select-selector') as Element); |
| 72 | + |
| 73 | + const selectOptions = baseElement.querySelectorAll('.ant-select-item'); |
| 74 | + |
| 75 | + expect(selectOptions.length).toBe(2); |
| 76 | + |
| 77 | + expect(selectOptions[0].textContent).includes('Ethereum'); |
| 78 | + expect(selectOptions[1].textContent).includes('Tether USD'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should call onChange with selected token and amount input', () => { |
| 82 | + const TestComponent = (props: CryptoInputProps) => { |
| 83 | + const [crypto, setCrypto] = useState<CryptoInputProps['value']>(); |
| 84 | + |
| 85 | + return ( |
| 86 | + <CryptoInput |
| 87 | + tokenList={mockTokens} |
| 88 | + value={crypto} |
| 89 | + onChange={(newCrypto) => { |
| 90 | + setCrypto(newCrypto); |
| 91 | + |
| 92 | + props.onChange?.(newCrypto); |
| 93 | + }} |
| 94 | + /> |
| 95 | + ); |
| 96 | + }; |
| 97 | + |
| 98 | + const handleChange = vi.fn(); |
| 99 | + |
| 100 | + const { baseElement } = render(<TestComponent onChange={handleChange} />); |
| 101 | + |
| 102 | + fireEvent.mouseDown(baseElement.querySelector('.ant-select-selector') as Element); |
| 103 | + |
| 104 | + const selectOptions = baseElement.querySelectorAll('.ant-select-item'); |
| 105 | + |
| 106 | + fireEvent.click(selectOptions[0]); |
| 107 | + |
| 108 | + expect(handleChange).toHaveBeenCalledWith({ token: mockTokens[0] }); |
| 109 | + |
| 110 | + fireEvent.change(baseElement.querySelector('.ant-input-number-input') as Element, { |
| 111 | + target: { value: '10' }, |
| 112 | + }); |
| 113 | + |
| 114 | + expect(handleChange).toHaveBeenCalledWith({ |
| 115 | + token: mockTokens[0], |
| 116 | + amount: 10000000000000000000n, |
| 117 | + inputString: '10', |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should correct handle token decimals', () => { |
| 122 | + const TestComponent = (props: CryptoInputProps) => { |
| 123 | + const [crypto, setCrypto] = useState<CryptoInputProps['value']>(); |
| 124 | + |
| 125 | + return ( |
| 126 | + <CryptoInput |
| 127 | + tokenList={mockTokens} |
| 128 | + value={crypto} |
| 129 | + onChange={(newCrypto) => { |
| 130 | + setCrypto(newCrypto); |
| 131 | + |
| 132 | + props.onChange?.(newCrypto); |
| 133 | + }} |
| 134 | + /> |
| 135 | + ); |
| 136 | + }; |
| 137 | + |
| 138 | + const handleChange = vi.fn(); |
| 139 | + |
| 140 | + const { baseElement } = render(<TestComponent onChange={handleChange} />); |
| 141 | + |
| 142 | + fireEvent.mouseDown(baseElement.querySelector('.ant-select-selector') as Element); |
| 143 | + |
| 144 | + const selectOptions = baseElement.querySelectorAll('.ant-select-item'); |
| 145 | + |
| 146 | + fireEvent.click(selectOptions[0]); |
| 147 | + |
| 148 | + const inputEle = baseElement.querySelector('.ant-input-number-input') as Element; |
| 149 | + |
| 150 | + /** |
| 151 | + * check token amount value |
| 152 | + * first input some value and the input element should display the same value |
| 153 | + * then check onChange callback is called with correct amount |
| 154 | + * then blur the input element, when input value decimals is over token decimals, it should cut correctly |
| 155 | + */ |
| 156 | + function checkValue(orginInputValue: string, expectInputValue: string, expectAmount: bigint) { |
| 157 | + fireEvent.change(inputEle, { |
| 158 | + target: { value: orginInputValue }, |
| 159 | + }); |
| 160 | + |
| 161 | + expect(inputEle.getAttribute('value')).toBe(orginInputValue); |
| 162 | + |
| 163 | + expect(handleChange).toHaveBeenCalledWith({ |
| 164 | + token: mockTokens[0], |
| 165 | + amount: expectAmount, |
| 166 | + inputString: expectInputValue, |
| 167 | + }); |
| 168 | + |
| 169 | + fireEvent.blur(inputEle); |
| 170 | + |
| 171 | + expect(inputEle.getAttribute('value')).toBe(expectInputValue); |
| 172 | + } |
| 173 | + |
| 174 | + // smaller than token decimals |
| 175 | + checkValue('0.012345678', '0.012345678', 12345678000000000n); |
| 176 | + |
| 177 | + // equal to token decimals |
| 178 | + checkValue('0.012345678901234567', '0.012345678901234567', 12345678901234567n); |
| 179 | + |
| 180 | + // over token decimals and cut correctly |
| 181 | + checkValue('0.01234567890123456789', '0.012345678901234567', 12345678901234567n); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should calculate correct total price', () => { |
| 185 | + const TestComponent = (props: CryptoInputProps) => { |
| 186 | + const [crypto, setCrypto] = useState<CryptoInputProps['value']>({ token: mockTokens[0] }); |
| 187 | + |
| 188 | + return ( |
| 189 | + <CryptoInput |
| 190 | + tokenList={mockTokens} |
| 191 | + value={crypto} |
| 192 | + onChange={setCrypto} |
| 193 | + balance={{ amount: 100000000000000000000n, unit: '$', price: 3894.57 }} |
| 194 | + {...props} |
| 195 | + /> |
| 196 | + ); |
| 197 | + }; |
| 198 | + |
| 199 | + const { baseElement, rerender } = render(<TestComponent />); |
| 200 | + |
| 201 | + // set token amount to 10 |
| 202 | + fireEvent.change(baseElement.querySelector('.ant-input-number-input') as Element, { |
| 203 | + target: { value: '10.012345678' }, |
| 204 | + }); |
| 205 | + |
| 206 | + expect(baseElement.querySelector('.total-price')?.textContent).toBe('$ 38993.78110716846'); |
| 207 | + expect(baseElement.querySelector('.token-balance')?.textContent).includes('100'); |
| 208 | + |
| 209 | + // change token amount to max |
| 210 | + fireEvent.click(baseElement.querySelector('.max-button') as Element); |
| 211 | + |
| 212 | + expect(baseElement.querySelector('.ant-input-number-input')?.getAttribute('value')).toBe('100'); |
| 213 | + expect(baseElement.querySelector('.total-price')?.textContent).toBe('$ 389457'); |
| 214 | + |
| 215 | + // set token amount to null |
| 216 | + fireEvent.change(baseElement.querySelector('.ant-input-number-input') as Element, { |
| 217 | + target: { value: null }, |
| 218 | + }); |
| 219 | + |
| 220 | + expect(baseElement.querySelector('.total-price')?.textContent).toBe('-'); |
| 221 | + |
| 222 | + // change token balance to undefined |
| 223 | + rerender(<TestComponent balance={undefined} />); |
| 224 | + expect(baseElement.querySelector('.total-price')?.textContent).toBe('-'); |
| 225 | + }); |
| 226 | +}); |
0 commit comments