|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import type { FundSwapUnit } from '../types'; |
| 4 | +import { useResetFundSwapInputs } from './useResetFundSwapInputs'; |
| 5 | + |
| 6 | +describe('useResetFundSwapInputs', () => { |
| 7 | + const mockFromTokenResponse = { |
| 8 | + refetch: vi.fn().mockResolvedValue(undefined), |
| 9 | + }; |
| 10 | + const mockFromUSDCTokenResponse = { |
| 11 | + refetch: vi.fn().mockResolvedValue(undefined), |
| 12 | + }; |
| 13 | + const mockToTokenResponse = { refetch: vi.fn().mockResolvedValue(undefined) }; |
| 14 | + const mockFromETH: FundSwapUnit = { |
| 15 | + balance: '100', |
| 16 | + balanceResponse: mockFromTokenResponse, |
| 17 | + amount: '50', |
| 18 | + setAmount: vi.fn(), |
| 19 | + setAmountUSD: vi.fn(), |
| 20 | + token: undefined, |
| 21 | + loading: false, |
| 22 | + setLoading: vi.fn(), |
| 23 | + error: undefined, |
| 24 | + }; |
| 25 | + const mockFromUSDC: FundSwapUnit = { |
| 26 | + balance: '100', |
| 27 | + balanceResponse: mockFromUSDCTokenResponse, |
| 28 | + amount: '50', |
| 29 | + setAmount: vi.fn(), |
| 30 | + setAmountUSD: vi.fn(), |
| 31 | + token: undefined, |
| 32 | + loading: false, |
| 33 | + setLoading: vi.fn(), |
| 34 | + error: undefined, |
| 35 | + }; |
| 36 | + const mockTo: FundSwapUnit = { |
| 37 | + balance: '200', |
| 38 | + balanceResponse: mockToTokenResponse, |
| 39 | + amount: '75', |
| 40 | + setAmount: vi.fn(), |
| 41 | + setAmountUSD: vi.fn(), |
| 42 | + token: undefined, |
| 43 | + loading: false, |
| 44 | + setLoading: vi.fn(), |
| 45 | + error: undefined, |
| 46 | + }; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + vi.clearAllMocks(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return a function', () => { |
| 53 | + const { result } = renderHook(() => |
| 54 | + useResetFundSwapInputs({ |
| 55 | + fromETH: mockFromETH, |
| 56 | + fromUSDC: mockFromUSDC, |
| 57 | + to: mockTo, |
| 58 | + }), |
| 59 | + ); |
| 60 | + expect(typeof result.current).toBe('function'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should call refetch on responses and set amounts to empty strings when executed', async () => { |
| 64 | + const { result } = renderHook(() => |
| 65 | + useResetFundSwapInputs({ |
| 66 | + fromETH: mockFromETH, |
| 67 | + fromUSDC: mockFromUSDC, |
| 68 | + to: mockTo, |
| 69 | + }), |
| 70 | + ); |
| 71 | + await act(async () => { |
| 72 | + await result.current(); |
| 73 | + }); |
| 74 | + expect(mockFromTokenResponse.refetch).toHaveBeenCalledTimes(1); |
| 75 | + expect(mockToTokenResponse.refetch).toHaveBeenCalledTimes(1); |
| 76 | + expect(mockFromETH.setAmount).toHaveBeenCalledWith(''); |
| 77 | + expect(mockFromETH.setAmountUSD).toHaveBeenCalledWith(''); |
| 78 | + expect(mockTo.setAmount).toHaveBeenCalledWith(''); |
| 79 | + expect(mockTo.setAmountUSD).toHaveBeenCalledWith(''); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should not create a new function reference if from and to haven't changed", () => { |
| 83 | + const { result, rerender } = renderHook(() => |
| 84 | + useResetFundSwapInputs({ |
| 85 | + fromETH: mockFromETH, |
| 86 | + fromUSDC: mockFromUSDC, |
| 87 | + to: mockTo, |
| 88 | + }), |
| 89 | + ); |
| 90 | + const firstRender = result.current; |
| 91 | + rerender(); |
| 92 | + expect(result.current).toBe(firstRender); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should create a new function reference if from or to change', () => { |
| 96 | + const { result, rerender } = renderHook( |
| 97 | + ({ fromETH, fromUSDC, to }) => |
| 98 | + useResetFundSwapInputs({ |
| 99 | + fromETH, |
| 100 | + fromUSDC, |
| 101 | + to, |
| 102 | + }), |
| 103 | + { |
| 104 | + initialProps: { |
| 105 | + fromETH: mockFromETH, |
| 106 | + fromUSDC: mockFromUSDC, |
| 107 | + to: mockTo, |
| 108 | + }, |
| 109 | + }, |
| 110 | + ); |
| 111 | + const firstRender = result.current; |
| 112 | + const newMockFromETH = { |
| 113 | + ...mockFromETH, |
| 114 | + balanceResponse: { refetch: vi.fn().mockResolvedValue(undefined) }, |
| 115 | + }; |
| 116 | + const newMockFromUSDC = { |
| 117 | + ...mockFromUSDC, |
| 118 | + balanceResponse: { refetch: vi.fn().mockResolvedValue(undefined) }, |
| 119 | + }; |
| 120 | + rerender({ |
| 121 | + fromETH: newMockFromETH, |
| 122 | + fromUSDC: newMockFromUSDC, |
| 123 | + to: mockTo, |
| 124 | + }); |
| 125 | + expect(result.current).not.toBe(firstRender); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should handle null responses gracefully', async () => { |
| 129 | + const mockFromWithNullResponse = { ...mockFromETH, balanceResponse: null }; |
| 130 | + const mockFromUSDCWithNullResponse = { |
| 131 | + ...mockFromUSDC, |
| 132 | + balanceResponse: null, |
| 133 | + }; |
| 134 | + const mockToWithNullResponse = { ...mockTo, balanceResponse: null }; |
| 135 | + const { result } = renderHook(() => |
| 136 | + useResetFundSwapInputs({ |
| 137 | + fromETH: mockFromWithNullResponse, |
| 138 | + fromUSDC: mockFromUSDCWithNullResponse, |
| 139 | + to: mockToWithNullResponse, |
| 140 | + }), |
| 141 | + ); |
| 142 | + await act(async () => { |
| 143 | + await result.current(); |
| 144 | + }); |
| 145 | + expect(mockFromWithNullResponse.setAmount).toHaveBeenCalledWith(''); |
| 146 | + expect(mockFromWithNullResponse.setAmountUSD).toHaveBeenCalledWith(''); |
| 147 | + expect(mockToWithNullResponse.setAmount).toHaveBeenCalledWith(''); |
| 148 | + expect(mockToWithNullResponse.setAmountUSD).toHaveBeenCalledWith(''); |
| 149 | + }); |
| 150 | +}); |
0 commit comments