-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(aptos): improvement to unit tests (#8779)
* test(aptos): improve unit tests
- Loading branch information
1 parent
f39521b
commit 940b324
Showing
6 changed files
with
416 additions
and
11 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
libs/ledger-live-common/src/families/aptos/createTransaction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.