Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add utils tests #80

Merged
merged 5 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/utils/date.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
59 changes: 32 additions & 27 deletions src/__tests__/utils/hash.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
93 changes: 93 additions & 0 deletions src/__tests__/utils/number.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
37 changes: 36 additions & 1 deletion src/__tests__/utils/object.spec.js
Original file line number Diff line number Diff line change
@@ -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'.", () => {
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/utils/string.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
19 changes: 19 additions & 0 deletions src/__tests__/utils/token.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
31 changes: 31 additions & 0 deletions src/__tests__/utils/wallet.spec.js
Original file line number Diff line number Diff line change
@@ -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('');
});
});
2 changes: 1 addition & 1 deletion src/utils/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/utils/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`;
};