"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 internal_1 = require("../../../internal");
const queries_1 = require("../../../middleware/queries");
const types_1 = require("../../../middleware/types");
const mocks_1 = require("../../../testUtils/mocks");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Identity', require("../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/base/Procedure', require("../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('NumberedPortfolio class', () => {
    let context;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend Entity', () => {
        expect(internal_1.NumberedPortfolio.prototype instanceof internal_1.Entity).toBe(true);
    });
    describe('constructor', () => {
        test('should assign Identity and id to instance', () => {
            const did = 'someDid';
            const id = new bignumber_js_1.default(1);
            const identity = mocks_1.entityMockUtils.getIdentityInstance({ did });
            const portfolio = new internal_1.NumberedPortfolio({ did, id }, context);
            expect(portfolio.owner).toEqual(identity);
            expect(portfolio.id).toEqual(id);
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.NumberedPortfolio.isUniqueIdentifiers({ did: 'someDid', id: new bignumber_js_1.default(1) })).toBe(true);
            expect(internal_1.NumberedPortfolio.isUniqueIdentifiers({ did: 'someDid' })).toBe(false);
            expect(internal_1.NumberedPortfolio.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.NumberedPortfolio.isUniqueIdentifiers({ did: 'someDid', id: 3 })).toBe(false);
            expect(internal_1.NumberedPortfolio.isUniqueIdentifiers({ did: 1, id: new bignumber_js_1.default(1) })).toBe(false);
        });
    });
    describe('method: delete', () => {
        test('should prepare the procedure and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const id = new bignumber_js_1.default(1);
            const did = 'someDid';
            const numberedPortfolio = new internal_1.NumberedPortfolio({ id, did }, context);
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { id, did }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield numberedPortfolio.delete();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: modifyName', () => {
        test('should prepare the procedure and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const id = new bignumber_js_1.default(1);
            const did = 'someDid';
            const name = 'newName';
            const numberedPortfolio = new internal_1.NumberedPortfolio({ id, did }, context);
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: { id, did, name }, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield numberedPortfolio.modifyName({ name });
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: getName', () => {
        test('should return the name of the Portfolio', () => __awaiter(void 0, void 0, void 0, function* () {
            const id = new bignumber_js_1.default(1);
            const did = 'someDid';
            const portfolioName = 'someName';
            const rawPortfolioName = mocks_1.dsMockUtils.createMockText(portfolioName);
            const numberedPortfolio = new internal_1.NumberedPortfolio({ id, did }, context);
            mocks_1.dsMockUtils.createQueryStub('portfolio', 'portfolios', {
                returnValue: rawPortfolioName,
            });
            const result = yield numberedPortfolio.getName();
            expect(result).toEqual(portfolioName);
        }));
    });
    describe('method: createdAt', () => {
        const id = new bignumber_js_1.default(1);
        const did = 'someDid';
        const variables = {
            moduleId: types_1.ModuleIdEnum.Portfolio,
            eventId: types_1.EventIdEnum.PortfolioCreated,
            eventArg0: did,
            eventArg1: id.toString(),
        };
        test('should return the event identifier object of the portfolio creation', () => __awaiter(void 0, void 0, void 0, function* () {
            const blockNumber = new bignumber_js_1.default(1234);
            const blockDate = new Date('4/14/2020');
            const eventIdx = 1;
            const fakeResult = { blockNumber, blockDate, eventIndex: eventIdx };
            const numberedPortfolio = new internal_1.NumberedPortfolio({ id, did }, context);
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.eventByIndexedArgs(variables), {
                /* eslint-disable @typescript-eslint/naming-convention */
                eventByIndexedArgs: {
                    block_id: blockNumber.toNumber(),
                    block: { datetime: blockDate },
                    event_idx: eventIdx,
                },
                /* eslint-enable @typescript-eslint/naming-convention */
            });
            const result = yield numberedPortfolio.createdAt();
            expect(result).toEqual(fakeResult);
        }));
        test('should return null if the query result is empty', () => __awaiter(void 0, void 0, void 0, function* () {
            const numberedPortfolio = new internal_1.NumberedPortfolio({ id, did }, context);
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.eventByIndexedArgs(variables), {});
            const result = yield numberedPortfolio.createdAt();
            expect(result).toBeNull();
        }));
    });
    describe('method: exists', () => {
        test('should return whether the portfolio exists', () => __awaiter(void 0, void 0, void 0, function* () {
            const did = 'someDid';
            const id = new bignumber_js_1.default(1);
            const portfolioId = new bignumber_js_1.default(0);
            const portfoliosStub = mocks_1.dsMockUtils.createQueryStub('portfolio', 'portfolios', {
                size: 0,
            });
            sinon_1.default
                .stub(utilsConversionModule, 'stringToIdentityId')
                .returns(mocks_1.dsMockUtils.createMockIdentityId(did));
            sinon_1.default
                .stub(utilsConversionModule, 'numberToU64')
                .returns(mocks_1.dsMockUtils.createMockU64(portfolioId.toNumber()));
            const numberedPortfolio = new internal_1.NumberedPortfolio({ id, did }, context);
            let result = yield numberedPortfolio.exists();
            expect(result).toBe(false);
            portfoliosStub.size.resolves(mocks_1.dsMockUtils.createMockU64(10));
            result = yield numberedPortfolio.exists();
            expect(result).toBe(true);
        }));
    });
});
//# sourceMappingURL=NumberedPortfolio.js.map