-
Notifications
You must be signed in to change notification settings - Fork 192
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
c9efda3
commit 4fb8ad3
Showing
2 changed files
with
173 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import type { FundSwapUnit } from '../types'; | ||
import { useResetFundSwapInputs } from './useResetFundSwapInputs'; | ||
|
||
describe('useResetFundSwapInputs', () => { | ||
const mockFromTokenResponse = { | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockFromUSDCTokenResponse = { | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockToTokenResponse = { refetch: vi.fn().mockResolvedValue(undefined) }; | ||
const mockFromETH: FundSwapUnit = { | ||
balance: '100', | ||
balanceResponse: mockFromTokenResponse, | ||
amount: '50', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
}; | ||
const mockFromUSDC: FundSwapUnit = { | ||
balance: '100', | ||
balanceResponse: mockFromUSDCTokenResponse, | ||
amount: '50', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
}; | ||
const mockTo: FundSwapUnit = { | ||
balance: '200', | ||
balanceResponse: mockToTokenResponse, | ||
amount: '75', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should return a function', () => { | ||
const { result } = renderHook(() => | ||
useResetFundSwapInputs({ | ||
fromETH: mockFromETH, | ||
fromUSDC: mockFromUSDC, | ||
to: mockTo, | ||
}), | ||
); | ||
expect(typeof result.current).toBe('function'); | ||
}); | ||
|
||
it('should call refetch on responses and set amounts to empty strings when executed', async () => { | ||
const { result } = renderHook(() => | ||
useResetFundSwapInputs({ | ||
fromETH: mockFromETH, | ||
fromUSDC: mockFromUSDC, | ||
to: mockTo, | ||
}), | ||
); | ||
await act(async () => { | ||
await result.current(); | ||
}); | ||
expect(mockFromTokenResponse.refetch).toHaveBeenCalledTimes(1); | ||
expect(mockToTokenResponse.refetch).toHaveBeenCalledTimes(1); | ||
expect(mockFromETH.setAmount).toHaveBeenCalledWith(''); | ||
expect(mockFromETH.setAmountUSD).toHaveBeenCalledWith(''); | ||
expect(mockTo.setAmount).toHaveBeenCalledWith(''); | ||
expect(mockTo.setAmountUSD).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
it("should not create a new function reference if from and to haven't changed", () => { | ||
const { result, rerender } = renderHook(() => | ||
useResetFundSwapInputs({ | ||
fromETH: mockFromETH, | ||
fromUSDC: mockFromUSDC, | ||
to: mockTo, | ||
}), | ||
); | ||
const firstRender = result.current; | ||
rerender(); | ||
expect(result.current).toBe(firstRender); | ||
}); | ||
|
||
it('should create a new function reference if from or to change', () => { | ||
const { result, rerender } = renderHook( | ||
({ fromETH, fromUSDC, to }) => | ||
useResetFundSwapInputs({ | ||
fromETH, | ||
fromUSDC, | ||
to, | ||
}), | ||
{ | ||
initialProps: { | ||
fromETH: mockFromETH, | ||
fromUSDC: mockFromUSDC, | ||
to: mockTo, | ||
}, | ||
}, | ||
); | ||
const firstRender = result.current; | ||
const newMockFromETH = { | ||
...mockFromETH, | ||
balanceResponse: { refetch: vi.fn().mockResolvedValue(undefined) }, | ||
}; | ||
const newMockFromUSDC = { | ||
...mockFromUSDC, | ||
balanceResponse: { refetch: vi.fn().mockResolvedValue(undefined) }, | ||
}; | ||
rerender({ | ||
fromETH: newMockFromETH, | ||
fromUSDC: newMockFromUSDC, | ||
to: mockTo, | ||
}); | ||
expect(result.current).not.toBe(firstRender); | ||
}); | ||
|
||
it('should handle null responses gracefully', async () => { | ||
const mockFromWithNullResponse = { ...mockFromETH, balanceResponse: null }; | ||
const mockFromUSDCWithNullResponse = { | ||
...mockFromUSDC, | ||
balanceResponse: null, | ||
}; | ||
const mockToWithNullResponse = { ...mockTo, balanceResponse: null }; | ||
const { result } = renderHook(() => | ||
useResetFundSwapInputs({ | ||
fromETH: mockFromWithNullResponse, | ||
fromUSDC: mockFromUSDCWithNullResponse, | ||
to: mockToWithNullResponse, | ||
}), | ||
); | ||
await act(async () => { | ||
await result.current(); | ||
}); | ||
expect(mockFromWithNullResponse.setAmount).toHaveBeenCalledWith(''); | ||
expect(mockFromWithNullResponse.setAmountUSD).toHaveBeenCalledWith(''); | ||
expect(mockToWithNullResponse.setAmount).toHaveBeenCalledWith(''); | ||
expect(mockToWithNullResponse.setAmountUSD).toHaveBeenCalledWith(''); | ||
}); | ||
}); |
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,23 @@ | ||
import { useCallback } from 'react'; | ||
import type { FundSwapTokens } from '../types'; | ||
|
||
// Refreshes balances and inputs post-swap | ||
export const useResetFundSwapInputs = ({ | ||
fromETH, | ||
fromUSDC, | ||
to, | ||
}: FundSwapTokens) => { | ||
return useCallback(async () => { | ||
await Promise.all([ | ||
fromETH.balanceResponse?.refetch(), | ||
fromUSDC.balanceResponse?.refetch(), | ||
to.balanceResponse?.refetch(), | ||
fromETH.setAmount(''), | ||
fromUSDC.setAmount(''), | ||
fromETH.setAmountUSD(''), | ||
fromUSDC.setAmountUSD(''), | ||
to.setAmount(''), | ||
to.setAmountUSD(''), | ||
]); | ||
}, [fromETH, fromUSDC, to]); | ||
}; |