Skip to content

Commit

Permalink
test(aptos): improvement to unit tests (#8779)
Browse files Browse the repository at this point in the history
* test(aptos): improve unit tests
  • Loading branch information
jccguimaraes authored Jan 6, 2025
1 parent f39521b commit 940b324
Show file tree
Hide file tree
Showing 6 changed files with 416 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies";
import createTransaction from "./createTransaction";
import buildTransaction from "./buildTransaction";
import { AptosAPI } from "./api";
import { normalizeTransactionOptions } from "./logic";
import { InputEntryFunctionData } from "@aptos-labs/ts-sdk";
import { TransactionOptions } from "./types";

const generateTransaction = jest.fn(() => "tx");

jest.mock("./logic", () => ({
normalizeTransactionOptions: jest.fn(() => ({
maxGasAmount: "100",
gasUnitPrice: "200",
})),
DEFAULT_GAS: 100,
DEFAULT_GAS_PRICE: 200,
}));

jest.mock("./api", () => {
return {
AptosAPI: function () {
return {
generateTransaction,
};
},
};
});

describe("buildTransaction Test", () => {
it("should return tx", async () => {
const account = createFixtureAccount();
const transaction = createTransaction();
const aptosClient = new AptosAPI(account.currency.id);
const result = await buildTransaction(account, transaction, aptosClient);

const expected = "tx";

expect(result).toBe(expected);

const mockedNormalizeTransactionOptions = jest.mocked(normalizeTransactionOptions);

expect(mockedNormalizeTransactionOptions).toHaveBeenCalledTimes(1);
expect(generateTransaction).toHaveBeenCalledTimes(1);

const generateTransactionArgs: [string, InputEntryFunctionData, TransactionOptions][] =
generateTransaction.mock.calls[0];

expect(mockedNormalizeTransactionOptions.mock.calls[0][0]).toEqual({
maxGasAmount: "100",
gasUnitPrice: "200",
});

expect(generateTransactionArgs[0]).toBe("0x01");
expect(generateTransactionArgs[1]).toEqual({
function: "0x1::aptos_account::transfer_coins",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: ["", "0"],
});
expect(generateTransactionArgs[2]).toEqual({ maxGasAmount: "100", gasUnitPrice: "200" });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import BigNumber from "bignumber.js";
import createTransaction from "./createTransaction";

jest.mock("./logic", () => ({
DEFAULT_GAS: 100,
DEFAULT_GAS_PRICE: 200,
}));

describe("createTransaction Test", () => {
it("should return a transaction object", async () => {
const result = createTransaction();

const expected = {
family: "aptos",
mode: "send",
amount: BigNumber(0),
recipient: "",
useAllAmount: false,
firstEmulation: true,
options: {
maxGasAmount: "100",
gasUnitPrice: "200",
},
estimate: {
maxGasAmount: "100",
gasUnitPrice: "200",
},
};

expect(result).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies";
import createTransaction from "./createTransaction";
import estimateMaxSpendable from "./estimateMaxSpendable";
import BigNumber from "bignumber.js";

jest.mock("./getFeesForTransaction", () => ({
getEstimatedGas: jest.fn(() => ({
fees: new BigNumber(0),
estimate: {
maxGasAmount: 1,
gasUnitPrice: 2,
sequenceNumber: "",
expirationTimestampSecs: "",
},
errors: {},
})),
}));

describe("estimateMaxSpendable Test", () => {
describe("spendable balance is lower than the total gas", () => {
it("should return 0", async () => {
const account = createFixtureAccount();

const spendableBalance = new BigNumber(0);

account.spendableBalance = spendableBalance;

const result = await estimateMaxSpendable({
account,
});

const expected = spendableBalance;

expect(result.isEqualTo(expected)).toBe(true);
});
});

describe("spendable balance is higher than the total gas", () => {
it("should return spendable amount minus total gas", async () => {
const account = createFixtureAccount();

const spendableBalance = new BigNumber(100000);

account.spendableBalance = spendableBalance;

const result = await estimateMaxSpendable({
account,
});

const expected = new BigNumber(80000);

expect(result.isEqualTo(expected)).toBe(true);
});
});

describe("transaction spendable balance is higher than the total gas", () => {
it("should return transaction spendable amount minus total gas", async () => {
const account = createFixtureAccount();
const transaction = createTransaction();

const spendableBalance = new BigNumber(1);

account.spendableBalance = spendableBalance;

const result = await estimateMaxSpendable({
account,
parentAccount: account,
transaction,
});

const expected = new BigNumber(0);

expect(result.isEqualTo(expected)).toBe(true);
});
});

describe("transaction spendable balance is higher than the total gas", () => {
it("should return transaction spendable amount minus total gas", async () => {
const account = createFixtureAccount();
const transaction = createTransaction();

const spendableBalance = new BigNumber(100000);

account.spendableBalance = spendableBalance;

const result = await estimateMaxSpendable({
account,
parentAccount: account,
transaction,
});

const expected = new BigNumber(99998);

expect(result.isEqualTo(expected)).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ export const getEstimatedGas = async (
): Promise<IGetEstimatedGasReturnType> => {
const key = getCacheKey(transaction);

if (CACHE.has(key)) {
return CACHE.get(key);
if (!CACHE.has(key)) {
CACHE.set(key, await getFee(account, transaction, aptosClient));
}

CACHE.set(key, await getFee(account, transaction, aptosClient));

return CACHE.get(key);
};
Loading

0 comments on commit 940b324

Please sign in to comment.