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