Skip to content

Commit

Permalink
chore: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteflower committed Feb 25, 2025
1 parent 5e9620f commit 4e3773d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
93 changes: 92 additions & 1 deletion app/components/UI/Swaps/utils/gas.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { getTransaction1559GasFeeEstimates } from './gas';
import { getTransaction1559GasFeeEstimates, getGasFeeEstimatesForTransaction } from './gas';
import {
TransactionParams,
FeeMarketGasFeeEstimates,
GasFeeEstimateType,
} from '@metamask/transaction-controller';
import Engine from '../../../../core/Engine';
import { decGWEIToHexWEI } from '../../../../util/conversions';
import { estimateGasFee } from '../../../../util/transaction-controller';

jest.mock('../../../../util/transaction-controller', () => {
const originalModule = jest.requireActual('../../../../util/transaction-controller');
return {
...originalModule,
estimateGasFee: jest.fn(originalModule.estimateGasFee),
};
});

jest.mock('../../../../core/Engine', () => ({
context: {
Expand All @@ -14,6 +24,10 @@ jest.mock('../../../../core/Engine', () => ({
},
}));

jest.mock('../../../../util/conversions', () => ({
decGWEIToHexWEI: jest.fn().mockReturnValue('123456'),
}));

describe('getTransaction1559GasFeeEstimates', () => {
const mockTransactionParams: TransactionParams = {
from: '0x1',
Expand Down Expand Up @@ -74,3 +88,80 @@ describe('getTransaction1559GasFeeEstimates', () => {
});
});
});

describe('getGasFeeEstimatesForTransaction', () => {
// Mock transaction and gas estimates
const mockTransaction = {
from: '0xfrom',
to: '0xto',
data: '0xdata',
value: '0x0',
gas: '0x5208',
chainId: '0x1' as `0x${string}`,
gasPrice: '0x20',
};

const mockGasEstimates = {
gasPrice: '20',
medium: '50',
};

beforeEach(() => {
jest.clearAllMocks();
});

it('should return EIP1559 gas fee estimates for EIP1559 networks', async () => {
(estimateGasFee as jest.Mock).mockResolvedValue({
estimates: {
high: { maxFeePerGas: '0x1234', maxPriorityFeePerGas: '0x5678' },
},
});

const result = await getGasFeeEstimatesForTransaction(
mockTransaction,
mockGasEstimates,
{ chainId: '0x1', isEIP1559Network: true }
);

// Verify the result
expect(result).toEqual({
maxFeePerGas: '0x1234',
maxPriorityFeePerGas: '0x5678',
});

// Verify that gasPrice is deleted from the transaction
expect(mockTransaction.gasPrice).toBeUndefined();
});

it('should return legacy gas fee estimates for non-EIP1559 networks', async () => {
const result = await getGasFeeEstimatesForTransaction(
mockTransaction,
mockGasEstimates,
{ chainId: '0x1', isEIP1559Network: false }
);

// Verify decGWEIToHexWEI was called with the gasPrice
expect(decGWEIToHexWEI).toHaveBeenCalledWith('20');

// Verify the result
expect(result).toEqual({
gasPrice: '0x123456',
});
});

it('should use medium gas price if gasPrice is not provided for legacy networks', async () => {
const result = await getGasFeeEstimatesForTransaction(
mockTransaction,
{ ...mockGasEstimates, gasPrice: undefined },
{ chainId: '0x1', isEIP1559Network: false }
);

// Verify decGWEIToHexWEI was called with the medium value
expect(decGWEIToHexWEI).toHaveBeenCalledWith('50');

// Verify the result
expect(result).toEqual({
gasPrice: '0x123456',
});
});
});
2 changes: 1 addition & 1 deletion app/components/UI/Swaps/utils/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getTransaction1559GasFeeEstimates(
export async function getGasFeeEstimatesForTransaction(
transaction: Partial<TransactionParams> & { from: string; chainId: string },
gasEstimates: {
gasPrice: string;
gasPrice?: string;
medium: string;
},
{ chainId, isEIP1559Network }: { chainId: Hex; isEIP1559Network: boolean },
Expand Down

0 comments on commit 4e3773d

Please sign in to comment.