"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const sinon_1 = __importDefault(require("sinon"));
const internal_1 = require("../../internal");
const testUtils_1 = require("../../testUtils");
const mocks_1 = require("../../testUtils/mocks");
const types_1 = require("../../types");
const utils_1 = require("../../types/utils");
describe('Polymesh Transaction Batch class', () => {
    let context;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
    });
    const txSpec = {
        signer: 'signer',
        isCritical: false,
        fee: new bignumber_js_1.default(100),
        batchSize: null,
        paidByThirdParty: false,
    };
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
    });
    describe('get: args', () => {
        test('should return unwrapped args', () => {
            const tx = mocks_1.dsMockUtils.createTxStub('asset', 'registerTicker');
            const args = [utils_1.tuple('A_TICKER')];
            const transaction = new internal_1.PolymeshTransactionBatch(Object.assign(Object.assign({}, txSpec), { tx,
                args }), context);
            expect(transaction.args).toEqual(args);
            expect(transaction.args).toEqual(args); // this second call is to cover the case where the internal value is already set
        });
    });
    describe('method: run', () => {
        beforeAll(() => {
            mocks_1.dsMockUtils.createRpcStub('chain', 'getBlock', {
                returnValue: mocks_1.dsMockUtils.createMockSignedBlock({
                    block: {
                        header: {
                            number: mocks_1.dsMockUtils.createMockCompact(mocks_1.dsMockUtils.createMockU32(1)),
                            parentHash: 'hash',
                            stateRoot: 'hash',
                            extrinsicsRoot: 'hash',
                        },
                    },
                }),
            });
        });
        test('should execute the underlying transaction with the provided arguments, setting the tx and block hash when finished', () => __awaiter(void 0, void 0, void 0, function* () {
            const tx = mocks_1.dsMockUtils.createTxStub('asset', 'registerTicker');
            const batchStub = mocks_1.dsMockUtils.createTxStub('utility', 'batchAtomic', { autoresolve: false });
            const args = [utils_1.tuple('A_TICKER')];
            const transaction = new internal_1.PolymeshTransactionBatch(Object.assign(Object.assign({}, txSpec), { tx,
                args }), context);
            transaction.run();
            mocks_1.dsMockUtils.updateTxStatus(batchStub, mocks_1.dsMockUtils.MockTxStatus.InBlock);
            yield testUtils_1.fakePromise();
            mocks_1.dsMockUtils.updateTxStatus(batchStub, mocks_1.dsMockUtils.MockTxStatus.Succeeded);
            yield testUtils_1.fakePromise();
            sinon_1.default.assert.calledWith(tx, ...args[0]);
            sinon_1.default.assert.calledOnce(batchStub);
            expect(transaction.blockHash).toBeDefined();
            expect(transaction.blockNumber).toBeDefined();
            expect(transaction.txHash).toBeDefined();
            expect(transaction.status).toBe(types_1.TransactionStatus.Succeeded);
        }));
        test('should throw an error when one of the transactions in the batch fails', () => __awaiter(void 0, void 0, void 0, function* () {
            const tx = mocks_1.dsMockUtils.createTxStub('asset', 'registerTicker');
            const batchStub = mocks_1.dsMockUtils.createTxStub('utility', 'batchAtomic', { autoresolve: false });
            const args = [utils_1.tuple('ANOTHER_TICKER')];
            const transaction = new internal_1.PolymeshTransactionBatch(Object.assign(Object.assign({}, txSpec), { tx,
                args }), context);
            const runPromise = transaction.run();
            mocks_1.dsMockUtils.updateTxStatus(batchStub, mocks_1.dsMockUtils.MockTxStatus.BatchFailed);
            yield expect(runPromise).rejects.toThrow('Unknown error');
            expect(transaction.status).toBe(types_1.TransactionStatus.Failed);
        }));
    });
});
//# sourceMappingURL=PolymeshTransactionBatch.js.map