Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alissacrane-cb committed Dec 13, 2024
1 parent ca39272 commit dd2d3db
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 4 deletions.
202 changes: 202 additions & 0 deletions src/core/api/getSwapLiteQuote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getSwapLiteQuote } from './getSwapLiteQuote';
import { getSwapQuote } from './getSwapQuote';
import { formatTokenAmount } from '../utils/formatTokenAmount';
import { isSwapError } from '../../swap/utils/isSwapError';
import { Token } from '@/token/types';
import { base } from 'viem/chains';

vi.mock('./getSwapQuote');
vi.mock('../utils/formatTokenAmount');
vi.mock('../../swap/utils/isSwapError');

const toToken: Token = {
name: 'DEGEN',
address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed',
symbol: 'DEGEN',
decimals: 18,
image:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm',
chainId: base.id,
};

const fromToken: Token = {
name: 'ETH',
address: '',
symbol: 'ETH',
decimals: 18,
image:
'https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png',
chainId: base.id,
};

const mockResponse = {
from: fromToken,
to: toToken,
fromAmount: '100000000000000000',
toAmount: '16732157880511600003860',
amountReference: 'from',
priceImpact: '0.07',
chainId: 8453,
hasHighPriceImpact: false,
slippage: '3',
fromAmountUSD: '100',
};

const mockEmptyResponse = {
from: fromToken,
to: toToken,
fromAmount: '',
toAmount: '16732157880511600003860',
amountReference: 'from',
priceImpact: '0.07',
chainId: 8453,
hasHighPriceImpact: false,
slippage: '3',
fromAmountUSD: '',
};

describe('getSwapLiteQuote', () => {
beforeEach(() => {
vi.clearAllMocks();
});

const mockFromSwapUnit = {
setAmountUSD: vi.fn(),
setAmount: vi.fn(),
};

it('should return default values if `from` token is not provided', async () => {
const result = await getSwapLiteQuote({
amount: '1',
amountReference: 'exactIn',
maxSlippage: '0.5',
to: toToken,
useAggregator: true,
fromSwapUnit: mockFromSwapUnit,
});

expect(result).toEqual({
response: undefined,
formattedFromAmount: '',
error: undefined,
});
});

it('should call `getSwapQuote` if `from` and `to` tokens are different', async () => {
(getSwapQuote as jest.Mock).mockResolvedValue(mockResponse);
(isSwapError as jest.Mock).mockReturnValue(false);
(formatTokenAmount as jest.Mock).mockReturnValue('1.0');

const result = await getSwapLiteQuote({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
maxSlippage: '0.5',
to: toToken,
useAggregator: true,
fromSwapUnit: mockFromSwapUnit,
});

expect(getSwapQuote).toHaveBeenCalledWith({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
maxSlippage: '0.5',
to: toToken,
useAggregator: true,
});

expect(formatTokenAmount).toHaveBeenCalledWith('100000000000000000', 18);
expect(mockFromSwapUnit.setAmountUSD).toHaveBeenCalledWith('100');
expect(mockFromSwapUnit.setAmount).toHaveBeenCalledWith('1.0');

expect(result).toEqual({
response: mockResponse,
formattedFromAmount: '1.0',
error: undefined,
});
});

it('should handle case where amount values are undefined', async () => {
(getSwapQuote as jest.Mock).mockResolvedValue(mockEmptyResponse);
(isSwapError as jest.Mock).mockReturnValue(false);
(formatTokenAmount as jest.Mock).mockReturnValue('1.0');

const result = await getSwapLiteQuote({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
maxSlippage: '0.5',
to: toToken,
useAggregator: true,
fromSwapUnit: mockFromSwapUnit,
});

expect(getSwapQuote).toHaveBeenCalledWith({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
maxSlippage: '0.5',
to: toToken,
useAggregator: true,
});

expect(formatTokenAmount).not.toHaveBeenCalled();
expect(mockFromSwapUnit.setAmountUSD).toHaveBeenCalledWith('');
expect(mockFromSwapUnit.setAmount).toHaveBeenCalledWith('');

expect(result).toEqual({
response: mockEmptyResponse,
formattedFromAmount: '',
error: undefined,
});
});

it('should handle swap errors correctly', async () => {
const mockError = {
code: 'UNCAUGHT_SWAP_QUOTE_ERROR',
error: 'Something went wrong',
message: '',
};

(getSwapQuote as jest.Mock).mockResolvedValue(mockError);
(isSwapError as jest.Mock).mockReturnValue(true);

const result = await getSwapLiteQuote({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
maxSlippage: '0.5',
to: toToken,
useAggregator: true,
fromSwapUnit: mockFromSwapUnit,
});

expect(isSwapError).toHaveBeenCalledWith(mockError);
expect(result).toEqual({
response: undefined,
formattedFromAmount: '',
error: mockError,
});
});

it('should not call `getSwapQuote` if `from` and `to` tokens are the same', async () => {
const result = await getSwapLiteQuote({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
maxSlippage: '0.5',
to: fromToken,
useAggregator: true,
fromSwapUnit: mockFromSwapUnit,
});

expect(getSwapQuote).not.toHaveBeenCalled();
expect(result).toEqual({
response: undefined,
formattedFromAmount: '',
error: undefined,
});
});
});
11 changes: 7 additions & 4 deletions src/core/api/getSwapLiteQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type GetSwapLiteQuoteParams = Omit<GetSwapQuoteParams, 'from'> & {
from?: Token;
};

/**
* Fetches a quote for a swap, but only if the from and to tokens are different.
*/

export async function getSwapLiteQuote({
amount,
amountReference,
Expand Down Expand Up @@ -49,10 +53,9 @@ export async function getSwapLiteQuote({

let formattedFromAmount = '';
if (response && !isSwapError(response)) {
formattedFromAmount = formatTokenAmount(
response.fromAmount,
response.from.decimals,
);
formattedFromAmount = response?.fromAmount
? formatTokenAmount(response.fromAmount, response.from.decimals)
: '';

fromSwapUnit.setAmountUSD(response?.fromAmountUSD || '');
fromSwapUnit.setAmount(formattedFromAmount || '');
Expand Down

0 comments on commit dd2d3db

Please sign in to comment.