Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(aptos): improve unit tests #8792

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions libs/ledger-live-common/src/families/aptos/signOperation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import { Observable } from "rxjs";
import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies";
import createTransaction from "./createTransaction";
import signOperation from "./signOperation";
import BigNumber from "bignumber.js";

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

let signTransaction;

jest.mock("./LedgerAccount", () => {
return function () {
return {
init: jest.fn(),
signTransaction,
};
};
});

jest.mock("../../hw/deviceAccess", () => {
return {
withDevice: jest.fn(() => observable => {
return observable(new Observable());
}),
};
});

jest.mock("../../operation", () => {
return {
encodeOperationId: jest.fn(() => "js:2:aptos:0x000"),
};
});

jest.mock("./buildTransaction", () => {
return function () {
return {
sequence_number: "789",
};
};
});

describe("signOperation Test", () => {
beforeEach(() => {
signTransaction = jest.fn(() => "tx");
});

it("should thrown an error", async () => {
signTransaction = () => {
throw new Error("observable-catch-error");
};

const account = createFixtureAccount();
const transaction = createTransaction();

account.id = "js:2:aptos:0x000:";
transaction.mode = "send";

const observable = await signOperation({
account,
deviceId: "1",
transaction,
});

observable.subscribe({
error: err => {
expect(err.message).toBe("observable-catch-error");
},
});
});

it("should return 3 operations", async () => {
const date = new Date("2020-01-01");
jest.useFakeTimers().setSystemTime(date);

const account = createFixtureAccount();
const transaction = createTransaction();

account.id = "js:2:aptos:0x000:";
transaction.mode = "send";

const observable = await signOperation({
account,
deviceId: "1",
transaction,
});

expect(observable).toBeInstanceOf(Observable);

const expectedValues = [
{ type: "device-signature-requested" },
{ type: "device-signature-granted" },
{
type: "signed",
signedOperation: {
operation: {
id: "js:2:aptos:0x000",
hash: "",
type: "OUT",
value: new BigNumber(0),
fee: new BigNumber(0),
extra: {},
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: "js:2:aptos:0x000:",
date,
transactionSequenceNumber: 789,
},
signature: "7478",
},
},
];

let i = 0;

observable.forEach(signOperationEvent => {
expect(signOperationEvent).toEqual(expectedValues[i]);
i++;
});
});

it("should return 3 operations with all amount", async () => {
const date = new Date("2020-01-01");
jest.useFakeTimers().setSystemTime(date);

const account = createFixtureAccount();
const transaction = createTransaction();

account.balance = new BigNumber(40);
transaction.fees = new BigNumber(30);
transaction.useAllAmount = true;

account.id = "js:2:aptos:0x000:";
transaction.mode = "send";

const observable = await signOperation({
account,
deviceId: "1",
transaction,
});

expect(observable).toBeInstanceOf(Observable);

const expectedValues = [
{ type: "device-signature-requested" },
{ type: "device-signature-granted" },
{
type: "signed",
signedOperation: {
operation: {
id: "js:2:aptos:0x000",
hash: "",
type: "OUT",
value: new BigNumber(10),
fee: transaction.fees,
extra: {},
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: "js:2:aptos:0x000:",
date,
transactionSequenceNumber: 789,
},
signature: "7478",
},
},
];

let i = 0;

observable.forEach(signOperationEvent => {
expect(signOperationEvent).toEqual(expectedValues[i]);
i++;
});
});
});
Loading