Skip to content

Commit

Permalink
test(coinmarket): add tests to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Jul 29, 2024
1 parent 5e6d15e commit e12a93f
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DefinitionType, TokenDefinitions } from '@suite-common/token-definitions';
import { Account } from '@suite-common/wallet-types';

export const accountBtc = {
index: 1,
Expand Down Expand Up @@ -55,3 +56,59 @@ export const coinDefinitions: TokenDefinitions[DefinitionType.COIN] = {
hide: [],
show: [],
};

export const FIXTURE_ACCOUNTS: Partial<Account>[] = [
{
deviceState: 'deviceState',
formattedBalance: '0',
tokens: [],
descriptor: 'descriptor1',
symbol: 'btc',
},
{
deviceState: 'deviceState',
formattedBalance: '0.101213',
tokens: [],
descriptor: 'descriptor2',
symbol: 'ltc',
},
{
deviceState: 'deviceState',
formattedBalance: '0',
descriptor: 'descriptor3',
symbol: 'eth',
tokens: [
{
balance: '2.76149',
contract: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
decimals: 6,
name: 'Tether USD',
symbol: 'usdt',
type: 'ERC20',
},
],
},
{
deviceState: 'no-deviceState',
formattedBalance: '0.101213',
tokens: [],
descriptor: 'descriptor4',
symbol: 'btc',
},
{
deviceState: 'no-deviceState',
formattedBalance: '0.101213',
symbol: 'matic',
tokens: [
{
balance: '2.76149',
contract: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
decimals: 6,
name: 'Tether USD',
symbol: 'usdt',
type: 'ERC20',
},
],
descriptor: 'descriptor5',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ import {
coinmarketBuildCryptoOptions,
addIdsToQuotes,
filterQuotesAccordingTags,
coinmarketGetSortedAccountsWithBalance,
coinmarketBuildAccountOptions,
coinmarketGetRoundedFiatAmount,
coinmarketGetAmountLabels,
} from '../coinmarketUtils';
import { accountBtc, accountEth, coinDefinitions } from '../__fixtures__/coinmarketUtils';
import {
FIXTURE_ACCOUNTS,
accountBtc,
accountEth,
coinDefinitions,
} from '../__fixtures__/coinmarketUtils';
import * as BUY_FIXTURE from 'src/utils/wallet/coinmarket/__fixtures__/buyUtils';
import * as SELL_FIXTURE from 'src/utils/wallet/coinmarket/__fixtures__/sellUtils';
import * as EXCHANGE_FIXTURE from 'src/utils/wallet/coinmarket/__fixtures__/exchangeUtils';
Expand All @@ -25,6 +34,12 @@ import {
CryptoCategoryD,
CryptoCategoryE,
} from 'src/constants/wallet/coinmarket/cryptoCategories';
import { useAccountLabel } from 'src/components/suite/AccountLabel';

jest.mock('src/components/suite/AccountLabel', () => ({
...jest.requireActual('src/components/suite/AccountLabel'),
useAccountLabel: jest.fn(),
}));

describe('coinmarket utils', () => {
it('buildFiatOption', () => {
Expand Down Expand Up @@ -323,4 +338,117 @@ describe('coinmarket utils', () => {
},
]);
});

it('coinmarketGetSortedAccountsWithBalance', () => {
const sortedAccountsWithBalance = coinmarketGetSortedAccountsWithBalance({
accounts: FIXTURE_ACCOUNTS as Account[],
deviceState: 'deviceState',
});

expect(sortedAccountsWithBalance).toStrictEqual([FIXTURE_ACCOUNTS[1], FIXTURE_ACCOUNTS[2]]);
});

it('coinmarketBuildAccountOptions', () => {
const symbolsInfo: CryptoSymbolInfo[] = [
{
symbol: 'BTC',
name: 'Bitcoin',
category: 'Popular currencies',
},
{
symbol: 'LTC',
name: 'Litecoin',
category: 'Popular currencies',
},
{
symbol: 'USDT@ETH',
name: 'Tether',
category: 'Ethereum ERC20 tokens',
},
{
symbol: 'ETH',
name: 'Ethereum',
category: 'Popular currencies',
},
];
const label = 'mocked label';
const defaultAccountLabelString = (useAccountLabel as jest.Mock).mockImplementation(
() => label,
);

const sortedAccountsWithBalance = coinmarketBuildAccountOptions({
accounts: FIXTURE_ACCOUNTS as Account[],
deviceState: 'deviceState',
accountLabels: {},
defaultAccountLabelString,
symbolsInfo,
});

expect(sortedAccountsWithBalance).toStrictEqual([
{
label,
options: [
{
balance: '0.101213',
cryptoName: 'Litecoin',
descriptor: 'descriptor2',
label: 'LTC',
value: 'LTC',
},
],
},
{
label,
options: [
{
balance: '0',
cryptoName: 'Ethereum',
descriptor: 'descriptor3',
label: 'ETH',
value: 'ETH',
},
{
balance: '2.76149',
contractAddress: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
cryptoName: 'Tether',
descriptor: 'descriptor3',
label: 'USDT',
value: 'USDT@ETH',
},
],
},
]);
});

it('coinmarketGetAmountLabels', () => {
expect(coinmarketGetAmountLabels({ type: 'sell', amountInCrypto: true })).toEqual({
label1: 'TR_COINMARKET_YOU_PAY',
label2: 'TR_COINMARKET_YOU_GET',
labelComparatorOffer: 'TR_COINMARKET_YOU_WILL_GET',
});

expect(coinmarketGetAmountLabels({ type: 'sell', amountInCrypto: false })).toEqual({
label1: 'TR_COINMARKET_YOU_GET',
label2: 'TR_COINMARKET_YOU_PAY',
labelComparatorOffer: 'TR_COINMARKET_YOU_WILL_PAY',
});

expect(coinmarketGetAmountLabels({ type: 'buy', amountInCrypto: true })).toEqual({
label1: 'TR_COINMARKET_YOU_GET',
label2: 'TR_COINMARKET_YOU_PAY',
labelComparatorOffer: 'TR_COINMARKET_YOU_WILL_PAY',
});

expect(coinmarketGetAmountLabels({ type: 'buy', amountInCrypto: false })).toEqual({
label1: 'TR_COINMARKET_YOU_PAY',
label2: 'TR_COINMARKET_YOU_GET',
labelComparatorOffer: 'TR_COINMARKET_YOU_WILL_GET',
});
});

it('coinmarketBuildAccountOptions', () => {
expect(coinmarketGetRoundedFiatAmount('0.23923')).toBe('0.24');
expect(coinmarketGetRoundedFiatAmount('0.24423')).toBe('0.24');
expect(coinmarketGetRoundedFiatAmount('0.2')).toBe('0.20');
});
});
6 changes: 3 additions & 3 deletions packages/suite/src/utils/wallet/coinmarket/coinmarketUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export const coinmarketGetSortedAccountsWithBalance = ({

const accountSorted = sortByCoin(accounts.filter(a => a.deviceState === deviceState));
const accountsWithBalance = accountSorted.filter(account => {
const accountWithBalance = account.availableBalance !== '0';
const accountWithBalance = account.formattedBalance !== '0';
const tokens = account.tokens?.filter(token => token.balance !== '0');

return accountWithBalance || (tokens && tokens?.length > 0);
Expand Down Expand Up @@ -448,7 +448,7 @@ export const coinmarketBuildAccountOptions = ({
label: accountSymbol.toUpperCase(),
cryptoName: foundSymbolInfo?.name ?? null,
descriptor,
balance: formattedBalance,
balance: formattedBalance ?? '',
},
];

Expand All @@ -475,7 +475,7 @@ export const coinmarketBuildAccountOptions = ({
cryptoName: tokenSymbolInfo?.name ?? null,
contractAddress: contract,
descriptor,
balance,
balance: balance ?? '',
});
});
}
Expand Down

0 comments on commit e12a93f

Please sign in to comment.