UNPKG

test-ic-wallet-middleware-icrc

Version:
86 lines (85 loc) 4.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const agent_1 = require("@dfinity/agent"); const principal_1 = require("@dfinity/principal"); const common_1 = require("@ic-wallet-middleware/common"); const mockLogger_1 = require("../../../../__tests_utils/mockLogger"); const getIcrcAllowanceForContactCacheHandler_1 = require("../../../../internalHandlers/icrcCacheDataHandlers/allowances/getIcrcAllowanceForContactCacheHandler/getIcrcAllowanceForContactCacheHandler"); const repositories_1 = require("../../../../repositories"); const types_1 = require("../../../../types"); describe("Unit GetIcrcAllowanceForContactCacheHandler tests", () => { const testData = [ { name: "getAllowanceForContact return data", input: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: types_1.SubAccountId.parseFromString("0x1"), ownerPrincipal: "gjcgk-x4xlt-6dzvd-q3mrr-pvgj5-5bjoe-beege-n4b7d-7hna5-pa5uq-5qe", spenderPrincipal: "xqknu-lpemp-tx4sq-ovbp4-2cf7s-z3was-f374m-l3mui-6zh7u-qdna6-fae", loadType: common_1.LoadType.Cache }, data: { cacheData: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: "0x1", senderPrincipal: "xqknu-lpemp-tx4sq-ovbp4-2cf7s-z3was-f374m-l3mui-6zh7u-qdna6-fae", amount: BigInt(1000000), expiration: undefined, } }, result: { amount: BigInt(1000000), ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", expiration: undefined, senderPrincipal: "xqknu-lpemp-tx4sq-ovbp4-2cf7s-z3was-f374m-l3mui-6zh7u-qdna6-fae", subAccountId: types_1.SubAccountId.parseFromString("0x1"), } }, { name: "getAllowanceForContact return false", input: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: types_1.SubAccountId.parseFromString("0x0"), ownerPrincipal: "xqknu-lpemp-tx4sq-ovbp4-2cf7s-z3was-f374m-l3mui-6zh7u-qdna6-fae", spenderPrincipal: "gjcgk-x4xlt-6dzvd-q3mrr-pvgj5-5bjoe-beege-n4b7d-7hna5-pa5uq-5qe", loadType: common_1.LoadType.Full }, data: { cacheData: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: "0x0", senderPrincipal: "xqknu-lpemp-tx4sq-ovbp4-2cf7s-z3was-f374m-l3mui-6zh7u-qdna6-fae", amount: BigInt(0), expiration: undefined, } }, result: { amount: BigInt(10000000), ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", expiration: undefined, senderPrincipal: "xqknu-lpemp-tx4sq-ovbp4-2cf7s-z3was-f374m-l3mui-6zh7u-qdna6-fae", subAccountId: types_1.SubAccountId.parseFromString("0x0"), } } ]; for (let test of testData) { it(test.name, async () => { const identifierService = new common_1.IdentifierService(); const cacheStorage = new repositories_1.AllowanceLocalCache(); identifierService.getAgent = jest.fn().mockReturnValue(new agent_1.HttpAgent()); identifierService.getPrincipal = jest.fn().mockReturnValue(principal_1.Principal.fromText(test.input.spenderPrincipal)); cacheStorage.getAllowanceForContact = jest.fn().mockReturnValue(test.data.cacheData); cacheStorage.updateAllowanceForContact = jest.fn().mockReturnValue(undefined); const expectedResult = common_1.FormResult.success(test.result); const logger = new mockLogger_1.MockLogger(); const getIcrcAllowanceForContactCacheHandler = new getIcrcAllowanceForContactCacheHandler_1.GetIcrcAllowanceForContactCacheHandler(logger, identifierService, cacheStorage); const result = await getIcrcAllowanceForContactCacheHandler.handle({ senderPrincipal: test.input.ownerPrincipal, ledgerAddress: test.input.ledgerAddress, subAccountId: test.input.subAccountId, loadType: test.input.loadType }); expect(result).toEqual(expectedResult); }); } });