From 08c7cb642bfdce649d403c23296765d40e42b507 Mon Sep 17 00:00:00 2001 From: Sameer Kumar Subudhi Date: Fri, 17 Nov 2023 17:51:13 +0100 Subject: [PATCH] :rotating_light: Add more unit tests --- .../business/transactionsEstimateFees.js | 2 + .../business/transactionEstimateFees.test.js | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/services/blockchain-indexer/shared/dataService/business/transactionsEstimateFees.js b/services/blockchain-indexer/shared/dataService/business/transactionsEstimateFees.js index c926756ffc..185bfab1e1 100644 --- a/services/blockchain-indexer/shared/dataService/business/transactionsEstimateFees.js +++ b/services/blockchain-indexer/shared/dataService/business/transactionsEstimateFees.js @@ -496,10 +496,12 @@ module.exports = { estimateTransactionFees, // Export for the unit tests + getCcmBuffer, calcDynamicFeeEstimates, mockTransaction, calcAdditionalFees, filterOptionalProps, getNumberOfSignatures, validateTransactionParams, + validateUserHasTokenAccount, }; diff --git a/services/blockchain-indexer/tests/unit/shared/dataservice/business/transactionEstimateFees.test.js b/services/blockchain-indexer/tests/unit/shared/dataservice/business/transactionEstimateFees.test.js index 8d1fc56ec3..a89795ccbc 100644 --- a/services/blockchain-indexer/tests/unit/shared/dataservice/business/transactionEstimateFees.test.js +++ b/services/blockchain-indexer/tests/unit/shared/dataservice/business/transactionEstimateFees.test.js @@ -160,6 +160,42 @@ jest.mock('../../../../../shared/dataService/business/token', () => ({ }, })); +describe('getCcmBuffer', () => { + it('should return null if the transaction is not token transferCrossChain', () => { + const { getCcmBuffer } = require(mockedTransactionFeeEstimatesFilePath); + expect(getCcmBuffer(mockRegisterValidatorTxRequestConnector.transaction)).resolves.toBeNull(); + }); + + it.todo("Add test cases for 'if (!ccmSendSuccess)' scenarios"); +}); + +describe('validateUserHasTokenAccount', () => { + it.todo('Fix the mocks for validateUserHasTokenAccount dependencies and enable the tests'); + + it('should return undefined if user has token account initialized', () => { + const { validateUserHasTokenAccount } = require(mockedTransactionFeeEstimatesFilePath); + const { tokenID, recipientAddress } = mockTransferCrossChainTxRequest.transaction.params; + + expect(validateUserHasTokenAccount(tokenID, recipientAddress)).resolves.toBeUndefined(); + }); + + xit("should throw an error if user doesn't have token account initialized", async () => { + jest.mock('../../../../../shared/dataService/business/token', () => ({ + async tokenHasUserAccount() { + return { + data: { isExists: false }, + meta: {}, + }; + }, + })); + + const { validateUserHasTokenAccount } = require(mockedTransactionFeeEstimatesFilePath); + const { tokenID, recipientAddress } = mockTransferCrossChainTxRequest.transaction.params; + + await expect(validateUserHasTokenAccount(tokenID, recipientAddress)).rejects.toThrow(); + }); +}); + describe('validateTransactionParams', () => { it('should validate a valid token and register validator transaction', () => { const { validateTransactionParams } = require(mockedTransactionFeeEstimatesFilePath); @@ -298,6 +334,23 @@ describe('validateTransactionParams', () => { }), ).rejects.toThrow(); }); + + it('should throw an error if user has insufficient balance transaction', () => { + const { sendingChainID, ...remParams } = + mockInteroperabilitySubmitMainchainCrossChainUpdateTxRequest.transaction.params; + + const { validateTransactionParams } = require(mockedTransactionFeeEstimatesFilePath); + + expect(() => + validateTransactionParams({ + ...mockInteroperabilitySubmitMainchainCrossChainUpdateTxRequest.transaction, + params: { + ...remParams, + amount: Number.MAX_SAFE_INTEGER.toString(), + }, + }), + ).rejects.toThrow(); + }); }); describe('Test transaction fees estimates', () => {