"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 createPortfolio_1 = require("../createPortfolio");
const mocks_1 = require("../../../testUtils/mocks");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
const utilsInternalModule = __importStar(require("../../../utils/internal"));
describe('createPortfolio procedure', () => {
    let mockContext;
    let numberedPortfolio;
    let stringToTextStub;
    let getPortfolioIdByNameStub;
    let newPortfolioName;
    let rawNewPortfolioName;
    let addTransactionStub;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        numberedPortfolio = 'numberedPortfolio';
        stringToTextStub = sinon_1.default.stub(utilsConversionModule, 'stringToText');
        getPortfolioIdByNameStub = sinon_1.default.stub(utilsInternalModule, 'getPortfolioIdByName');
        newPortfolioName = 'newPortfolioName';
        rawNewPortfolioName = mocks_1.dsMockUtils.createMockText(newPortfolioName);
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub().returns([numberedPortfolio]);
        stringToTextStub.withArgs(newPortfolioName, mockContext).returns(rawNewPortfolioName);
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the portfolio name is duplicated', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        getPortfolioIdByNameStub.returns(new bignumber_js_1.default(1));
        return expect(createPortfolio_1.prepareCreatePortfolio.call(proc, { name: newPortfolioName })).rejects.toThrow('A Portfolio with that name already exists');
    });
    test('should add a create portfolio transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const createPortfolioTransaction = mocks_1.dsMockUtils.createTxStub('portfolio', 'createPortfolio');
        getPortfolioIdByNameStub.returns(null);
        const result = yield createPortfolio_1.prepareCreatePortfolio.call(proc, { name: newPortfolioName });
        sinon_1.default.assert.calledWith(addTransactionStub, sinon_1.default.match({
            transaction: createPortfolioTransaction,
            resolvers: sinon_1.default.match.array,
            args: [rawNewPortfolioName],
        }));
        expect(result).toBe(numberedPortfolio);
    }));
});
describe('createPortfolioResolver', () => {
    const filterEventRecordsStub = sinon_1.default.stub(utilsInternalModule, 'filterEventRecords');
    const did = 'someDid';
    const rawIdentityId = mocks_1.dsMockUtils.createMockIdentityId(did);
    const id = new bignumber_js_1.default(1);
    const rawId = mocks_1.dsMockUtils.createMockU64(id);
    let identityIdToStringStub;
    let u64ToBigNumberStub;
    beforeAll(() => {
        identityIdToStringStub = sinon_1.default.stub(utilsConversionModule, 'identityIdToString');
        u64ToBigNumberStub = sinon_1.default.stub(utilsConversionModule, 'u64ToBigNumber');
    });
    beforeEach(() => {
        identityIdToStringStub.withArgs(rawIdentityId).returns(did);
        u64ToBigNumberStub.withArgs(rawId).returns(id);
        filterEventRecordsStub.returns([mocks_1.dsMockUtils.createMockIEvent([rawIdentityId, rawId])]);
    });
    afterEach(() => {
        sinon_1.default.reset();
        filterEventRecordsStub.reset();
    });
    test('should return the new Numbered Portfolio', () => {
        const fakeContext = {};
        const result = createPortfolio_1.createPortfolioResolver(fakeContext)({});
        expect(result.id).toEqual(id);
    });
});
//# sourceMappingURL=createPortfolio.js.map