diff --git a/package.json b/package.json index 885062b0..ecdcac7f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "react-scripts build", "build:testing": "env-cmd -f .env.testing npm run build", "start": "ESLINT_NO_DEV_ERRORS=true react-scripts start", - "test": "react-scripts test --env=jsdom", + "test": "TZ='GMT+0200' react-scripts test --env=jsdom", "eject": "react-scripts eject", "lint": "eslint .", "lint:fix": "eslint --fix .", diff --git a/src/__tests__/utils/date.spec.js b/src/__tests__/utils/date.spec.js new file mode 100644 index 00000000..c33f6de0 --- /dev/null +++ b/src/__tests__/utils/date.spec.js @@ -0,0 +1,13 @@ +import {get24Time, getDate} from '../../utils'; + +describe('getDate', () => { + it('should get date', () => { + expect(getDate(1644828892412)).toEqual('14/02/2022'); + }); +}); + +describe('get24Time', () => { + it('should get 24 time', () => { + expect(get24Time(1644828892412).endsWith(':54:52')).toBeTruthy(); + }); +}); diff --git a/src/__tests__/utils/hash.spec.js b/src/__tests__/utils/hash.spec.js index 880f804c..af417a88 100644 --- a/src/__tests__/utils/hash.spec.js +++ b/src/__tests__/utils/hash.spec.js @@ -1,34 +1,39 @@ import {ChainType, TransactionHashPrefix} from '../../enums'; import {hashEquals, txHash} from '../../utils'; -it('should calc tx hash', () => { - const from_address = '0xcf98f0a8edc6a730e1ca6b64a2528c6be031cb12'; - const to_address = '1384622289134235426972866085149619554404298343372540338336104355150443775597'; - const selector = '1285101517810983806491589552491143496277809242732141897358598292095611420389'; - const nonce = '55'; - const payload = [ - '17466514784613283928575916580398045172482824287888203092305238190565527099', - '52145000000000', - '0' - ]; +describe('txHash', () => { + it('should calc tx hash', () => { + const from_address = '0xcf98f0a8edc6a730e1ca6b64a2528c6be031cb12'; + const to_address = + '1384622289134235426972866085149619554404298343372540338336104355150443775597'; + const selector = '1285101517810983806491589552491143496277809242732141897358598292095611420389'; + const nonce = '55'; + const payload = [ + '17466514784613283928575916580398045172482824287888203092305238190565527099', + '52145000000000', + '0' + ]; - expect( - txHash( - TransactionHashPrefix.L1_HANDLER, - from_address, - to_address, - selector, - payload, - ChainType.GOERLI.id, - nonce - ) - ).toEqual('0x35ab0e4de971ac0736844ef36a05796dc41490c165373923c423f4b995983e8'); + expect( + txHash( + TransactionHashPrefix.L1_HANDLER, + from_address, + to_address, + selector, + payload, + ChainType.GOERLI.id, + nonce + ) + ).toEqual('0x35ab0e4de971ac0736844ef36a05796dc41490c165373923c423f4b995983e8'); + }); }); -it('should compare hashes', () => { - expect(hashEquals([1, 2])).toBeTruthy(); - expect(hashEquals([1, 2, 3], [1, 2, 3])).toBeTruthy(); - expect(hashEquals([1, 2, 3], [1, 2, 3], [1, 2, 3])).toBeTruthy(); - expect(hashEquals([1, 2, 3], [1, 2, 3], [1])).toBeFalsy(); - expect(hashEquals([1, 2, 3], [1, 2])).toBeFalsy(); +describe('hashEquals', () => { + it('should compare hashes', () => { + expect(hashEquals([1, 2])).toBeTruthy(); + expect(hashEquals([1, 2, 3], [1, 2, 3])).toBeTruthy(); + expect(hashEquals([1, 2, 3], [1, 2, 3], [1, 2, 3])).toBeTruthy(); + expect(hashEquals([1, 2, 3], [1, 2, 3], [1])).toBeFalsy(); + expect(hashEquals([1, 2, 3], [1, 2])).toBeFalsy(); + }); }); diff --git a/src/__tests__/utils/number.spec.js b/src/__tests__/utils/number.spec.js new file mode 100644 index 00000000..1f2c1f28 --- /dev/null +++ b/src/__tests__/utils/number.spec.js @@ -0,0 +1,93 @@ +import { + parseFromDecimals, + parseFromFelt, + parseFromUint256, + parseToDecimals, + parseToUint256, + UNIT_MAP +} from '../../utils'; + +describe('UNIT_MAP', () => { + it('should init unit map', () => { + expect(UNIT_MAP).toEqual({ + 0: 'noether', + 1: 'wei', + 1000: 'femtoether', + 1000000: 'picoether', + 1000000000: 'nano', + 1000000000000: 'micro', + 1000000000000000: 'milli', + 1000000000000000000: 'ether', + '1000000000000000000000': 'grand', + '1000000000000000000000000': 'mether', + '1000000000000000000000000000': 'gether', + '1000000000000000000000000000000': 'tether' + }); + }); + + describe('parseToDecimals', () => { + it('should parse to decimals', () => { + expect(parseToDecimals('1')).toEqual('1000000000000000000'); + expect(parseToDecimals('1', 3)).toEqual('1000'); + expect(parseToDecimals('1', 6)).toEqual('1000000'); + expect(parseToDecimals('1', 9)).toEqual('1000000000'); + expect(parseToDecimals('1', 12)).toEqual('1000000000000'); + expect(parseToDecimals('1', 15)).toEqual('1000000000000000'); + expect(parseToDecimals('1', 18)).toEqual('1000000000000000000'); + }); + }); + + describe('parseFromDecimals', () => { + it('should parse from decimals', () => { + expect(parseFromDecimals('1000000000000000000')).toEqual(1); + expect(parseFromDecimals('1000000000000000000', 3)).toEqual(1000000000000000); + expect(parseFromDecimals('1000000000000000000', 6)).toEqual(1000000000000); + expect(parseFromDecimals('1000000000000000000', 9)).toEqual(1000000000); + expect(parseFromDecimals('1000000000000000000', 12)).toEqual(1000000); + expect(parseFromDecimals('1000000000000000000', 15)).toEqual(1000); + expect(parseFromDecimals('1000000000000000000', 18)).toEqual(1); + }); + }); + + describe('parseFromFelt', () => { + it('should parse from felt', () => { + expect(parseFromFelt('0x1')).toEqual(1); + expect(parseFromFelt('0x10')).toEqual(16); + expect(parseFromFelt('0x100')).toEqual(256); + }); + }); + + describe('parseToUint256', () => { + it('should parse to uint256', () => { + expect(parseToUint256('100')).toEqual({ + high: '0x0', + low: '0x56bc75e2d63100000', + type: 'struct' + }); + expect(parseToUint256('10000')).toEqual({ + high: '0x0', + low: '0x21e19e0c9bab2400000', + type: 'struct' + }); + expect(parseToUint256('10000000')).toEqual({ + high: '0x0', + low: '0x84595161401484a000000', + type: 'struct' + }); + }); + }); + + describe('parseFromUint256', () => { + it('should parse from uint256', () => { + expect(parseFromUint256({high: '0x0', low: '0x56bc75e2d63100000', type: 'struct'})).toEqual( + 100 + ); + expect(parseFromUint256({high: '0x0', low: '0x21e19e0c9bab2400000', type: 'struct'})).toEqual( + 10000 + ); + expect( + parseFromUint256({high: '0x0', low: '0x84595161401484a000000', type: 'struct'}) + ).toEqual(10000000); + }); + }); +}); diff --git a/src/__tests__/utils/object.spec.js b/src/__tests__/utils/object.spec.js index 431e61b2..2611e903 100644 --- a/src/__tests__/utils/object.spec.js +++ b/src/__tests__/utils/object.spec.js @@ -1,4 +1,39 @@ -import {findIndexById} from '../../utils'; +import {evaluate, findIndexById, getPropertyPath, toClasses} from '../../utils'; + +describe('getPropertyPath', () => { + const obj = { + a: 1, + b: 'hello', + c: { + d: true + } + }; + it('should get property by path', () => { + expect(getPropertyPath(obj, 'a')).toEqual(1); + expect(getPropertyPath(obj, 'b')).toEqual('hello'); + expect(getPropertyPath(obj, 'c')).toEqual({ + d: true + }); + expect(getPropertyPath(obj, 'c.d')).toBeTruthy(); + }); +}); + +describe('toClasses', () => { + it('should transform array to seperated string', () => { + expect(toClasses('c1', 'c2')).toEqual('c1 c2'); + }); +}); + +describe('evaluate', () => { + it('should evaluate handlebars in template', () => { + expect(evaluate('Hello {{name}}! this is my message', {name: 'foo'})).toEqual( + 'Hello foo! this is my message' + ); + expect(evaluate('{{host}}:{{port}}', {host: 'localhost', port: '8080'})).toEqual( + 'localhost:8080' + ); + }); +}); describe('findIndexById', () => { it("should return the matching-object's index (Number), accourding it's 'id'.", () => { diff --git a/src/__tests__/utils/string.spec.js b/src/__tests__/utils/string.spec.js new file mode 100644 index 00000000..ce74fb5e --- /dev/null +++ b/src/__tests__/utils/string.spec.js @@ -0,0 +1,8 @@ +import {capitalize} from '../../utils'; + +describe('capitalize', () => { + it('should capitalize string', () => { + expect(capitalize('hello')).toEqual('Hello'); + expect(capitalize('some message')).toEqual('Some message'); + }); +}); diff --git a/src/__tests__/utils/token.spec.js b/src/__tests__/utils/token.spec.js new file mode 100644 index 00000000..f7470b0b --- /dev/null +++ b/src/__tests__/utils/token.spec.js @@ -0,0 +1,19 @@ +import {isEth} from '../../utils'; + +describe('isEth', () => { + it('should return true for eth symbol as string', () => { + expect(isEth('ETH')).toBeTruthy(); + }); + + it('should return true for eth token as object', () => { + expect(isEth({symbol: 'ETH'})).toBeTruthy(); + }); + + it('should return false for non-eth symbol as string', () => { + expect(isEth('DAI')).toBeFalsy(); + }); + + it('should return false for non-eth token as object', () => { + expect(isEth({symbol: 'DAI'})).toBeFalsy(); + }); +}); diff --git a/src/__tests__/utils/wallet.spec.js b/src/__tests__/utils/wallet.spec.js new file mode 100644 index 00000000..148116f8 --- /dev/null +++ b/src/__tests__/utils/wallet.spec.js @@ -0,0 +1,31 @@ +import {formatBalance, shortenAddress} from '../../utils'; + +describe('formatBalance', () => { + it('should format balance to 5 digits precision', () => { + expect(formatBalance(1.222243232)).toEqual(1.22224); + expect(formatBalance(3000.232143123212)).toEqual(3000.23214); + expect(formatBalance(10.000000001)).toEqual(10); + }); + + it('should return N/A for non-numbers', () => { + expect(formatBalance('')).toBe('N/A'); + expect(formatBalance(null)).toBe('N/A'); + expect(formatBalance()).toBe('N/A'); + }); +}); + +describe('shortenAddress', () => { + it('should shorten long address', () => { + expect(shortenAddress('0x9e2bd0a6b6b98f4586a867678f5ebd3dcfda02e7')).toEqual('0x9e2...2e7'); + }); + + it('should return the same address for short address', () => { + expect(shortenAddress('0x9ed0a')).toEqual('0x9ed0a'); + }); + + it('should return empty string for bad input', () => { + expect(shortenAddress('')).toBe(''); + expect(shortenAddress()).toBe(''); + expect(shortenAddress(null)).toBe(''); + }); +}); diff --git a/src/utils/number.js b/src/utils/number.js index c8012581..7061ae51 100644 --- a/src/utils/number.js +++ b/src/utils/number.js @@ -2,7 +2,7 @@ import {web3, starknet} from '../libs'; const TEN = 10; const DEFAULT_DECIMALS = 18; -const UNIT_MAP = {}; +export const UNIT_MAP = {}; for (let key in web3.utils.unitMap) { UNIT_MAP[web3.utils.unitMap[key]] = key; diff --git a/src/utils/wallet.js b/src/utils/wallet.js index 88f8b9ae..48d6a695 100644 --- a/src/utils/wallet.js +++ b/src/utils/wallet.js @@ -2,8 +2,10 @@ export const formatBalance = balance => typeof balance === 'number' ? parseFloat(balance.toFixed(5)) : 'N/A'; export const shortenAddress = account => { - if (account) { - return `${account.substring(0, 5)}...${account.substring(account.length - 3)}`; + if (!account) { + return ''; } - return ''; + return account.length <= 8 + ? account + : `${account.substring(0, 5)}...${account.substring(account.length - 3)}`; };