Skip to content

Commit

Permalink
Add utility test
Browse files Browse the repository at this point in the history
  • Loading branch information
OGPoyraz committed Dec 12, 2024
1 parent 7b160db commit fcc975e
Showing 1 changed file with 106 additions and 1 deletion.
107 changes: 106 additions & 1 deletion ui/pages/confirmations/components/confirm/info/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { TransactionMeta } from '@metamask/transaction-controller';
import { toHex } from '@metamask/controller-utils';
import { DecodedTransactionDataSource } from '../../../../../../shared/types/transaction-decode';
import { getIsRevokeSetApprovalForAll } from './utils';
import {
getIsRevokeSetApprovalForAll,
hasValueAndNativeBalanceMismatch,
} from './utils';

describe('getIsRevokeSetApprovalForAll', () => {
it('returns false if no data is passed as an argument', () => {
Expand Down Expand Up @@ -36,3 +41,103 @@ describe('getIsRevokeSetApprovalForAll', () => {
expect(actual).toEqual(true);
});
});

describe('hasValueAndNativeBalanceMismatch', () => {
it('returns false when transaction value matches simulated balance change', () => {
const transactionValueInDecimal = 10000000000000000;
const transactionValueInHex = toHex(transactionValueInDecimal);

const transaction = {
txParams: {
value: transactionValueInHex,
},
simulationData: {
nativeBalanceChange: {
difference: transactionValueInHex,
isDecrease: true,
},
},
} as unknown as TransactionMeta;

expect(hasValueAndNativeBalanceMismatch(transaction)).toBe(false);
});

it('returns false when values differ within threshold', () => {
const transactionValueInDecimal = 10000000000000000;
const transactionValueInHex = toHex(transactionValueInDecimal);

const differenceInDecimal = 10400000000000000;
const differenceInHex = toHex(differenceInDecimal);

const transaction = {
txParams: {
value: transactionValueInHex,
},
simulationData: {
nativeBalanceChange: {
difference: differenceInHex,
isDecrease: true,
},
},
} as unknown as TransactionMeta;

expect(hasValueAndNativeBalanceMismatch(transaction)).toBe(false);
});

it('returns true when values differ beyond threshold', () => {
const transactionValueInDecimal = 10000000000000000;
const transactionValueInHex = toHex(transactionValueInDecimal);

const differenceInDecimal = 1000000000;
const muchSmallerDifferenceInHex = toHex(differenceInDecimal);

const transaction = {
txParams: {
value: transactionValueInHex,
},
simulationData: {
nativeBalanceChange: {
difference: muchSmallerDifferenceInHex,
isDecrease: true,
},
},
} as unknown as TransactionMeta;

expect(hasValueAndNativeBalanceMismatch(transaction)).toBe(true);
});

it('returns true when no simulation data is present', () => {
const transactionValueInDecimal = 10000000000000000;
const transactionValueInHex = toHex(transactionValueInDecimal);

const transaction = {
txParams: {
value: transactionValueInHex,
},
} as unknown as TransactionMeta;

expect(hasValueAndNativeBalanceMismatch(transaction)).toBe(true);
});

it('handles case when value is increased in simulation', () => {
const transactionValueInDecimal = 10000000000000000;
const transactionValueInHex = toHex(transactionValueInDecimal);

const differenceInDecimal = 10000000000000000;
const differenceInHex = toHex(differenceInDecimal);

const transaction = {
txParams: {
value: transactionValueInHex,
},
simulationData: {
nativeBalanceChange: {
difference: differenceInHex,
isDecrease: false,
},
},
} as unknown as TransactionMeta;

expect(hasValueAndNativeBalanceMismatch(transaction)).toBe(true);
});
});

0 comments on commit fcc975e

Please sign in to comment.