@ledgerhq/coin-casper
Version:
Ledger Casper integration
148 lines • 6.03 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 logs_1 = require("@ledgerhq/logs");
const deviceTransactionConfig_1 = __importDefault(require("./deviceTransactionConfig"));
const fixtures_1 = require("../test/fixtures");
const consts_1 = require("../consts");
const common_logic_1 = require("../common-logic");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
// Mock the log function to prevent console output during tests
jest.mock("@ledgerhq/logs", () => ({
log: jest.fn(),
}));
/**
* Tests for the getDeviceTransactionConfig function
* Validates the correct display fields are generated for Ledger devices
*/
describe("getDeviceTransactionConfig", () => {
// Common test objects
const MOCK_AMOUNT = new bignumber_js_1.default("1000000000"); // 1 CSPR
const TRANSFER_ID = "12345678";
/**
* Creates a mock transaction status for testing
* @returns A valid TransactionStatus object
*/
const createMockStatus = () => ({
errors: {},
warnings: {},
estimatedFees: new bignumber_js_1.default(0),
amount: new bignumber_js_1.default(0),
totalSpent: new bignumber_js_1.default(0),
});
/**
* Helper to get config fields
*/
const getConfigFields = (transaction, account = (0, fixtures_1.createMockAccount)()) => (0, deviceTransactionConfig_1.default)({
account,
parentAccount: null,
transaction,
status: createMockStatus(),
});
beforeEach(() => {
jest.clearAllMocks();
});
test("should display chain ID, transaction type and amount fields for standard transactions", () => {
// Create mock transaction
const mockTransaction = (0, fixtures_1.createMockTransaction)({
amount: MOCK_AMOUNT,
});
// Get display fields
const fields = getConfigFields(mockTransaction);
// Verify the results
expect(fields).toHaveLength(4);
// Check Type field
expect(fields[0]).toEqual({
type: "text",
label: "Type",
value: (0, common_logic_1.methodToString)(0), // 0 = "transfer"
});
// Check Chain ID field
expect(fields[1]).toEqual({
type: "text",
label: "Chain ID",
value: consts_1.CASPER_NETWORK,
});
// Check Fee field
expect(fields[2]).toEqual({
type: "casper.extendedAmount",
label: "Fee",
value: new bignumber_js_1.default(100000000),
});
// Check Amount field
expect(fields[3]).toEqual({
type: "casper.extendedAmount",
label: "Amount",
value: MOCK_AMOUNT,
});
// Verify logging
expect(logs_1.log).toHaveBeenCalledWith("debug", expect.stringContaining("Transaction config"));
});
test("should include transferId field when provided in transaction", () => {
// Create mock transaction with transferId
const mockTransaction = (0, fixtures_1.createMockTransaction)({
amount: MOCK_AMOUNT,
transferId: TRANSFER_ID,
});
// Get display fields
const fields = getConfigFields(mockTransaction);
// Verify the results
expect(fields).toHaveLength(5); // Chain ID, Type, Amount, Transfer ID fields
// Check all fields are present
const fieldLabels = fields.map(field => field.label);
expect(fieldLabels).toEqual(["Type", "Chain ID", "Fee", "Amount", "Transfer ID"]);
// Check Transfer ID field specifically
expect(fields[4]).toEqual({
type: "text",
label: "Transfer ID",
value: TRANSFER_ID,
});
});
test("should not include transferId field when undefined in transaction", () => {
// Create mock transaction with explicitly undefined transferId
const mockTransaction = (0, fixtures_1.createMockTransaction)({
amount: MOCK_AMOUNT,
transferId: undefined,
});
// Get display fields
const fields = getConfigFields(mockTransaction);
// Verify no transferId field is present
expect(fields).toHaveLength(4);
expect(fields.map(field => field.label)).not.toContain("Transfer ID");
});
test("should handle zero amount transactions correctly", () => {
// Create mock transaction with zero amount
const mockTransaction = (0, fixtures_1.createMockTransaction)({
amount: new bignumber_js_1.default(0),
});
// Get display fields
const fields = getConfigFields(mockTransaction);
// Verify amount field has zero value
const amountField = fields.find(field => field.label === "Amount");
expect(amountField).toBeDefined();
expect(amountField).toEqual({
type: "casper.extendedAmount",
label: "Amount",
value: new bignumber_js_1.default(0),
});
});
test("should maintain consistent order of fields regardless of transaction properties", () => {
// Create two transactions - one with transferId, one without
const txWithTransferId = (0, fixtures_1.createMockTransaction)({
amount: MOCK_AMOUNT,
transferId: TRANSFER_ID,
});
const txWithoutTransferId = (0, fixtures_1.createMockTransaction)({
amount: MOCK_AMOUNT,
});
const fieldsWithId = getConfigFields(txWithTransferId);
const fieldsWithoutId = getConfigFields(txWithoutTransferId);
// Verify field order consistency for common fields
for (let i = 0; i < fieldsWithoutId.length; i++) {
expect(fieldsWithId[i].label).toEqual(fieldsWithoutId[i].label);
}
});
});
//# sourceMappingURL=deviceTransactionConfig.test.js.map