@ledgerhq/coin-casper
Version:
Ledger Casper integration
94 lines • 4.69 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 = __importDefault(require("bignumber.js"));
const index_1 = require("../../api/index");
const txn_1 = require("./txn");
const accountShape_1 = require("./accountShape");
const fixtures_1 = require("../../test/fixtures");
// Mock dependencies
jest.mock("../../api/index");
jest.mock("./txn");
describe("getAccountShape", () => {
const { mockAddress, mockAccountInfo, mockAccountId, mockBlockHeight, mockPurseUref, mockAccountHash, mockBalance, mockTxs, mockOperations, } = (0, fixtures_1.createMockAccountShapeData)();
// Define a mock SyncConfig as required by the getAccountShape function
const mockSyncConfig = {
paginationConfig: {},
};
beforeEach(() => {
jest.clearAllMocks();
// Default mock implementations
index_1.fetchAccountStateInfo.mockResolvedValue({
purseUref: mockPurseUref,
accountHash: mockAccountHash,
});
index_1.fetchBlockHeight.mockResolvedValue(mockBlockHeight);
index_1.fetchBalance.mockResolvedValue(mockBalance);
index_1.fetchTxs.mockResolvedValue(mockTxs);
txn_1.mapTxToOps.mockImplementation(() => () => mockOperations);
});
test("should return the correct account shape for an account with balance", async () => {
const accountShape = await (0, accountShape_1.getAccountShape)(mockAccountInfo, mockSyncConfig);
expect(index_1.fetchAccountStateInfo).toHaveBeenCalledWith(mockAddress);
expect(index_1.fetchBlockHeight).toHaveBeenCalled();
expect(index_1.fetchBalance).toHaveBeenCalledWith(mockPurseUref);
expect(index_1.fetchTxs).toHaveBeenCalledWith(mockAddress);
expect(txn_1.mapTxToOps).toHaveBeenCalledWith(mockAccountId, mockAccountHash);
expect(accountShape).toEqual({
id: mockAccountId,
balance: mockBalance,
spendableBalance: mockBalance,
operations: mockOperations,
blockHeight: mockBlockHeight,
});
});
test("should return account with zero balance when purseUref is not found", async () => {
index_1.fetchAccountStateInfo.mockResolvedValue({
purseUref: undefined,
accountHash: mockAccountHash,
});
const accountShape = await (0, accountShape_1.getAccountShape)(mockAccountInfo, mockSyncConfig);
expect(index_1.fetchAccountStateInfo).toHaveBeenCalledWith(mockAddress);
expect(index_1.fetchBlockHeight).toHaveBeenCalled();
expect(index_1.fetchBalance).not.toHaveBeenCalled();
expect(index_1.fetchTxs).not.toHaveBeenCalled();
expect(accountShape).toEqual({
id: mockAccountId,
balance: new bignumber_js_1.default(0),
spendableBalance: new bignumber_js_1.default(0),
operations: [],
blockHeight: mockBlockHeight,
});
});
test("should return account with operations even when accountHash is not found", async () => {
index_1.fetchAccountStateInfo.mockResolvedValue({
purseUref: mockPurseUref,
accountHash: undefined,
});
const accountShape = await (0, accountShape_1.getAccountShape)(mockAccountInfo, mockSyncConfig);
expect(index_1.fetchAccountStateInfo).toHaveBeenCalledWith(mockAddress);
expect(index_1.fetchBlockHeight).toHaveBeenCalled();
expect(index_1.fetchBalance).toHaveBeenCalledWith(mockPurseUref);
expect(index_1.fetchTxs).toHaveBeenCalledWith(mockAddress);
expect(txn_1.mapTxToOps).toHaveBeenCalledWith(mockAccountId, "");
expect(accountShape).toEqual({
id: mockAccountId,
balance: mockBalance,
spendableBalance: mockBalance,
operations: mockOperations,
blockHeight: mockBlockHeight,
});
});
test("should handle API errors appropriately", async () => {
const errorMessage = "API Error";
index_1.fetchAccountStateInfo.mockRejectedValue(new Error(errorMessage));
await expect((0, accountShape_1.getAccountShape)(mockAccountInfo, mockSyncConfig)).rejects.toThrow(errorMessage);
expect(index_1.fetchAccountStateInfo).toHaveBeenCalledWith(mockAddress);
expect(index_1.fetchBlockHeight).not.toHaveBeenCalled();
expect(index_1.fetchBalance).not.toHaveBeenCalled();
expect(index_1.fetchTxs).not.toHaveBeenCalled();
});
});
//# sourceMappingURL=accountShape.test.js.map