Skip to content

Commit

Permalink
fix: add tests for conversion fcts
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri committed Sep 19, 2023
1 parent 37e89ab commit d2c6d07
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
} from '../../component-library';
import Tooltip from '../../ui/tooltip';
import { useNftsCollections } from '../../../hooks/useNftsCollections';
import { tokenIdExist } from '../../../helpers/utils/util';
import { checkTokenIdExists } from '../../../helpers/utils/util';

export const ImportNftsModal = ({ onClose }) => {
const t = useI18nContext();
Expand All @@ -71,7 +71,6 @@ export const ImportNftsModal = ({ onClose }) => {
ignoreErc20Token,
} = useSelector((state) => state.appState.importNftsModal);
const existingNfts = useNftsCollections();

const [nftAddress, setNftAddress] = useState(initialTokenAddress ?? '');
const [tokenId, setTokenId] = useState(initialTokenId ?? '');
const [disabled, setDisabled] = useState(true);
Expand Down Expand Up @@ -141,7 +140,7 @@ export const ImportNftsModal = ({ onClose }) => {
const validateAndSetTokenId = (val) => {
setDuplicateTokenIdError(null);
// Check if tokenId is already imported
const tokenIdExists = tokenIdExist(
const tokenIdExists = checkTokenIdExists(
nftAddress,
val,
existingNfts.collections,
Expand Down
38 changes: 29 additions & 9 deletions ui/helpers/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
} from '../../../shared/constants/snaps';
///: END:ONLY_INCLUDE_IN
// formatData :: ( date: <Unix Timestamp> ) -> String
import { isEqualCaseInsensitive } from '../../../shared/modules/string-utils';

export function formatDate(date, format = "M/d/y 'at' T") {
if (!date) {
return '';
Expand Down Expand Up @@ -634,37 +636,55 @@ export const isAbleToExportAccount = (keyringType = '') => {
return !keyringType.includes('Hardware') && !keyringType.includes('Snap');
};

/**
* Converts hex string to decimal value.
*
* @param {*} hex - string hex to convert to decimal
* @returns string decimal
*/

export const hexToDecimal = (hex) => parseInt(hex, 16);

/**
* Converts decimal string to hex string
*
* @param {*} dec - string to convert to hex
* @returns string hex
*/
export const decimalToHex = (dec) => {
const decimalNumber = parseInt(dec, 10); // 10 is the base for decimal
return `0x${decimalNumber.toString(16)}`;
};

/**
* Checks if a tokenId in Hex or decimal format already exists in an object.
*
* @param {*} address - collection address.
* @param {*} tokId - tokenId to search for
* @param {*} tokenId - tokenId to search for
* @param {*} obj - object to look into
* @returns {boolean} `false` if tokenId does not already exist.
*/
export const tokenIdExist = (address, tokId, obj) => {
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
const isHex = isStrictHexString(tokId);
const isHex = isStrictHexString(tokenId);
let convertedTokenId;
if (isHex) {
// Convert to decimal
convertedTokenId = parseInt(tokId, 16);
convertedTokenId = hexToDecimal(tokenId);
} else {
// Convert to hex
const decimalNumber = parseInt(tokId, 10); // 10 is the base for decimal
convertedTokenId = `0x${decimalNumber.toString(16)}`;
convertedTokenId = decimalToHex(tokenId);
}

if (obj[address]) {
const value = obj[address];
return lodash.some(value.nfts, (nft) => {
return (
nft.address === address &&
(nft.tokenId.toLowerCase() === tokId.toLowerCase() ||
nft.tokenId.toLowerCase() ===
convertedTokenId.toString().toLowerCase())
(isEqualCaseInsensitive(nft.tokenId, tokenId) ||
isEqualCaseInsensitive(nft.tokenId, convertedTokenId.toString()))
);
});
}
Expand Down
26 changes: 21 additions & 5 deletions ui/helpers/utils/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,23 @@ describe('util', () => {
});
});

describe('tokenIdExist()', () => {
describe('hexToDecimal', () => {
it('should convert hex to decimal', () => {
const hexArray = ['0x1', '0x23', '0x24c', '0xa09']; // 1, 35, 588, 2569
const decArray = hexArray.map((hexValue) => util.hexToDecimal(hexValue));
expect(decArray).toStrictEqual([1, 35, 588, 2569]);
});
});

describe('decimalToHex', () => {
it('should decimal to hex', () => {
const decArray = ['1', '35', '588', '2569']; // '0x1', '0x23', '0x24C', '0xA09'
const hexArray = decArray.map((decValue) => util.decimalToHex(decValue));
expect(hexArray).toStrictEqual(['0x1', '0x23', '0x24c', '0xa09']);
});
});

describe('checkTokenIdExists()', () => {
const data = {
'0x2df920B180c58766951395c26ecF1EC2063490Fa': {
collectionName: 'Numbers',
Expand Down Expand Up @@ -959,7 +975,7 @@ describe('util', () => {
};
it('should return true if it exists', () => {
expect(
util.tokenIdExist(
util.checkTokenIdExists(
'0x2df920B180c58766951395c26ecF1EC206343334',
'3453',
data,
Expand All @@ -969,7 +985,7 @@ describe('util', () => {

it('should return true if it exists in decimal format', () => {
expect(
util.tokenIdExist(
util.checkTokenIdExists(
'0x2df920B180c58766951395c26ecF1EC206343334',
'0xD7D',
data,
Expand All @@ -979,7 +995,7 @@ describe('util', () => {

it('should return false if it does not exists', () => {
expect(
util.tokenIdExist(
util.checkTokenIdExists(
'0x2df920B180c58766951395c26ecF1EC206343334',
'1122',
data,
Expand All @@ -989,7 +1005,7 @@ describe('util', () => {

it('should return false if it address does not exists', () => {
expect(
util.tokenIdExist(
util.checkTokenIdExists(
'0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57',
'1122',
data,
Expand Down

0 comments on commit d2c6d07

Please sign in to comment.