UNPKG

stellar-plus

Version:

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

86 lines (85 loc) 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const stellar_sdk_1 = tslib_1.__importDefault(require("@stellar/stellar-sdk")); const result_meta_xdr_1 = require("./result-meta-xdr"); jest.mock('@stellar/stellar-sdk'); describe('extractSorobanResultXdrOpErrorCode', () => { afterEach(() => { jest.resetAllMocks(); }); beforeEach(() => { stellar_sdk_1.default.xdr.TransactionResult = { fromXDR: jest.fn(), }; }); it('should extract op error code from XDR string', () => { const mockResultXdrObject = { result: () => ({ results: () => [ { tr: () => ({ value: () => ({ switch: () => ({ name: 'opErrorCode', }), }), }), }, ], }), }; stellar_sdk_1.default.xdr.TransactionResult.fromXDR.mockReturnValueOnce(mockResultXdrObject); const result = (0, result_meta_xdr_1.extractSorobanResultXdrOpErrorCode)('base64EncodedXDR'); expect(result).toBe('opErrorCode'); expect(stellar_sdk_1.default.xdr.TransactionResult.fromXDR).toHaveBeenCalledWith('base64EncodedXDR', 'base64'); }); it('should handle XDR object with result method', () => { const mockResultXdrObject = { result: jest.fn(() => ({ results: jest.fn(() => [ { tr: jest.fn(() => ({ value: jest.fn(() => ({ switch: jest.fn(() => ({ name: 'opErrorCode', })), })), })), }, ]), })), }; const result = (0, result_meta_xdr_1.extractSorobanResultXdrOpErrorCode)(mockResultXdrObject); expect(result).toBe('opErrorCode'); expect(mockResultXdrObject.result).toHaveBeenCalled(); }); it('should handle XDR object with result.switch method', () => { const mockResultXdrObject = { result: jest.fn(() => ({ switch: jest.fn(() => ({ name: 'opErrorCode', })), })), }; const result = (0, result_meta_xdr_1.extractSorobanResultXdrOpErrorCode)(mockResultXdrObject); expect(result).toBe('opErrorCode'); expect(mockResultXdrObject.result).toHaveBeenCalled(); }); it('should handle XDR object with missing results or switch methods', () => { const result = (0, result_meta_xdr_1.extractSorobanResultXdrOpErrorCode)({}); expect(result).toBe('not_found'); }); it('should handle decoding errors', () => { const mockResultXdrObject = { result: () => { throw new Error('Decoding error'); }, }; const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => { }); const result = (0, result_meta_xdr_1.extractSorobanResultXdrOpErrorCode)(mockResultXdrObject); expect(result).toBe('fail_in_decode_xdr'); expect(consoleSpy).toHaveBeenCalledWith('Fail in decode xdr: %s, Error: %s', mockResultXdrObject, expect.any(Error)); consoleSpy.mockRestore(); }); });