@ledgerhq/coin-casper
Version:
Ledger Casper integration
72 lines • 3.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const casper_js_sdk_1 = require("casper-js-sdk");
const broadcast_1 = require("./broadcast");
const api_1 = require("../api");
const fixtures_1 = require("../test/fixtures");
const operation_1 = require("@ledgerhq/coin-framework/lib/operation");
// Mock the dependencies
jest.mock("casper-js-sdk", () => {
return {
Transaction: {
fromJSON: jest.fn().mockReturnValue({
setSignature: jest.fn(),
}),
},
PublicKey: {
fromHex: jest.fn().mockReturnValue("mockedPublicKey"),
},
};
});
jest.mock("../api", () => ({
broadcastTx: jest.fn().mockResolvedValue("mockedTxHash"),
}));
describe("broadcast", () => {
// Create test fixtures
const mockAccount = (0, fixtures_1.createMockAccount)();
const mockTransaction = (0, fixtures_1.createMockTransaction)();
const mockSignedOperation = (0, fixtures_1.createMockSignedOperation)(mockAccount, mockTransaction, {
signature: "deadbeef",
rawTxJson: { hash: "mockTxHash" },
});
afterEach(() => {
jest.clearAllMocks();
});
test("should successfully broadcast a transaction", async () => {
const result = await (0, broadcast_1.broadcast)({
account: mockAccount,
signedOperation: mockSignedOperation,
});
// Assert the transaction was constructed and signed correctly
expect(casper_js_sdk_1.Transaction.fromJSON).toHaveBeenCalledWith(mockSignedOperation.rawData.tx);
expect(casper_js_sdk_1.PublicKey.fromHex).toHaveBeenCalledWith(mockAccount.freshAddress);
// Assert the transaction was broadcast
expect(api_1.broadcastTx).toHaveBeenCalled();
// Assert the operation was patched with the hash
expect(result.hash).toBe("mockedTxHash");
expect(result).toEqual({
...(0, operation_1.patchOperationWithHash)(mockSignedOperation.operation, "mockedTxHash"),
hash: "mockedTxHash",
});
});
test("should throw if rawData is missing", async () => {
// Create a type-safe but invalid signed operation for testing
const invalidSignedOperation = {
...mockSignedOperation,
rawData: null, // Force type assertion for test
};
await expect((0, broadcast_1.broadcast)({
account: mockAccount,
signedOperation: invalidSignedOperation,
})).rejects.toThrow("casper: rawData is required");
});
test("should throw if broadcast fails to return a hash", async () => {
// Mock broadcastTx to return null (failed broadcast)
api_1.broadcastTx.mockResolvedValueOnce(null);
await expect((0, broadcast_1.broadcast)({
account: mockAccount,
signedOperation: mockSignedOperation,
})).rejects.toThrow("casper: failed to broadcast transaction and get transaction hash");
});
});
//# sourceMappingURL=broadcast.test.js.map