UNPKG

test-ic-wallet-middleware-icrc

Version:
199 lines (198 loc) 8.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const principal_1 = require("@dfinity/principal"); const common_1 = require("@ic-wallet-middleware/common"); const mockLogger_1 = require("../../../__tests_utils/mockLogger"); const seedToIdentity_1 = require("../../../__tests_utils/seedToIdentity"); const transferToServiceHandler_1 = require("../../../handlers/serviceHandlers/transferToServiceHandler/transferToServiceHandler"); const getSubAccountByHandler_1 = require("../../../internalHandlers/getSubAccountByHandler/getSubAccountByHandler"); const assetMetaDataCacheHandler_1 = require("../../../internalHandlers/icrcCacheDataHandlers/assets/assetMetaDataCacheHandler/assetMetaDataCacheHandler"); const types_1 = require("../../../types"); const wrappers_1 = require("../../../wrappers"); describe("Unit ServiceAssetCacheCreditHandler tests", () => { const princBytes = principal_1.Principal.fromText("gjcgk-x4xlt-6dzvd-q3mrr-pvgj5-5bjoe-beege-n4b7d-7hna5-pa5uq-5qe").toUint8Array(); const princSubId = `0x${princBytes.length.toString(16) + Buffer.from(princBytes).toString("hex")}`; const testData = [ { name: "transfer error", input: { amount: "0.0002", fromPrincipal: "ryjl3-tyaaa-aaaaa-aaaba-cai", fromSubId: types_1.SubAccountId.parseFromString("0x1"), toPrincipal: "pmr6h-yaaaa-aaaao-a3myq-cai", toSubId: types_1.SubAccountId.parseFromString(princSubId) }, data: { metadata: { symbol: "ICP", name: "Internet Computer", decimals: 8, logo: "", fee: BigInt(10000), }, subAccount: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: "0x0", balance: BigInt(3167000), currencyAmount: "0.00", decimal: 8, name: "Default", isSync: true, }, success: false }, result: common_1.FormResult.error([{ fieldName: "", localizationKey: "default.error.message", message: "Anonymous principal cannot hold tokens on the ledger." }]) }, { name: "transfer success", input: { amount: "0.0002", fromPrincipal: "ryjl3-tyaaa-aaaaa-aaaba-cai", fromSubId: types_1.SubAccountId.parseFromString("0x1"), toPrincipal: "pmr6h-yaaaa-aaaao-a3myq-cai", toSubId: types_1.SubAccountId.parseFromString(princSubId) }, data: { metadata: { symbol: "ICP", name: "Internet Computer", decimals: 8, logo: "", fee: BigInt(10000), }, subAccount: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: "0x0", balance: BigInt(3167000), currencyAmount: "0.00", decimal: 8, name: "Default", isSync: true, }, success: true }, result: common_1.FormResult.success({}) }, { name: "error, Amount should be more that 0", input: { amount: "0", fromPrincipal: "ryjl3-tyaaa-aaaaa-aaaba-cai", fromSubId: types_1.SubAccountId.parseFromString("0x1"), toPrincipal: "pmr6h-yaaaa-aaaao-a3myq-cai", toSubId: types_1.SubAccountId.parseFromString(princSubId) }, data: { metadata: { symbol: "ICP", name: "Internet Computer", decimals: 8, logo: "", fee: BigInt(10000), }, subAccount: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: "0x0", balance: BigInt(0), currencyAmount: "0.00", decimal: 8, name: "Default", isSync: true, } }, result: common_1.FormResult.error([{ fieldName: "amount", localizationKey: "transfer.service.invalid.amount", message: "Invalid amount", }]) }, { name: "error, Sent amount should be less than balance ", input: { amount: "0.0002", fromPrincipal: "ryjl3-tyaaa-aaaaa-aaaba-cai", fromSubId: types_1.SubAccountId.parseFromString("0x1"), toPrincipal: "pmr6h-yaaaa-aaaao-a3myq-cai", toSubId: types_1.SubAccountId.parseFromString(princSubId) }, data: { metadata: { symbol: "ICP", name: "Internet Computer", decimals: 8, logo: "", fee: BigInt(10000), }, subAccount: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: "0x0", balance: BigInt(1000), currencyAmount: "0.00", decimal: 8, name: "Default", isSync: true, }, success: true }, result: common_1.FormResult.error([{ fieldName: "sentAmount", localizationKey: "balance.less.amount", message: "Sent amount should be less than balance", }]) }, { name: "error, Asset Not Found", input: { amount: "0.0002", fromPrincipal: "ryjl3-tyaaa-aaaaa-aaaba-cai", fromSubId: types_1.SubAccountId.parseFromString("0x1"), toPrincipal: "pmr6h-yaaaa-aaaao-a3myq-cai", toSubId: types_1.SubAccountId.parseFromString(princSubId) }, data: { metadata: undefined, subAccount: { ledgerAddress: "ryjl3-tyaaa-aaaaa-aaaba-cai", subAccountId: "0x0", balance: BigInt(100000), currencyAmount: "0.00", decimal: 8, name: "Default", isSync: true, }, success: true }, result: common_1.FormResult.error([{ fieldName: "fromPrincipal", localizationKey: "asset.not.found", message: "Asset Not Found", }]) } ]; for (let test of testData) { it(test.name, async () => { jest.restoreAllMocks(); const logger = new mockLogger_1.MockLogger(); const identifierService = (0, seedToIdentity_1.mockAnonymousIdentifierService)(); const assetMetaDataHandler = new assetMetaDataCacheHandler_1.AssetMetaDataCacheHandler(); const getSubAccountByHandler = new getSubAccountByHandler_1.GetSubAccountByHandler(); const ledgerWrapper = new wrappers_1.LedgerWrapper(); wrappers_1.LedgerWrapper.create = jest.fn().mockReturnValue(ledgerWrapper); if (test.data.success) { ledgerWrapper.transfer = jest.fn().mockResolvedValue({}); } else { ledgerWrapper.transfer = jest.fn().mockRejectedValue(new Error("Anonymous principal cannot hold tokens on the ledger.")); } getSubAccountByHandler.process = jest.fn().mockReturnValue(Promise.resolve(test.data.subAccount)); assetMetaDataHandler.handle = jest.fn().mockReturnValue(Promise.resolve(test.data.metadata)); const transferToServiceHandler = new transferToServiceHandler_1.TransferToServiceHandler(logger, assetMetaDataHandler, identifierService, getSubAccountByHandler); const result = await transferToServiceHandler.handle(test.input); expect(result).toEqual(test.result); }, 10000); } });