"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 mocks_1 = require("../../../../testUtils/mocks");
jest.mock('~/base/Procedure', require("../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
jest.mock('~/api/entities/Account', require("../../../../testUtils/mocks/entities").mockAccountModule('~/api/entities/Account'));
describe('Subsidy class', () => {
    let context;
    let beneficiary;
    let subsidizer;
    let subsidy;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
        beneficiary = 'beneficiary';
        subsidizer = 'subsidizer';
        subsidy = new internal_1.Subsidy({ beneficiary, subsidizer }, context);
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend Entity', () => {
        expect(internal_1.Subsidy.prototype instanceof internal_1.Entity).toBe(true);
    });
    describe('constructor', () => {
        test('should assign beneficiary and subsidizer to instance', () => {
            const fakeResult = expect.objectContaining({
                beneficiary: expect.objectContaining({ address: beneficiary }),
                subsidizer: expect.objectContaining({ address: subsidizer }),
            });
            expect(subsidy).toEqual(fakeResult);
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.Subsidy.isUniqueIdentifiers({ beneficiary, subsidizer })).toBe(true);
            expect(internal_1.Subsidy.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.Subsidy.isUniqueIdentifiers({ beneficiary: 1, subsidizer: 2 })).toBe(false);
        });
    });
    describe('method: quit', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should prepare the quit procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const args = { subsidy };
            const expectedQueue = 'mockQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield subsidy.quit();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: exists', () => {
        test('should return whether the Subsidy exists', () => __awaiter(void 0, void 0, void 0, function* () {
            context.accountSubsidy.onFirstCall().returns(null);
            yield expect(subsidy.exists()).resolves.toBe(false);
            mocks_1.entityMockUtils.configureMocks({
                accountOptions: {
                    isEqual: false,
                },
            });
            context.accountSubsidy.onSecondCall().returns({
                subsidy: mocks_1.entityMockUtils.getSubsidyInstance({ subsidizer: 'mockSubsidizer' }),
                allowance: new bignumber_js_1.default(1),
            });
            yield expect(subsidy.exists()).resolves.toBe(false);
            mocks_1.entityMockUtils.configureMocks({
                accountOptions: {
                    isEqual: true,
                },
            });
            context.accountSubsidy.onThirdCall().returns({
                subsidy: mocks_1.entityMockUtils.getSubsidyInstance(),
                allowance: new bignumber_js_1.default(1),
            });
            yield expect(subsidy.exists()).resolves.toBe(true);
        }));
    });
    describe('method: getAllowance', () => {
        test('should throw an error if the Subsidy relationship does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
            context.accountSubsidy.onFirstCall().returns(null);
            let error;
            try {
                yield subsidy.getAllowance();
            }
            catch (err) {
                error = err;
            }
            expect(error.message).toBe('The Subsidy no longer exists');
            context.accountSubsidy.onSecondCall().returns({
                subsidy: mocks_1.entityMockUtils.getSubsidyInstance({ subsidizer: 'otherAddress' }),
                allowance: new bignumber_js_1.default(1),
            });
            mocks_1.entityMockUtils.configureMocks({
                accountOptions: {
                    isEqual: false,
                },
            });
            try {
                yield subsidy.getAllowance();
            }
            catch (err) {
                error = err;
            }
            expect(error.message).toBe('The Subsidy no longer exists');
        }));
        test('should return allowance of the Subsidy relationship', () => __awaiter(void 0, void 0, void 0, function* () {
            const allowance = new bignumber_js_1.default(100);
            context.accountSubsidy.returns({
                subsidy: mocks_1.entityMockUtils.getSubsidyInstance(),
                allowance,
            });
            yield expect(subsidy.getAllowance()).resolves.toBe(allowance);
        }));
    });
    describe('method: toJson', () => {
        test('should return a human readable version of the entity', () => {
            subsidy.beneficiary.toJson = sinon_1.default.stub().returns(beneficiary);
            subsidy.subsidizer.toJson = sinon_1.default.stub().returns(subsidizer);
            expect(subsidy.toJson()).toEqual({ beneficiary, subsidizer });
        });
    });
});
//# sourceMappingURL=index.js.map