@ledgerhq/coin-tron
Version:
Ledger Tron Coin integration
57 lines • 2.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const network_1 = require("../network");
const trongrid_adapters_1 = require("../network/trongrid/trongrid-adapters");
const listOperations_1 = require("./listOperations");
// Mock the fetchTronAccountTxs and fromTrongridTxInfoToOperation functions
jest.mock("../network", () => ({
fetchTronAccountTxs: jest.fn(),
}));
jest.mock("../network/trongrid/trongrid-adapters", () => ({
fromTrongridTxInfoToOperation: jest.fn(),
}));
describe("listOperations", () => {
const mockAddress = "tronExampleAddress";
it("should fetch transactions and return operations", async () => {
const mockTxs = [
{ txID: "tx1", value: new bignumber_js_1.default(0) },
{ txID: "tx2", value: new bignumber_js_1.default(42) },
];
const mockOperations = [
{ tx: { hash: "tx1" }, value: BigInt(0) },
{ tx: { hash: "tx2" }, value: BigInt(42) },
];
network_1.fetchTronAccountTxs.mockResolvedValue(mockTxs);
trongrid_adapters_1.fromTrongridTxInfoToOperation.mockImplementation(tx => {
return {
tx: { hash: tx.txID },
value: BigInt(tx.value.toString()),
};
});
const [operations, token] = await (0, listOperations_1.listOperations)(mockAddress);
expect(network_1.fetchTronAccountTxs).toHaveBeenCalledWith(mockAddress, expect.any(Function), {});
expect(trongrid_adapters_1.fromTrongridTxInfoToOperation).toHaveBeenCalledTimes(mockTxs.length);
expect(operations).toEqual(mockOperations);
expect(token).toBe("");
});
it("should handle empty transactions", async () => {
const mockTxs = [];
const mockOperations = [];
network_1.fetchTronAccountTxs.mockResolvedValue(mockTxs);
trongrid_adapters_1.fromTrongridTxInfoToOperation.mockImplementation(() => null);
const [operations, token] = await (0, listOperations_1.listOperations)(mockAddress);
expect(network_1.fetchTronAccountTxs).toHaveBeenCalledWith(mockAddress, expect.any(Function), {});
expect(operations).toEqual(mockOperations);
expect(token).toBe("");
});
it("should handle errors from fetchTronAccountTxs", async () => {
const exampleError = new Error("Network error!");
network_1.fetchTronAccountTxs.mockRejectedValue(exampleError);
await expect((0, listOperations_1.listOperations)(mockAddress)).rejects.toThrow(new Error(exampleError.message));
});
});
//# sourceMappingURL=listOperations.unit.test.js.map