-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: the default chainId is picked from
defaultChainId
(#281)
* check provider for default check * check priority wallet * group default input token * default the chain id to mainnet * switch behavior on connector change * output token defaults and first test * initial render tests * add tests and update eslint * add default chain id to default * style(lint): lint action with ESLint * update tests with defaultChainId on TokenDefaults * static import and revert eslint * cast type for type check pass * useTokens to accept chain id * skip network * update param name * update switch var names * no default for setToDefaults * define skipNetwork * pull in supported chain id Co-authored-by: Lint Action <lint-action@uniswap.org>
- Loading branch information
1 parent
9ebc588
commit 5479440
Showing
5 changed files
with
215 additions
and
48 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,127 @@ | ||
import { TradeType } from '@uniswap/sdk-core' | ||
import { SupportedChainId } from 'constants/chains' | ||
import { DAI_POLYGON, nativeOnChain } from 'constants/tokens' | ||
import { useAtomValue } from 'jotai/utils' | ||
import { Field, stateAtom, Swap, swapAtom } from 'state/swap' | ||
import { renderHook } from 'test' | ||
|
||
import { USDC_MAINNET } from '../../constants/tokens' | ||
import useSyncTokenDefaults, { TokenDefaults } from './useSyncTokenDefaults' | ||
|
||
const MOCK_DAI_POLYGON = DAI_POLYGON | ||
const MOCK_USDC_MAINNET = USDC_MAINNET | ||
|
||
const INITIAL_SWAP: Swap = { | ||
type: TradeType.EXACT_INPUT, | ||
amount: '10', | ||
[Field.INPUT]: MOCK_USDC_MAINNET, | ||
[Field.OUTPUT]: MOCK_DAI_POLYGON, | ||
} | ||
|
||
const TOKEN_DEFAULTS: TokenDefaults = { | ||
defaultInputAmount: 10, | ||
defaultInputTokenAddress: 'NATIVE', | ||
defaultOutputTokenAddress: 'NATIVE', | ||
} | ||
|
||
jest.mock('@web3-react/core', () => { | ||
const { SupportedChainId } = jest.requireActual('constants/chains') | ||
|
||
return { | ||
useWeb3React: () => ({ | ||
chainId: SupportedChainId.MAINNET, | ||
connector: {}, | ||
}), | ||
} | ||
}) | ||
|
||
jest.mock('../useTokenList', () => { | ||
return { | ||
useIsTokenListLoaded: () => true, | ||
} | ||
}) | ||
|
||
jest.mock('hooks/useCurrency', () => { | ||
return { | ||
useToken: () => MOCK_DAI_POLYGON, | ||
} | ||
}) | ||
|
||
describe('useSyncTokenDefaults', () => { | ||
it('syncs to default chainId on initial render if defaultChainId is provided', () => { | ||
const { rerender } = renderHook( | ||
() => { | ||
useSyncTokenDefaults({ ...TOKEN_DEFAULTS, defaultChainId: SupportedChainId.POLYGON }) | ||
}, | ||
{ | ||
initialAtomValues: [[stateAtom, INITIAL_SWAP]], | ||
} | ||
) | ||
|
||
const { result } = rerender(() => useAtomValue(swapAtom)) | ||
expect(result.current).toMatchObject({ | ||
...INITIAL_SWAP, | ||
INPUT: nativeOnChain(SupportedChainId.POLYGON), | ||
OUTPUT: nativeOnChain(SupportedChainId.POLYGON), | ||
}) | ||
}) | ||
|
||
it('does not sync to default chainId on initial render if defaultChainId is not provided', () => { | ||
const { rerender } = renderHook( | ||
() => { | ||
useSyncTokenDefaults(TOKEN_DEFAULTS) | ||
}, | ||
{ | ||
initialAtomValues: [[stateAtom, INITIAL_SWAP]], | ||
} | ||
) | ||
|
||
const { result } = rerender(() => useAtomValue(swapAtom)) | ||
expect(result.current).toMatchObject({ | ||
...INITIAL_SWAP, | ||
INPUT: nativeOnChain(SupportedChainId.MAINNET), | ||
OUTPUT: nativeOnChain(SupportedChainId.MAINNET), | ||
}) | ||
}) | ||
|
||
it('syncs to default non NATIVE tokens of default chainId on initial render if defaultChainId is provided', () => { | ||
const { rerender } = renderHook( | ||
() => { | ||
useSyncTokenDefaults({ | ||
...TOKEN_DEFAULTS, | ||
defaultInputTokenAddress: DAI_POLYGON.address, | ||
defaultOutputTokenAddress: DAI_POLYGON.address, | ||
defaultChainId: SupportedChainId.POLYGON, | ||
}) | ||
}, | ||
{ | ||
initialAtomValues: [[stateAtom, INITIAL_SWAP]], | ||
} | ||
) | ||
|
||
const { result } = rerender(() => useAtomValue(swapAtom)) | ||
expect(result.current).toMatchObject({ | ||
...INITIAL_SWAP, | ||
INPUT: DAI_POLYGON, | ||
OUTPUT: DAI_POLYGON, | ||
}) | ||
}) | ||
|
||
it('syncs to non NATIVE tokens of chainId on initial render if defaultChainId is not provided', () => { | ||
const { rerender } = renderHook( | ||
() => { | ||
useSyncTokenDefaults(TOKEN_DEFAULTS) | ||
}, | ||
{ | ||
initialAtomValues: [[stateAtom, INITIAL_SWAP]], | ||
} | ||
) | ||
|
||
const { result } = rerender(() => useAtomValue(swapAtom)) | ||
expect(result.current).toMatchObject({ | ||
...INITIAL_SWAP, | ||
INPUT: nativeOnChain(SupportedChainId.MAINNET), | ||
OUTPUT: nativeOnChain(SupportedChainId.MAINNET), | ||
}) | ||
}) | ||
}) |
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
5479440
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
widgets – ./
widgets-git-main-uniswap.vercel.app
widgets-uniswap.vercel.app
widgets-seven-tau.vercel.app