-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4501399
commit 92f2364
Showing
13 changed files
with
623 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { isEns } from './isEns'; | ||
|
||
describe('isEns', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('Returns true for mainnet names', async () => { | ||
expect(isEns('shrek.eth')).toBe(true); | ||
expect(isEns('shrek.optimisim.eth')).toBe(true); | ||
expect(isEns('shrek.baaaaaes.eth')).toBe(true); | ||
}); | ||
|
||
it('Returns true for mainnet sepolia names', async () => { | ||
expect(isEns('shrek.test.eth')).toBe(true); | ||
}); | ||
|
||
it('Returns false for basenames', async () => { | ||
expect(isEns('shrek.base.eth')).toBe(false); | ||
expect(isEns('shrek.basetest.eth')).toBe(false); | ||
}); | ||
|
||
it('Returns false for any other name', async () => { | ||
expect(isEns('shrek.optimisim')).toBe(false); | ||
expect(isEns('shrek.baaaaaes')).toBe(false); | ||
expect(isEns('shrek')).toBe(false); | ||
expect(isEns('shrek.sol')).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const isEns = (username: string) => { | ||
if (username.endsWith('.base.eth') || username.endsWith('.basetest.eth')) { | ||
return false; | ||
} | ||
if (username.endsWith('.eth') || username.endsWith('.test.eth')) { | ||
return true; | ||
} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { useAmountInput } from './useAmountInput'; | ||
|
||
describe('useAmountInput', () => { | ||
const defaultProps = { | ||
setFiatAmount: vi.fn(), | ||
setCryptoAmount: vi.fn(), | ||
selectedInputType: 'fiat' as const, | ||
exchangeRate: '2', | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('handleFiatChange', () => { | ||
it('should handle fiat input and calculate crypto amount', () => { | ||
const { result } = renderHook(() => useAmountInput(defaultProps)); | ||
|
||
act(() => { | ||
result.current.handleFiatChange('100.456'); | ||
}); | ||
|
||
expect(defaultProps.setFiatAmount).toHaveBeenCalledWith('100.46'); | ||
expect(defaultProps.setCryptoAmount).toHaveBeenCalledWith('200.92'); | ||
}); | ||
|
||
it('should set empty crypto amount when fiat is zero', () => { | ||
const { result } = renderHook(() => useAmountInput(defaultProps)); | ||
|
||
act(() => { | ||
result.current.handleFiatChange('0'); | ||
}); | ||
|
||
expect(defaultProps.setCryptoAmount).toHaveBeenCalledWith(''); | ||
}); | ||
}); | ||
|
||
describe('handleCryptoChange', () => { | ||
it('should handle crypto input and calculate fiat amount', () => { | ||
const { result } = renderHook(() => useAmountInput(defaultProps)); | ||
|
||
act(() => { | ||
result.current.handleCryptoChange('200.12345678'); | ||
}); | ||
|
||
expect(defaultProps.setCryptoAmount).toHaveBeenCalledWith('200.12345678'); | ||
expect(defaultProps.setFiatAmount).toHaveBeenCalledWith('100.06'); | ||
}); | ||
|
||
it('should set empty fiat amount when crypto calculation is zero', () => { | ||
const { result } = renderHook(() => useAmountInput(defaultProps)); | ||
|
||
act(() => { | ||
result.current.handleCryptoChange('0'); | ||
}); | ||
|
||
expect(defaultProps.setFiatAmount).toHaveBeenCalledWith(''); | ||
}); | ||
}); | ||
|
||
describe('handleChange', () => { | ||
it('should call handleFiatChange when selectedInputType is fiat', () => { | ||
const { result } = renderHook(() => useAmountInput(defaultProps)); | ||
const onChange = vi.fn(); | ||
|
||
act(() => { | ||
result.current.handleChange('100', onChange); | ||
}); | ||
|
||
expect(defaultProps.setFiatAmount).toHaveBeenCalled(); | ||
expect(onChange).toHaveBeenCalledWith('100'); | ||
}); | ||
|
||
it('should call handleCryptoChange when selectedInputType is crypto', () => { | ||
const { result } = renderHook(() => | ||
useAmountInput({ | ||
...defaultProps, | ||
selectedInputType: 'crypto', | ||
}), | ||
); | ||
const onChange = vi.fn(); | ||
|
||
act(() => { | ||
result.current.handleChange('100', onChange); | ||
}); | ||
|
||
expect(defaultProps.setCryptoAmount).toHaveBeenCalled(); | ||
expect(onChange).toHaveBeenCalledWith('100'); | ||
}); | ||
|
||
it('should work without optional onChange callback', () => { | ||
const { result } = renderHook(() => useAmountInput(defaultProps)); | ||
|
||
act(() => { | ||
result.current.handleChange('100'); | ||
}); | ||
|
||
expect(defaultProps.setFiatAmount).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { truncateDecimalPlaces } from '@/internal/utils/truncateDecimalPlaces'; | ||
import { useCallback, useMemo } from 'react'; | ||
|
||
type UseAmountInputParams = { | ||
setFiatAmount: (value: string) => void; | ||
setCryptoAmount: (value: string) => void; | ||
selectedInputType: 'fiat' | 'crypto'; | ||
exchangeRate: string; | ||
}; | ||
|
||
export const useAmountInput = ({ | ||
setFiatAmount, | ||
setCryptoAmount, | ||
selectedInputType, | ||
exchangeRate, | ||
}: UseAmountInputParams) => { | ||
const handleFiatChange = useCallback( | ||
(value: string) => { | ||
const fiatValue = truncateDecimalPlaces(value, 2); | ||
setFiatAmount(fiatValue); | ||
|
||
const calculatedCryptoValue = String( | ||
Number(fiatValue) * Number(exchangeRate), | ||
); | ||
const resultCryptoValue = truncateDecimalPlaces(calculatedCryptoValue, 8); | ||
setCryptoAmount(calculatedCryptoValue === '0' ? '' : resultCryptoValue); | ||
}, | ||
[exchangeRate, setFiatAmount, setCryptoAmount], | ||
); | ||
|
||
const handleCryptoChange = useCallback( | ||
(value: string) => { | ||
const truncatedValue = truncateDecimalPlaces(value, 8); | ||
setCryptoAmount(truncatedValue); | ||
|
||
const calculatedFiatValue = String( | ||
Number(truncatedValue) / Number(exchangeRate), | ||
); | ||
|
||
const resultFiatValue = truncateDecimalPlaces(calculatedFiatValue, 2); | ||
setFiatAmount(resultFiatValue === '0' ? '' : resultFiatValue); | ||
}, | ||
[exchangeRate, setFiatAmount, setCryptoAmount], | ||
); | ||
|
||
const handleChange = useCallback( | ||
(value: string, onChange?: (value: string) => void) => { | ||
if (selectedInputType === 'fiat') { | ||
handleFiatChange(value); | ||
} else { | ||
handleCryptoChange(value); | ||
} | ||
|
||
onChange?.(value); | ||
}, | ||
[handleFiatChange, handleCryptoChange, selectedInputType], | ||
); | ||
|
||
return useMemo( | ||
() => ({ | ||
handleChange, | ||
handleFiatChange, | ||
handleCryptoChange, | ||
}), | ||
[handleChange, handleFiatChange, handleCryptoChange], | ||
); | ||
}; |
Oops, something went wrong.