@ledgerhq/coin-mina
Version:
138 lines • 7.05 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const errors_2 = require("./errors");
const getTransactionStatus_1 = __importDefault(require("./getTransactionStatus"));
const fixtures_1 = require("../test/fixtures");
const currencies_1 = require("@ledgerhq/coin-framework/currencies");
const getEstimatedFees_1 = __importDefault(require("./getEstimatedFees"));
// Mock dependencies
jest.mock("@ledgerhq/coin-framework/currencies", () => ({
formatCurrencyUnit: jest.fn().mockReturnValue("0.1 MINA"),
}));
jest.mock("./getEstimatedFees");
describe("getTransactionStatus", () => {
// Standard test fixtures
const mockAccount = (0, fixtures_1.createMockAccount)();
const mockTransaction = (0, fixtures_1.createMockTransaction)({ amount: new bignumber_js_1.BigNumber(800) });
beforeEach(() => {
jest.clearAllMocks();
});
it("should return a valid transaction status with no errors for valid inputs", async () => {
const result = await (0, getTransactionStatus_1.default)(mockAccount, mockTransaction);
expect(result.errors).toEqual({});
expect(result.warnings).toEqual({});
expect(result.estimatedFees).toEqual(new bignumber_js_1.BigNumber(10));
expect(result.amount).toEqual(new bignumber_js_1.BigNumber(800));
expect(result.totalSpent).toEqual(new bignumber_js_1.BigNumber(810)); // amount + fee
});
it("should handle missing fees with FeeNotLoaded error", async () => {
getEstimatedFees_1.default.mockResolvedValue({
fee: new bignumber_js_1.BigNumber(10),
accountCreationFee: new bignumber_js_1.BigNumber(0),
});
// Using Type casting to handle the null fees for testing purposes
const txWithoutFees = {
...mockTransaction,
fees: {
fee: new bignumber_js_1.BigNumber(0),
accountCreationFee: new bignumber_js_1.BigNumber(0),
},
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithoutFees);
expect(result.errors.fees).toBeInstanceOf(errors_1.FeeNotLoaded);
});
it("should handle missing recipient with RecipientRequired error", async () => {
const txWithoutRecipient = {
...mockTransaction,
recipient: "",
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithoutRecipient);
expect(result.errors.recipient).toBeInstanceOf(errors_1.RecipientRequired);
});
it("should handle invalid recipient address with InvalidAddress error", async () => {
const txWithInvalidRecipient = {
...mockTransaction,
recipient: "invalid-address",
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithInvalidRecipient);
expect(result.errors.recipient).toBeInstanceOf(errors_1.InvalidAddress);
});
it("should handle self-transfer with InvalidAddressBecauseDestinationIsAlsoSource error", async () => {
const txWithSelfTransfer = {
...mockTransaction,
recipient: mockAccount.freshAddress,
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithSelfTransfer);
expect(result.errors.recipient).toBeInstanceOf(errors_1.InvalidAddressBecauseDestinationIsAlsoSource);
});
it("should handle invalid memo with InvalidMemoMina error", async () => {
const veryLongMemo = "a".repeat(33); // Create memo longer than MAX_MEMO_LENGTH (32)
const txWithInvalidMemo = {
...mockTransaction,
memo: veryLongMemo,
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithInvalidMemo);
expect(result.errors.transaction).toBeInstanceOf(errors_2.InvalidMemoMina);
});
it("should add AccountCreationFeeWarning when account creation fee is present", async () => {
const txWithAccountCreationFee = {
...mockTransaction,
fees: {
fee: new bignumber_js_1.BigNumber(10),
accountCreationFee: new bignumber_js_1.BigNumber(100),
},
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithAccountCreationFee);
expect(result.warnings.recipient).toBeInstanceOf(errors_2.AccountCreationFeeWarning);
expect(currencies_1.formatCurrencyUnit).toHaveBeenCalled();
});
it("should add AmountTooSmall error when amount is less than account creation fee", async () => {
const txWithSmallAmount = {
...mockTransaction,
amount: new bignumber_js_1.BigNumber(50),
fees: {
fee: new bignumber_js_1.BigNumber(10),
accountCreationFee: new bignumber_js_1.BigNumber(100),
},
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithSmallAmount);
expect(result.errors.amount).toBeInstanceOf(errors_2.AmountTooSmall);
});
it("should handle zero amount with AmountRequired error when useAllAmount is false", async () => {
const txWithZeroAmount = {
...mockTransaction,
amount: new bignumber_js_1.BigNumber(0),
useAllAmount: false,
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithZeroAmount);
expect(result.errors.amount).toBeInstanceOf(errors_1.AmountRequired);
});
it("should handle amount greater than max with NotEnoughBalance error", async () => {
// Account has spendable balance of 900
const txWithTooLargeAmount = {
...mockTransaction,
amount: new bignumber_js_1.BigNumber(1000),
useAllAmount: false,
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithTooLargeAmount);
expect(result.errors.amount).toBeInstanceOf(errors_1.NotEnoughBalance);
});
it("should calculate correct amount and totalSpent when useAllAmount is true", async () => {
const txWithUseAllAmount = {
...mockTransaction,
amount: new bignumber_js_1.BigNumber(0), // Amount should be ignored when useAllAmount is true
useAllAmount: true,
};
const result = await (0, getTransactionStatus_1.default)(mockAccount, txWithUseAllAmount);
// Should return spendableBalance - fee as the amount
expect(result.amount).toEqual(new bignumber_js_1.BigNumber(890)); // spendableBalance(900) - fee(10)
expect(result.totalSpent).toEqual(new bignumber_js_1.BigNumber(900)); // spendableBalance
expect(result.errors).toEqual({});
});
});
//# sourceMappingURL=getTransactionStatus.test.js.map