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'
|
|
|
|
import { V3TradeState } from 'state/routing/types'
|
|
|
|
import { useRoutingAPIEnabled } from 'state/user/hooks'
|
2021-09-22 02:21:28 +03:00
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
import { useRoutingAPITrade } from '../state/routing/useRoutingAPITrade'
|
|
|
|
import { useBestV3Trade } from './useBestV3Trade'
|
|
|
|
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')
|
|
|
|
|
|
|
|
jest.mock('./useDebounce')
|
|
|
|
const mockUseDebounce = useDebounce as jest.MockedFunction<typeof useDebounce>
|
|
|
|
|
|
|
|
// mock modules containing hooks
|
|
|
|
jest.mock('state/routing/useRoutingAPITrade')
|
|
|
|
jest.mock('./useClientSideV3Trade')
|
|
|
|
jest.mock('state/user/hooks')
|
|
|
|
jest.mock('./useIsWindowVisible')
|
|
|
|
|
|
|
|
const mockUseRoutingAPIEnabled = useRoutingAPIEnabled as jest.MockedFunction<typeof useRoutingAPIEnabled>
|
|
|
|
const mockUseIsWindowVisible = useIsWindowVisible as jest.MockedFunction<typeof useIsWindowVisible>
|
|
|
|
|
|
|
|
// useRouterTrade mocks
|
2021-09-21 20:51:50 +03:00
|
|
|
const mockUseRoutingAPITrade = useRoutingAPITrade as jest.MockedFunction<typeof useRoutingAPITrade>
|
2021-09-16 17:50:58 +03:00
|
|
|
|
|
|
|
// useClientSideV3Trade mocks
|
2021-09-21 20:51:50 +03:00
|
|
|
const mockUseClientSideV3Trade = useClientSideV3Trade as jest.MockedFunction<typeof useClientSideV3Trade>
|
2021-09-16 17:50:58 +03:00
|
|
|
|
|
|
|
// helpers to set mock expectations
|
|
|
|
const expectRouterMock = (state: V3TradeState) => {
|
2021-09-21 20:51:50 +03:00
|
|
|
mockUseRoutingAPITrade.mockReturnValue({ state, trade: null })
|
2021-09-16 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const expectClientSideMock = (state: V3TradeState) => {
|
2021-09-21 20:51:50 +03:00
|
|
|
mockUseClientSideV3Trade.mockReturnValue({ state, trade: null })
|
2021-09-16 17:50:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// ignore debounced value
|
|
|
|
mockUseDebounce.mockImplementation((value) => value)
|
|
|
|
|
|
|
|
mockUseIsWindowVisible.mockReturnValue(true)
|
|
|
|
mockUseRoutingAPIEnabled.mockReturnValue(true)
|
|
|
|
})
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
describe('#useBestV3TradeExactIn', () => {
|
2021-09-16 17:50:58 +03:00
|
|
|
it('does not compute routing api trade when routing API is disabled', () => {
|
|
|
|
mockUseRoutingAPIEnabled.mockReturnValue(false)
|
|
|
|
expectRouterMock(V3TradeState.INVALID)
|
|
|
|
expectClientSideMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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-09-16 17:50:58 +03:00
|
|
|
expect(result.current).toEqual({ state: V3TradeState.VALID, trade: null })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute routing api trade when window is not focused', () => {
|
|
|
|
mockUseIsWindowVisible.mockReturnValue(false)
|
|
|
|
expectRouterMock(V3TradeState.NO_ROUTE_FOUND)
|
|
|
|
expectClientSideMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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-09-16 17:50:58 +03:00
|
|
|
expect(result.current).toEqual({ state: V3TradeState.VALID, trade: null })
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when routing api is in non-error state', () => {
|
|
|
|
it('does not compute client side v3 trade if routing api is LOADING', () => {
|
|
|
|
expectRouterMock(V3TradeState.LOADING)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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
|
|
|
expect(result.current).toEqual({ state: V3TradeState.LOADING, trade: null })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute client side v3 trade if routing api is VALID', () => {
|
|
|
|
expectRouterMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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
|
|
|
expect(result.current).toEqual({ state: V3TradeState.VALID, trade: null })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute client side v3 trade if routing api is SYNCING', () => {
|
|
|
|
expectRouterMock(V3TradeState.SYNCING)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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
|
|
|
expect(result.current).toEqual({ state: V3TradeState.SYNCING, trade: null })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when routing api is in error state', () => {
|
|
|
|
it('does not compute client side v3 trade if routing api is INVALID', () => {
|
|
|
|
expectRouterMock(V3TradeState.INVALID)
|
|
|
|
expectClientSideMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
renderHook(() => useBestV3Trade(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', () => {
|
|
|
|
expectRouterMock(V3TradeState.NO_ROUTE_FOUND)
|
|
|
|
expectClientSideMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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-09-16 17:50:58 +03:00
|
|
|
expect(result.current).toEqual({ state: V3TradeState.VALID, trade: null })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
describe('#useBestV3TradeExactOut', () => {
|
2021-09-16 17:50:58 +03:00
|
|
|
it('does not compute routing api trade when routing API is disabled', () => {
|
|
|
|
mockUseRoutingAPIEnabled.mockReturnValue(false)
|
|
|
|
expectRouterMock(V3TradeState.INVALID)
|
|
|
|
expectClientSideMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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-09-16 17:50:58 +03:00
|
|
|
expect(result.current).toEqual({ state: V3TradeState.VALID, trade: null })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute routing api trade when window is not focused', () => {
|
|
|
|
mockUseIsWindowVisible.mockReturnValue(false)
|
|
|
|
expectRouterMock(V3TradeState.NO_ROUTE_FOUND)
|
|
|
|
expectClientSideMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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-09-16 17:50:58 +03:00
|
|
|
expect(result.current).toEqual({ state: V3TradeState.VALID, trade: null })
|
|
|
|
})
|
|
|
|
describe('when routing api is in non-error state', () => {
|
|
|
|
it('does not compute client side v3 trade if routing api is LOADING', () => {
|
|
|
|
expectRouterMock(V3TradeState.LOADING)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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
|
|
|
expect(result.current).toEqual({ state: V3TradeState.LOADING, trade: null })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute client side v3 trade if routing api is VALID', () => {
|
|
|
|
expectRouterMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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
|
|
|
expect(result.current).toEqual({ state: V3TradeState.VALID, trade: null })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not compute client side v3 trade if routing api is SYNCING', () => {
|
|
|
|
expectRouterMock(V3TradeState.SYNCING)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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
|
|
|
expect(result.current).toEqual({ state: V3TradeState.SYNCING, trade: null })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when routing api is in error state', () => {
|
|
|
|
it('computes client side v3 trade if routing api is INVALID', () => {
|
|
|
|
expectRouterMock(V3TradeState.INVALID)
|
|
|
|
expectClientSideMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
renderHook(() => useBestV3Trade(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', () => {
|
|
|
|
expectRouterMock(V3TradeState.NO_ROUTE_FOUND)
|
|
|
|
expectClientSideMock(V3TradeState.VALID)
|
|
|
|
|
2021-09-21 20:51:50 +03:00
|
|
|
const { result } = renderHook(() => useBestV3Trade(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-09-16 17:50:58 +03:00
|
|
|
expect(result.current).toEqual({ state: V3TradeState.VALID, trade: null })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|