@ledgerhq/coin-icon
Version:
Ledger Icon Coin integration
164 lines • 6.83 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const network_1 = __importDefault(require("@ledgerhq/live-network/network"));
const api_1 = require("../../api");
const logic_1 = require("../../logic");
const config_1 = require("../../config");
const querystring_1 = __importDefault(require("querystring"));
const contants = __importStar(require("../../constants"));
// Mock the necessary modules and functions
jest.mock("@ledgerhq/live-network/network");
jest.mock("@ledgerhq/coin-framework/operation");
jest.mock("@ledgerhq/cryptoassets");
jest.mock("../../logic");
jest.mock("../../config");
jest.mock("querystring");
jest.mock("@ledgerhq/logs");
describe("ICON API", () => {
const networkMock = network_1.default;
const isTestnetMock = logic_1.isTestnet;
const getCoinConfigMock = config_1.getCoinConfig;
const querystringMock = querystring_1.default.stringify;
const mockedLogic = jest.mocked(contants);
isTestnetMock.mockReturnValue(true);
getCoinConfigMock.mockReturnValue({
infra: {
indexer: "mainnet-url",
indexer_testnet: "testnet-url",
},
});
beforeEach(() => {
jest.clearAllMocks();
});
describe("fetchOperationList", () => {
it("should recursively fetch operation list correctly", async () => {
const accountId = "accountId";
const addr = "hx123";
const skip = 0;
const network = { id: "icon" };
const maxLength = 10;
// @ts-expect-error type
mockedLogic.LIMIT = 10;
const tx1 = {
hash: "tx1",
from_address: addr,
to_address: "hx456",
transaction_fee: "10",
block_number: 12345,
block_timestamp: 1609459200000,
status: "0x1",
value: "1000",
};
const tx2 = {
hash: "tx2",
from_address: "hx456",
to_address: addr,
transaction_fee: "5",
block_number: 12346,
block_timestamp: 1609459201000,
status: "0x1",
value: "2000",
};
const txHistory = [tx1, tx2];
querystringMock.mockReturnValue("address=hx123&skip=0&limit=10");
networkMock.mockResolvedValue({ data: txHistory });
const result = await (0, api_1.fetchOperationList)(accountId, addr, skip, network, maxLength);
expect(result.length).toBe(2);
expect(networkMock).toHaveBeenCalledWith({
method: "GET",
url: `testnet-url/transactions/address/${addr}?address=${addr}&skip=${skip}&limit=${10}`,
});
});
it("should recursively fetch operation list correctly", async () => {
const accountId = "accountId";
const addr = "hx123";
const skip = 0;
const network = { id: "icon" };
const maxLength = 10;
// @ts-expect-error type
mockedLogic.LIMIT = 2; // set a small limit for easier testing
const tx1 = {
hash: "tx1",
from_address: addr,
to_address: "hx456",
transaction_fee: "10",
block_number: 12345,
block_timestamp: 1609459200000,
status: "0x1",
value: "1000",
};
const tx2 = {
hash: "tx2",
from_address: "hx456",
to_address: addr,
transaction_fee: "5",
block_number: 12346,
block_timestamp: 1609459201000,
status: "0x1",
value: "2000",
};
const tx3 = {
hash: "tx3",
from_address: addr,
to_address: "hx456",
transaction_fee: "15",
block_number: 12347,
block_timestamp: 1609459202000,
status: "0x1",
value: "1500",
};
networkMock
.mockResolvedValueOnce({ data: [tx1, tx2] }) // First fetch returns two transactions
.mockResolvedValueOnce({ data: [tx3] }); // Second fetch returns one transaction
querystringMock
.mockReturnValueOnce("address=hx123&skip=0&limit=2")
.mockReturnValueOnce("address=hx123&skip=2&limit=2");
const result = await (0, api_1.fetchOperationList)(accountId, addr, skip, network, maxLength);
// Verify that the function was called recursively
expect(networkMock).toHaveBeenCalledTimes(2);
// Verify that the result includes all transactions fetched
expect(result.length).toBe(3);
expect(result).toEqual([
expect.objectContaining({ hash: "tx1" }),
expect.objectContaining({ hash: "tx2" }),
expect.objectContaining({ hash: "tx3" }),
]);
// Verify the URLs used in the network calls
expect(networkMock).toHaveBeenCalledWith({
method: "GET",
url: `testnet-url/transactions/address/${addr}?address=${addr}&skip=${skip}&limit=${2}`,
});
expect(networkMock).toHaveBeenCalledWith({
method: "GET",
url: `testnet-url/transactions/address/${addr}?address=${addr}&skip=${skip + 2}&limit=${2}`,
});
});
});
});
//# sourceMappingURL=index.unit.test.js.map