UNPKG

stellar-plus

Version:

beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain

177 lines (176 loc) 8.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMockedHorizonHandler = createMockedHorizonHandler; const tslib_1 = require("tslib"); const stellar_base_1 = require("@stellar/stellar-base"); const stellar_sdk_1 = require("@stellar/stellar-sdk"); const network_1 = require("../../../../stellar-plus/network"); const _1 = require("."); jest.mock('@stellar/stellar-sdk', () => ({ Horizon: { Server: jest.fn(), }, Account: jest.fn(), TransactionBuilder: jest.fn(() => { return { addOperation: jest.fn(), setTimeout: jest.fn(), setSorobanData: jest.fn(() => { return {}; }), build: jest.fn(() => { return {}; }), }; }), })); function createMockedHorizonHandler() { return { loadAccount: jest.fn().mockResolvedValue({}), server: new stellar_sdk_1.Horizon.Server((0, network_1.TestNet)().horizonUrl), }; } const mockedHorizonHandler = createMockedHorizonHandler(); const MOCKED_BT_INPUT = { header: { source: 'source', fee: '100', timeout: 2, }, horizonHandler: mockedHorizonHandler, operations: [], networkPassphrase: 'networkPassphrase', }; const MOCKED_BT_OUTPUT = {}; describe('BuildTransactionPipeline', () => { let buildTransactionPipeline; beforeEach(() => { jest.clearAllMocks(); mockedHorizonHandler.loadAccount.mockClear(); }); describe('Load Account', () => { beforeEach(() => { buildTransactionPipeline = new _1.BuildTransactionPipeline(); jest.clearAllMocks(); }); it('should load account successfully', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield buildTransactionPipeline.execute(MOCKED_BT_INPUT); expect(mockedHorizonHandler.loadAccount).toHaveBeenCalledWith('source'); expect(mockedHorizonHandler.loadAccount).toHaveBeenCalledTimes(1); })); it('should throw error', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { mockedHorizonHandler.loadAccount.mockRejectedValueOnce(new Error('error')); yield expect(buildTransactionPipeline.execute(MOCKED_BT_INPUT)).rejects.toThrow('Could not load account!'); expect(mockedHorizonHandler.loadAccount).toHaveBeenCalledWith('source'); expect(mockedHorizonHandler.loadAccount).toHaveBeenCalledTimes(1); })); }); describe('Build Envelope', () => { const transactionBuilderOptions = { fee: MOCKED_BT_INPUT.header.fee, networkPassphrase: MOCKED_BT_INPUT.networkPassphrase, }; beforeEach(() => { buildTransactionPipeline = new _1.BuildTransactionPipeline(); jest.clearAllMocks(); }); it('should create envelope successfully', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield buildTransactionPipeline.execute(MOCKED_BT_INPUT); expect(stellar_sdk_1.TransactionBuilder).toHaveBeenCalledWith({}, transactionBuilderOptions); })); it('should add sorobanData successfully', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield buildTransactionPipeline.execute(Object.assign(Object.assign({}, MOCKED_BT_INPUT), { sorobanData: 'sorobanData' })); expect(stellar_sdk_1.TransactionBuilder).toHaveBeenCalledWith({}, transactionBuilderOptions); })); it('should add single operation successfully', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield buildTransactionPipeline.execute(Object.assign(Object.assign({}, MOCKED_BT_INPUT), { operations: [ stellar_base_1.Operation.payment({ destination: 'GB3MXH633VRECLZRUAR3QCLQJDMXNYNHKZCO6FJEWXVWSUEIS7NU376P', asset: stellar_base_1.Asset.native(), amount: '100', }), ] })); expect(stellar_sdk_1.TransactionBuilder).toHaveBeenCalledWith({}, transactionBuilderOptions); })); it('should add multiple operation successfully', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield buildTransactionPipeline.execute(Object.assign(Object.assign({}, MOCKED_BT_INPUT), { operations: [ stellar_base_1.Operation.payment({ destination: 'GB3MXH633VRECLZRUAR3QCLQJDMXNYNHKZCO6FJEWXVWSUEIS7NU376P', asset: stellar_base_1.Asset.native(), amount: '100', }), stellar_base_1.Operation.payment({ destination: 'GB3MXH633VRECLZRUAR3QCLQJDMXNYNHKZCO6FJEWXVWSUEIS7NU376P', asset: stellar_base_1.Asset.native(), amount: '100', }), stellar_base_1.Operation.payment({ destination: 'GB3MXH633VRECLZRUAR3QCLQJDMXNYNHKZCO6FJEWXVWSUEIS7NU376P', asset: stellar_base_1.Asset.native(), amount: '100', }), ] })); expect(stellar_sdk_1.TransactionBuilder).toHaveBeenCalledWith({}, transactionBuilderOptions); })); it('should throw error', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { ; stellar_sdk_1.TransactionBuilder.mockImplementationOnce(() => { throw new Error('error'); }); yield expect(buildTransactionPipeline.execute(MOCKED_BT_INPUT)).rejects.toThrow('Could not create transaction builder!'); expect(stellar_sdk_1.TransactionBuilder).toHaveBeenCalledWith({}, transactionBuilderOptions); })); it('should throw error adding sorobanData', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { ; stellar_sdk_1.TransactionBuilder.mockImplementationOnce(() => { return { setSorobanData: jest.fn(() => { throw new Error('error'); }), }; }); yield expect(buildTransactionPipeline.execute(Object.assign(Object.assign({}, MOCKED_BT_INPUT), { sorobanData: 'sorobanData' }))).rejects.toThrow('Could not set Soroban data!'); expect(stellar_sdk_1.TransactionBuilder).toHaveBeenCalledWith({}, transactionBuilderOptions); })); it('should throw error adding operation', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { ; stellar_sdk_1.TransactionBuilder.mockImplementationOnce(() => { return { addOperation: jest.fn(() => { throw new Error('error'); }), }; }); yield expect(buildTransactionPipeline.execute(Object.assign(Object.assign({}, MOCKED_BT_INPUT), { operations: [ stellar_base_1.Operation.payment({ destination: 'GB3MXH633VRECLZRUAR3QCLQJDMXNYNHKZCO6FJEWXVWSUEIS7NU376P', asset: stellar_base_1.Asset.native(), amount: '100', }), ] }))).rejects.toThrow('Could not add operations!'); expect(stellar_sdk_1.TransactionBuilder).toHaveBeenCalledWith({}, transactionBuilderOptions); })); it('should throw error building operation', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { ; stellar_sdk_1.TransactionBuilder.mockImplementationOnce(() => { return { setTimeout: jest.fn(), build: jest.fn(() => { throw new Error('error'); }), }; }); yield expect(buildTransactionPipeline.execute(MOCKED_BT_INPUT)).rejects.toThrow('Could not build transaction!'); expect(stellar_sdk_1.TransactionBuilder).toHaveBeenCalledWith({}, transactionBuilderOptions); })); }); describe('Process', () => { beforeEach(() => { buildTransactionPipeline = new _1.BuildTransactionPipeline(); jest.clearAllMocks(); }); it('should build transaction successfully', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield expect(buildTransactionPipeline.execute(MOCKED_BT_INPUT)).resolves.toEqual(MOCKED_BT_OUTPUT); })); }); });