2021-09-16 17:50:58 +03:00
|
|
|
import { renderHook } from '@testing-library/react-hooks'
|
2021-09-21 20:51:50 +03:00
|
|
|
import { CurrencyAmount, TradeType } from '@uniswap/sdk-core'
|
2021-09-16 17:50:58 +03:00
|
|
|
import { DAI, USDC } from 'constants/tokens'
|
2021-12-16 22:44:03 +03:00
|
|
|
import { TradeState } from 'state/routing/types'
|
2021-09-22 02:21:28 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
import { useRoutingAPITrade } from '../state/routing/useRoutingAPITrade'
|
2021-12-16 22:44:03 +03:00
|
|
|
import useAutoRouterSupported from './useAutoRouterSupported'
|
|
|
|
import { useBestTrade } from './useBestTrade'
|
2021-09-21 20:51:50 +03:00
|
|
|
import { useClientSideV3Trade } from './useClientSideV3Trade'
|
2021-09-16 17:50:58 +03:00
|
|
|
import useDebounce from './useDebounce'
|
|
|
|
import useIsWindowVisible from './useIsWindowVisible'
|
|
|
|
|
|
|
|
const USDCAmount = CurrencyAmount.fromRawAmount(USDC, '10000')
|
|
|
|
const DAIAmount = CurrencyAmount.fromRawAmount(DAI, '10000')
|
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
jest.mock('./useAutoRouterSupported')
|
|
|
|
jest.mock('./useClientSideV3Trade')
|
2021-09-16 17:50:58 +03:00
|
|
|
jest.mock('./useDebounce')
|
2021-12-16 22:44:03 +03:00
|
|
|
jest.mock('./useIsWindowVisible')
|
2021-09-16 17:50:58 +03:00
|
|
|
jest.mock('state/routing/useRoutingAPITrade')
|
|
|
|
jest.mock('state/user/hooks')
|
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const mockUseDebounce = useDebounce as jest.MockedFunction<typeof useDebounce>
|
|
|
|
const mockUseAutoRouterSupported = useAutoRouterSupported as jest.MockedFunction<typeof useAutoRouterSupported>
|
2021-09-16 17:50:58 +03:00
|
|
|
const mockUseIsWindowVisible = useIsWindowVisible as jest.MockedFunction<typeof useIsWindowVisible>
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const mockUseRoutingAPITrade = useRoutingAPITrade as jest.MockedFunction<typeof useRoutingAPITrade>
|
|
|
|
const mockUseClientSideV3Trade = useClientSideV3Trade as jest.MockedFunction<typeof useClientSideV3Trade>
|
2021-09-16 17:50:58 +03:00
|
|
|
|
|
|
|
// helpers to set mock expectations
|
2021-12-16 22:44:03 +03:00
|
|
|
const expectRouterMock = (state: TradeState) => {
|
|
|
|
mockUseRoutingAPITrade.mockReturnValue({ state, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const expectClientSideMock = (state: TradeState) => {
|
|
|
|
mockUseClientSideV3Trade.mockReturnValue({ state, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// ignore debounced value
|
|
|
|
mockUseDebounce.mockImplementation((value) => value)
|
|
|
|
|
|
|
|
mockUseIsWindowVisible.mockReturnValue(true)
|
2021-12-16 22:44:03 +03:00
|
|
|
mockUseAutoRouterSupported.mockReturnValue(true)
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
describe('#useBestV3Trade ExactIn', () => {
|
|
|
|
it('does not compute routing api trade when routing API is not supported', () => {
|
|
|
|
mockUseAutoRouterSupported.mockReturnValue(false)
|
|
|
|
expectRouterMock(TradeState.INVALID)
|
|
|
|
expectClientSideMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_INPUT, USDCAmount, DAI))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseRoutingAPITrade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, undefined, DAI)
|
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, USDCAmount, DAI)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.VALID, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute routing api trade when window is not focused', () => {
|
|
|
|
mockUseIsWindowVisible.mockReturnValue(false)
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.NO_ROUTE_FOUND)
|
|
|
|
expectClientSideMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_INPUT, USDCAmount, DAI))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseRoutingAPITrade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, undefined, DAI)
|
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, USDCAmount, DAI)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.VALID, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('when routing api is in non-error state', () => {
|
|
|
|
it('does not compute client side v3 trade if routing api is LOADING', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.LOADING)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_INPUT, USDCAmount, DAI))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, undefined, undefined)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.LOADING, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute client side v3 trade if routing api is VALID', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_INPUT, USDCAmount, DAI))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, undefined, undefined)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.VALID, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute client side v3 trade if routing api is SYNCING', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.SYNCING)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_INPUT, USDCAmount, DAI))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, undefined, undefined)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.SYNCING, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when routing api is in error state', () => {
|
|
|
|
it('does not compute client side v3 trade if routing api is INVALID', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.INVALID)
|
|
|
|
expectClientSideMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
renderHook(() => useBestTrade(TradeType.EXACT_INPUT, USDCAmount, DAI))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, undefined, undefined)
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('computes client side v3 trade if routing api is NO_ROUTE_FOUND', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.NO_ROUTE_FOUND)
|
|
|
|
expectClientSideMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_INPUT, USDCAmount, DAI))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_INPUT, USDCAmount, DAI)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.VALID, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
describe('#useBestV3Trade ExactOut', () => {
|
|
|
|
it('does not compute routing api trade when routing API is not supported', () => {
|
|
|
|
mockUseAutoRouterSupported.mockReturnValue(false)
|
|
|
|
expectRouterMock(TradeState.INVALID)
|
|
|
|
expectClientSideMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_OUTPUT, DAIAmount, USDC))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseRoutingAPITrade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, undefined, USDC)
|
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, DAIAmount, USDC)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.VALID, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute routing api trade when window is not focused', () => {
|
|
|
|
mockUseIsWindowVisible.mockReturnValue(false)
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.NO_ROUTE_FOUND)
|
|
|
|
expectClientSideMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_OUTPUT, DAIAmount, USDC))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseRoutingAPITrade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, undefined, USDC)
|
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, DAIAmount, USDC)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.VALID, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
describe('when routing api is in non-error state', () => {
|
|
|
|
it('does not compute client side v3 trade if routing api is LOADING', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.LOADING)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_OUTPUT, DAIAmount, USDC))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, undefined, undefined)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.LOADING, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute client side v3 trade if routing api is VALID', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_OUTPUT, DAIAmount, USDC))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, undefined, undefined)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.VALID, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute client side v3 trade if routing api is SYNCING', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.SYNCING)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_OUTPUT, DAIAmount, USDC))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, undefined, undefined)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.SYNCING, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when routing api is in error state', () => {
|
|
|
|
it('computes client side v3 trade if routing api is INVALID', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.INVALID)
|
|
|
|
expectClientSideMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
renderHook(() => useBestTrade(TradeType.EXACT_OUTPUT, DAIAmount, USDC))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, undefined, undefined)
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('computes client side v3 trade if routing api is NO_ROUTE_FOUND', () => {
|
2021-12-16 22:44:03 +03:00
|
|
|
expectRouterMock(TradeState.NO_ROUTE_FOUND)
|
|
|
|
expectClientSideMock(TradeState.VALID)
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-12-16 22:44:03 +03:00
|
|
|
const { result } = renderHook(() => useBestTrade(TradeType.EXACT_OUTPUT, DAIAmount, USDC))
|
2021-09-16 17:50:58 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
expect(mockUseClientSideV3Trade).toHaveBeenCalledWith(TradeType.EXACT_OUTPUT, DAIAmount, USDC)
|
2021-12-16 22:44:03 +03:00
|
|
|
expect(result.current).toEqual({ state: TradeState.VALID, trade: undefined })
|
2021-09-16 17:50:58 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|