-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Swap success state - refetch balances and clear inputs (#1089)
- Loading branch information
Showing
9 changed files
with
239 additions
and
27 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,5 @@ | ||
--- | ||
"@coinbase/onchainkit": minor | ||
--- | ||
|
||
**feat**: Swap success state - refetch balances and clear inputs by @0xAlec #1089 |
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
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,96 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import type { SwapUnit } from '../types'; | ||
import { useResetInputs } from './useResetInputs'; | ||
|
||
describe('useResetInputs', () => { | ||
const mockFromTokenResponse = { | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockToTokenResponse = { refetch: vi.fn().mockResolvedValue(undefined) }; | ||
const mockFrom: SwapUnit = { | ||
balance: '100', | ||
balanceResponse: mockFromTokenResponse, | ||
amount: '50', | ||
setAmount: vi.fn(), | ||
token: undefined, | ||
setToken: vi.fn(), | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
}; | ||
const mockTo: SwapUnit = { | ||
balance: '200', | ||
balanceResponse: mockToTokenResponse, | ||
amount: '75', | ||
setAmount: vi.fn(), | ||
token: undefined, | ||
setToken: vi.fn(), | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should return a function', () => { | ||
const { result } = renderHook(() => | ||
useResetInputs({ from: mockFrom, to: mockTo }), | ||
); | ||
expect(typeof result.current).toBe('function'); | ||
}); | ||
|
||
it('should call refetch on responses and setAmount on both from and to when executed', async () => { | ||
const { result } = renderHook(() => | ||
useResetInputs({ from: mockFrom, to: mockTo }), | ||
); | ||
await act(async () => { | ||
await result.current(); | ||
}); | ||
expect(mockFromTokenResponse.refetch).toHaveBeenCalledTimes(1); | ||
expect(mockToTokenResponse.refetch).toHaveBeenCalledTimes(1); | ||
expect(mockFrom.setAmount).toHaveBeenCalledWith(''); | ||
expect(mockTo.setAmount).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
it("should not create a new function reference if from and to haven't changed", () => { | ||
const { result, rerender } = renderHook(() => | ||
useResetInputs({ from: mockFrom, 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( | ||
({ from, to }) => useResetInputs({ from, to }), | ||
{ initialProps: { from: mockFrom, to: mockTo } }, | ||
); | ||
const firstRender = result.current; | ||
const newMockFrom = { | ||
...mockFrom, | ||
response: { refetch: vi.fn().mockResolvedValue(undefined) }, | ||
}; | ||
rerender({ from: newMockFrom, to: mockTo }); | ||
expect(result.current).not.toBe(firstRender); | ||
}); | ||
|
||
it('should handle null responses gracefully', async () => { | ||
const mockFromWithNullResponse = { ...mockFrom, response: null }; | ||
const mockToWithNullResponse = { ...mockTo, response: null }; | ||
const { result } = renderHook(() => | ||
useResetInputs({ | ||
from: mockFromWithNullResponse, | ||
to: mockToWithNullResponse, | ||
}), | ||
); | ||
await act(async () => { | ||
await result.current(); | ||
}); | ||
expect(mockFromWithNullResponse.setAmount).toHaveBeenCalledWith(''); | ||
expect(mockToWithNullResponse.setAmount).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,14 @@ | ||
import { useCallback } from 'react'; | ||
import type { FromTo } from '../types'; | ||
|
||
// Refreshes balances and inputs post-swap | ||
export const useResetInputs = ({ from, to }: FromTo) => { | ||
return useCallback(async () => { | ||
await Promise.all([ | ||
from.balanceResponse?.refetch(), | ||
to.balanceResponse?.refetch(), | ||
from.setAmount(''), | ||
to.setAmount(''), | ||
]); | ||
}, [from, to]); | ||
}; |
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
Oops, something went wrong.