@ledgerhq/coin-tron
Version:
Ledger Tron Coin integration
52 lines • 2.4 kB
JavaScript
import BigNumber from "bignumber.js";
import { fetchTronAccountTxs } from "../network";
import { fromTrongridTxInfoToOperation } from "../network/trongrid/trongrid-adapters";
import { listOperations } from "./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(0) },
{ txID: "tx2", value: new BigNumber(42) },
];
const mockOperations = [
{ tx: { hash: "tx1" }, value: BigInt(0) },
{ tx: { hash: "tx2" }, value: BigInt(42) },
];
fetchTronAccountTxs.mockResolvedValue(mockTxs);
fromTrongridTxInfoToOperation.mockImplementation(tx => {
return {
tx: { hash: tx.txID },
value: BigInt(tx.value.toString()),
};
});
const [operations, token] = await listOperations(mockAddress);
expect(fetchTronAccountTxs).toHaveBeenCalledWith(mockAddress, expect.any(Function), {});
expect(fromTrongridTxInfoToOperation).toHaveBeenCalledTimes(mockTxs.length);
expect(operations).toEqual(mockOperations);
expect(token).toBe("");
});
it("should handle empty transactions", async () => {
const mockTxs = [];
const mockOperations = [];
fetchTronAccountTxs.mockResolvedValue(mockTxs);
fromTrongridTxInfoToOperation.mockImplementation(() => null);
const [operations, token] = await listOperations(mockAddress);
expect(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!");
fetchTronAccountTxs.mockRejectedValue(exampleError);
await expect(listOperations(mockAddress)).rejects.toThrow(new Error(exampleError.message));
});
});
//# sourceMappingURL=listOperations.unit.test.js.map