From bd90602fb80a83b40fff8b2a65e8004cb28f99f3 Mon Sep 17 00:00:00 2001 From: sahar-fehri Date: Wed, 20 Sep 2023 00:49:18 +0200 Subject: [PATCH] fix: fix util fct --- ui/helpers/utils/util.js | 10 +++++++--- ui/helpers/utils/util.test.js | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/ui/helpers/utils/util.js b/ui/helpers/utils/util.js index cd0609a42e11..1fd830467042 100644 --- a/ui/helpers/utils/util.js +++ b/ui/helpers/utils/util.js @@ -643,8 +643,8 @@ export const isAbleToExportAccount = (keyringType = '') => { /** * Checks if a tokenId in Hex or decimal format already exists in an object. * - * @param {*} address - collection address. - * @param {*} tokenId - tokenId to search for + * @param {string} address - collection address. + * @param {string} tokenId - tokenId to search for * @param {*} obj - object to look into * @returns {boolean} `false` if tokenId does not already exist. */ @@ -652,14 +652,18 @@ export const checkTokenIdExists = (address, tokenId, obj) => { // check if input tokenId is hexadecimal // If it is convert to decimal and compare with existing tokens // if it is decimal convert to hexadecimal and compare + // if it is neither leave as it is const isHex = isStrictHexString(tokenId); let convertedTokenId; if (isHex) { // Convert to decimal convertedTokenId = hexToDecimal(tokenId); - } else { + // eslint-disable-next-line no-negated-condition + } else if (!isNaN(tokenId)) { // Convert to hex convertedTokenId = `0x${decimalToHex(tokenId)}`; + } else { + convertedTokenId = tokenId; } if (obj[address]) { diff --git a/ui/helpers/utils/util.test.js b/ui/helpers/utils/util.test.js index 75d68aad5cca..652efcd9e7f2 100644 --- a/ui/helpers/utils/util.test.js +++ b/ui/helpers/utils/util.test.js @@ -956,6 +956,19 @@ describe('util', () => { }, ], }, + '0xf4910C763eD4e47A585E2D34baA9A4b611aE448C': { + collectionName: 'foo', + nfts: [ + { + address: '0xf4910C763eD4e47A585E2D34baA9A4b611aE448C', + description: 'foo', + favorite: false, + name: 'toto#111486581076844052489180254627234340268504869259922513413248833349282110111749', + tokenId: + '111486581076844052489180254627234340268504869259922513413248833349282110111749', + }, + ], + }, }; it('should return true if it exists', () => { expect( @@ -977,6 +990,16 @@ describe('util', () => { ).toBeTruthy(); }); + it('should return true if is exists but input is not decimal nor hex', () => { + expect( + util.checkTokenIdExists( + '0xf4910C763eD4e47A585E2D34baA9A4b611aE448C', + '111486581076844052489180254627234340268504869259922513413248833349282110111749', + data, + ), + ).toBeTruthy(); + }); + it('should return false if it does not exists', () => { expect( util.checkTokenIdExists(