"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 Middleware_1 = require("../Middleware");
const queries_1 = require("../middleware/queries");
const types_1 = require("../middleware/types");
const mocks_1 = require("../testUtils/mocks");
describe('Middleware Class', () => {
    let context;
    let middleware;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
        middleware = new Middleware_1.Middleware(context);
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.entityMockUtils.cleanup();
    });
    describe('method: getEventByIndexedArgs', () => {
        const variables = {
            moduleId: types_1.ModuleIdEnum.Asset,
            eventId: types_1.EventIdEnum.AssetCreated,
        };
        test('should return a single event', () => __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 };
            mocks_1.dsMockUtils.configureMocks({ contextOptions: { withSeed: true } });
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.eventByIndexedArgs(Object.assign(Object.assign({}, variables), { eventArg0: undefined, eventArg1: undefined, eventArg2: undefined })), {
                /* 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 middleware.getEventByIndexedArgs(variables);
            expect(result).toEqual(fakeResult);
        }));
        test('should return null if the query result is empty', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.eventByIndexedArgs(Object.assign(Object.assign({}, variables), { eventArg0: 'someDid', eventArg1: undefined, eventArg2: undefined })), {});
            const result = yield middleware.getEventByIndexedArgs(Object.assign(Object.assign({}, variables), { eventArg0: 'someDid' }));
            expect(result).toBeNull();
        }));
    });
    describe('method: getEventsByIndexedArgs', () => {
        const variables = {
            moduleId: types_1.ModuleIdEnum.Asset,
            eventId: types_1.EventIdEnum.AssetCreated,
        };
        test('should return a list of events', () => __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 }];
            mocks_1.dsMockUtils.configureMocks({ contextOptions: { withSeed: true } });
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.eventsByIndexedArgs(Object.assign(Object.assign({}, variables), { eventArg0: undefined, eventArg1: undefined, eventArg2: undefined, count: undefined, skip: undefined })), {
                /* eslint-disable @typescript-eslint/naming-convention */
                eventsByIndexedArgs: [
                    {
                        block_id: blockNumber.toNumber(),
                        block: { datetime: blockDate },
                        event_idx: eventIdx,
                    },
                ],
                /* eslint-enable @typescript-eslint/naming-convention */
            });
            const result = yield middleware.getEventsByIndexedArgs(variables);
            expect(result).toEqual(fakeResult);
        }));
        test('should return null if the query result is empty', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.eventsByIndexedArgs(Object.assign(Object.assign({}, variables), { eventArg0: 'someDid', eventArg1: undefined, eventArg2: undefined, count: undefined, skip: undefined })), {});
            const result = yield middleware.getEventsByIndexedArgs(Object.assign(Object.assign({}, variables), { eventArg0: 'someDid' }));
            expect(result).toBeNull();
        }));
    });
    describe('method: getTransactionByHash', () => {
        const variable = { txHash: 'someHash' };
        test('should return a transaction', () => __awaiter(void 0, void 0, void 0, function* () {
            const blockNumber = new bignumber_js_1.default(1);
            const extrinsicIdx = 2;
            const address = 'someAddress';
            const specVersionId = 2006;
            mocks_1.dsMockUtils.configureMocks({ contextOptions: { withSeed: true } });
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.transactionByHash({ transactionHash: variable.txHash }), {
                /* eslint-disable @typescript-eslint/naming-convention */
                transactionByHash: {
                    module_id: types_1.ModuleIdEnum.Asset,
                    call_id: types_1.CallIdEnum.RegisterTicker,
                    extrinsic_idx: extrinsicIdx,
                    spec_version_id: specVersionId,
                    params: [],
                    block_id: blockNumber.toNumber(),
                    address,
                    success: 0,
                },
                /* eslint-enable @typescript-eslint/naming-convention */
            });
            let result = yield middleware.getTransactionByHash(variable);
            expect(result).toEqual({
                blockNumber,
                extrinsicIdx: extrinsicIdx,
                address,
                nonce: undefined,
                txTag: 'asset.registerTicker',
                params: [],
                success: false,
                specVersionId,
                extrinsicHash: undefined,
            });
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.transactionByHash({ transactionHash: variable.txHash }), {
                /* eslint-disable @typescript-eslint/naming-convention */
                transactionByHash: {
                    module_id: types_1.ModuleIdEnum.Asset,
                    call_id: types_1.CallIdEnum.RegisterTicker,
                    extrinsic_idx: extrinsicIdx,
                    spec_version_id: specVersionId,
                    params: [],
                    block_id: blockNumber.toNumber(),
                    address: null,
                    success: 0,
                },
                /* eslint-enable @typescript-eslint/naming-convention */
            });
            result = yield middleware.getTransactionByHash(variable);
            expect(result).toEqual({
                blockNumber,
                extrinsicIdx: extrinsicIdx,
                address: null,
                nonce: undefined,
                txTag: 'asset.registerTicker',
                params: [],
                success: false,
                specVersionId,
                extrinsicHash: undefined,
            });
        }));
        test('should return null if the query result is empty', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.dsMockUtils.createApolloQueryStub(queries_1.transactionByHash({
                transactionHash: variable.txHash,
            }), {});
            const result = yield middleware.getTransactionByHash(variable);
            expect(result).toBeNull();
        }));
    });
});
//# sourceMappingURL=Middleware.js.map