|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { base } from 'viem/chains'; |
| 3 | +import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { useValue } from '../../internal/hooks/useValue'; |
| 5 | +import type { Token } from '../../token'; |
| 6 | +import { USDC_TOKEN } from '../mocks'; |
| 7 | +import { useFundSwapTokens } from './useFundSwapTokens'; |
| 8 | +import { useSwapBalances } from './useSwapBalances'; |
| 9 | + |
| 10 | +vi.mock('./useSwapBalances', () => ({ |
| 11 | + useSwapBalances: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock('../../internal/hooks/useValue', () => ({ |
| 15 | + useValue: vi.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +const toToken: Token = { |
| 19 | + name: 'DEGEN', |
| 20 | + address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed', |
| 21 | + symbol: 'DEGEN', |
| 22 | + decimals: 18, |
| 23 | + image: |
| 24 | + 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm', |
| 25 | + chainId: base.id, |
| 26 | +}; |
| 27 | + |
| 28 | +describe('useFundSwapTokens', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return correct values', () => { |
| 34 | + (useSwapBalances as Mock).mockReturnValue({ |
| 35 | + fromBalanceString: '100', |
| 36 | + fromTokenBalanceError: null, |
| 37 | + fromTokenResponse: { refetch: vi.fn() }, |
| 38 | + toBalanceString: '200', |
| 39 | + toTokenBalanceError: null, |
| 40 | + toTokenResponse: { refetch: vi.fn() }, |
| 41 | + }); |
| 42 | + (useValue as Mock).mockImplementation((props) => ({ |
| 43 | + ...props, |
| 44 | + amount: '100', |
| 45 | + amountUSD: '150', |
| 46 | + response: props.response, |
| 47 | + setAmount: vi.fn(), |
| 48 | + setAmountUSD: vi.fn(), |
| 49 | + setLoading: vi.fn(), |
| 50 | + token: USDC_TOKEN, |
| 51 | + })); |
| 52 | + const { result } = renderHook(() => useFundSwapTokens(toToken, '0x123')); |
| 53 | + expect(result.current.fromETH).toEqual({ |
| 54 | + amount: '100', |
| 55 | + amountUSD: '150', |
| 56 | + balance: '100', |
| 57 | + balanceResponse: { refetch: expect.any(Function) }, |
| 58 | + error: null, |
| 59 | + loading: false, |
| 60 | + setAmount: expect.any(Function), |
| 61 | + setAmountUSD: expect.any(Function), |
| 62 | + setLoading: expect.any(Function), |
| 63 | + token: USDC_TOKEN, |
| 64 | + }); |
| 65 | + expect(result.current.to).toEqual({ |
| 66 | + amount: '100', |
| 67 | + amountUSD: '150', |
| 68 | + balance: '200', |
| 69 | + balanceResponse: { refetch: expect.any(Function) }, |
| 70 | + error: null, |
| 71 | + loading: false, |
| 72 | + setAmount: expect.any(Function), |
| 73 | + setAmountUSD: expect.any(Function), |
| 74 | + setLoading: expect.any(Function), |
| 75 | + token: USDC_TOKEN, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should call fromTokenResponse.refetch when fromETH.response.refetch is called', async () => { |
| 80 | + const mockFromRefetch = vi.fn().mockResolvedValue(undefined); |
| 81 | + const mockToRefetch = vi.fn().mockResolvedValue(undefined); |
| 82 | + (useSwapBalances as Mock).mockReturnValue({ |
| 83 | + fromTokenResponse: { refetch: mockFromRefetch }, |
| 84 | + toTokenResponse: { refetch: mockToRefetch }, |
| 85 | + }); |
| 86 | + (useValue as Mock).mockImplementation((props) => ({ |
| 87 | + ...props, |
| 88 | + response: props.response, |
| 89 | + })); |
| 90 | + const { result } = renderHook(() => useFundSwapTokens(toToken, '0x123')); |
| 91 | + await act(async () => { |
| 92 | + await result.current.fromETH.balanceResponse?.refetch(); |
| 93 | + }); |
| 94 | + expect(mockFromRefetch).toHaveBeenCalledTimes(1); |
| 95 | + expect(mockToRefetch).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should call toTokenResponse.refetch when to.response.refetch is called', async () => { |
| 99 | + const mockFromRefetch = vi.fn().mockResolvedValue(undefined); |
| 100 | + const mockToRefetch = vi.fn().mockResolvedValue(undefined); |
| 101 | + (useSwapBalances as Mock).mockReturnValue({ |
| 102 | + fromTokenResponse: { refetch: mockFromRefetch }, |
| 103 | + toTokenResponse: { refetch: mockToRefetch }, |
| 104 | + }); |
| 105 | + (useValue as Mock).mockImplementation((props) => ({ |
| 106 | + ...props, |
| 107 | + response: props.response, |
| 108 | + })); |
| 109 | + const { result } = renderHook(() => useFundSwapTokens(toToken, '0x123')); |
| 110 | + await act(async () => { |
| 111 | + await result.current.to.balanceResponse?.refetch(); |
| 112 | + }); |
| 113 | + expect(mockToRefetch).toHaveBeenCalledTimes(1); |
| 114 | + expect(mockFromRefetch).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | +}); |
0 commit comments