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

Feat: add tests #117

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
122 changes: 122 additions & 0 deletions src/common/utils/balance/balance.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import BigNumber from 'bignumber.js';
import { AssetAccount, TrasferAsset } from '@/common/types';
import { formatBalance, formatAmount, getTotalBalance, getTransferDetails, updateAssetsBalance } from './index';
import { Chain } from '@/common/chainRegistry/types';

describe('formatBalance function', () => {
test('should calculate small amount', () => {
const { formattedValue, suffix, decimalPlaces } = formatBalance('592321079928', 12);

expect(formattedValue).toEqual('0.59232');
expect(suffix).toEqual('');
expect(decimalPlaces).toEqual(5);
});

test('should calculate thousands', () => {
const { formattedValue, suffix, decimalPlaces } = formatBalance('16172107992822306', 12);

expect(formattedValue).toEqual('16172.1');
expect(suffix).toEqual('');
expect(decimalPlaces).toEqual(2);
});

test('should calculate millions', () => {
const { formattedValue, suffix, decimalPlaces } = formatBalance('1617210799282230602', 12);

expect(formattedValue).toEqual('1.61');
expect(suffix).toEqual('M');
expect(decimalPlaces).toEqual(2);
});

test('should calculate billion', () => {
const { formattedValue, suffix, decimalPlaces } = formatBalance('8717210799282230602024', 12);

expect(formattedValue).toEqual('8.71');
expect(suffix).toEqual('B');
expect(decimalPlaces).toEqual(2);
});

test('should calculate trillion', () => {
const { formattedValue, suffix, decimalPlaces } = formatBalance('91528717210799282230602024', 12);

expect(formattedValue).toEqual('91.52');
expect(suffix).toEqual('T');
expect(decimalPlaces).toEqual(2);
});
});

describe('formatAmount function', () => {
test('should freturn zero value with empty string', () => {
const amount = '';
const formattedAmount = formatAmount(amount, 12);

expect(formattedAmount).toBe('0');
});

test('should format amount', () => {
const amount = '5';
const formattedAmount = formatAmount(amount, 12);

expect(formattedAmount).toBe('5000000000000');
});
test('should format amount with precision', () => {
const amount = '0.92321';
const formattedAmount = formatAmount(amount, 12);

expect(formattedAmount).toBe('923210000000');
});
});

describe('getTotalBalance function', () => {
test('should return total balance', () => {
const assets = [
{ totalBalance: '100', asset: { priceId: '1' } },
{ totalBalance: '200', asset: { priceId: '2' } },
];
const assetsPrices = { '1': { price: 1 }, '2': { price: 2 } };
const totalBalance = getTotalBalance(assets as AssetAccount[], assetsPrices);

expect(totalBalance).toBe(500);
});
});

describe('updateAssetsBalance function', () => {
test('should update assets balances', () => {
const prevAssets = [
{ chainId: 'chain1', totalBalance: '100' },
{ chainId: 'chain2', totalBalance: '200' },
];
const chain = { chainId: 'chain1' };
const balance = { total: () => '300', transferable: () => '250' };
const updatedAssets = updateAssetsBalance(prevAssets as AssetAccount[], chain as Chain, balance);

expect(updatedAssets[0].totalBalance).toBe('300');
expect(updatedAssets[0].transferableBalance).toBe('250');
expect(updatedAssets[1].totalBalance).toBe('200');
});
});

describe('getTransferDetails function', () => {
test('should return transfer details', async () => {
const selectedAsset = {
chainId: 'chain1',
transferableBalance: '100000000000',
asset: { precision: 12 },
isGift: false,
};
const amount = '50';
const estimateFee = jest.fn().mockResolvedValue(new BigNumber(0.00001));
const getExistentialDeposit = jest.fn().mockResolvedValue('10000000000');

const transferDetails = await getTransferDetails(
selectedAsset as TrasferAsset,
amount,
estimateFee,
getExistentialDeposit,
);

expect(transferDetails.fee).toBe(0.00001);
expect(transferDetails.max).toBe('0.10000');
expect(transferDetails.formattedDeposit).toBe(0.01);
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import BigNumber from 'bignumber.js';
import { BN, BN_TEN } from '@polkadot/util';

import { AssetAccount, AssetPrice, ChainAssetAccount, ChainAssetId, ChainId, TrasferAsset } from '../types';
import { Chain } from '../chainRegistry/types';
import { IAssetBalance } from '../balances/types';
import { EstimateFee, GetExistentialDeposit } from '../extrinsicService/types';
import { handleFee } from './extrinsics';
import { AssetAccount, AssetPrice, ChainAssetAccount, ChainAssetId, ChainId, TrasferAsset } from '../../types';
import { Chain } from '../../chainRegistry/types';
import { IAssetBalance } from '../../balances/types';
import { EstimateFee, GetExistentialDeposit } from '../../extrinsicService/types';
import { handleFee } from '../extrinsics';

const ZERO_BALANCE = '0';

Expand Down
42 changes: 42 additions & 0 deletions src/common/utils/extrinsics/extrinsics.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { handleSend, handleSendGift } from './index';
import { TrasferAsset } from '../../types';

const mockSubmitExtrinsic = jest.fn().mockResolvedValue('successHash');
const mockEstimateFee = jest.fn().mockResolvedValue(10);
console.log = jest.fn();

const transferAsset = {
destinationAddress: '0x1234567890123456789012345678901234567890',
chainId: 'chainId',
amount: '100',
transferAll: false,
asset: { precision: 18 },
};

describe('handleSend function', () => {
test('should handle sending of funds', async () => {
await handleSend(mockSubmitExtrinsic, transferAsset as TrasferAsset);

expect(mockSubmitExtrinsic).toHaveBeenCalledWith('chainId', expect.any(Function));
expect(console.log).toHaveBeenCalledWith('Success, Hash:', 'successHash');
});
});

describe('handleSendGift function', () => {
test('should calculate the amount of a gift', async () => {
await handleSendGift(
mockSubmitExtrinsic,
mockEstimateFee,
transferAsset as TrasferAsset,
'0x1234567890123456789012345678901234567890',
);
expect(mockSubmitExtrinsic).toHaveBeenCalledWith('chainId', expect.any(Function));

const transferAmount = (+transferAsset.amount * Math.pow(10, 18)).toString();
expect(transferAmount).toEqual('100000000000000000000');

const fee = 10000000000000;
const giftAmount = (+transferAmount + fee).toString();
expect(giftAmount).toEqual('100000010000000000000');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { decodeAddress } from '@polkadot/util-crypto';
import { KeyringPair } from '@polkadot/keyring/types';
import { Balance } from '@polkadot/types/interfaces';

import { EstimateFee, ExtrinsicBuilder, SubmitExtrinsic } from '../extrinsicService/types';
import { formatAmount } from './balance';
import { Address, ChainId, TrasferAsset } from '../types';
import { FAKE_ACCOUNT_ID } from './constants';
import { EstimateFee, ExtrinsicBuilder, SubmitExtrinsic } from '../../extrinsicService/types';
import { formatAmount } from '../balance';
import { Address, ChainId, TrasferAsset } from '../../types';
import { FAKE_ACCOUNT_ID } from '../constants';

const transferExtrinsic = async (
submitExtrinsic: SubmitExtrinsic,
Expand Down
Loading