Skip to content

Commit

Permalink
test: add test scenarios for wallet balance fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w committed Dec 1, 2024
1 parent d26c538 commit 3bc00fb
Show file tree
Hide file tree
Showing 16 changed files with 1,008 additions and 176 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
isImportedAccountsRequest,
} from './models';
import {
calculateTotalBalanceForAddresses,
calculateTotalBalanceForAccounts,
getAccountsWithActivity,
getAllAddressesForAccounts,
getIncludedNetworks,
Expand Down Expand Up @@ -61,7 +61,7 @@ export class GetTotalBalanceForWalletHandler implements HandlerType {
addr.replace(/^[PXC]-/i, '')
);
const underivedXPChainAddresses = secrets.xpubXP
? Object.keys(
? (
await getAccountsWithActivity(
secrets.xpubXP,
await this.networkService.getAvalanceProviderXP(),
Expand Down Expand Up @@ -114,7 +114,7 @@ export class GetTotalBalanceForWalletHandler implements HandlerType {
[TokenType.NATIVE, TokenType.ERC20]
);

let totalBalanceInCurrency = calculateTotalBalanceForAddresses(
let totalBalanceInCurrency = calculateTotalBalanceForAccounts(
derivedAddressesBalances,
derivedAccounts,
networksIncludedInTotal
Expand All @@ -134,7 +134,7 @@ export class GetTotalBalanceForWalletHandler implements HandlerType {
false // Don't cache this
);

const underivedAccountsTotal = calculateTotalBalanceForAddresses(
const underivedAccountsTotal = calculateTotalBalanceForAccounts(
underivedAddressesBalances,
underivedAccounts,
getXPChainIds(this.networkService.isMainnet())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Account } from '@src/background/services/accounts/models';
import { calculateTotalBalance } from '@src/utils/calculateTotalBalance';

import { calculateTotalBalanceForAccounts } from './calculateTotalBalanceForAccounts';

jest.mock('@src/utils/calculateTotalBalance');

describe('src/background/services/balances/handlers/helpers/calculateTotalBalanceForAccounts', () => {
it('aggregates results of calculateTotalBalance() for provided accounts', () => {
jest
.mocked(calculateTotalBalance)
.mockReturnValueOnce({
sum: 100,
priceChange: {
percentage: [0],
value: 0,
},
})
.mockReturnValueOnce({
sum: 0,
priceChange: {
percentage: [0],
value: 0,
},
})
.mockReturnValueOnce({
sum: null,
priceChange: {
percentage: [],
value: 0,
},
})
.mockReturnValueOnce({
sum: 1500,
priceChange: {
percentage: [0],
value: 0,
},
});

const accounts: Partial<Account>[] = [
{
addressAVM: 'addressAVM',
addressPVM: 'addressPVM',
},
{
addressPVM: 'addressPVM',
},
{
addressC: 'addressC',
addressBTC: 'addressBTC',
},
{
addressC: 'addressC',
addressAVM: 'addressAVM',
addressPVM: 'addressPVM',
},
];

const balances = {} as any;
const chainIds = [];

const result = calculateTotalBalanceForAccounts(
balances,
accounts,
chainIds
);

expect(calculateTotalBalance).toHaveBeenCalledTimes(4);
expect(calculateTotalBalance).toHaveBeenNthCalledWith(
1,
accounts[0],
chainIds,
balances
);
expect(calculateTotalBalance).toHaveBeenNthCalledWith(
2,
accounts[1],
chainIds,
balances
);
expect(calculateTotalBalance).toHaveBeenNthCalledWith(
3,
accounts[2],
chainIds,
balances
);
expect(calculateTotalBalance).toHaveBeenNthCalledWith(
4,
accounts[3],
chainIds,
balances
);

expect(result).toEqual(1600);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Account } from '@src/background/services/accounts/models';
import { calculateTotalBalance } from '@src/utils/calculateTotalBalance';

import { Balances } from '../../../models';

export function calculateTotalBalanceForAccounts(
balances: Balances,
accounts: Partial<Account>[],
chainIds: number[]
): number {
return accounts.reduce((sum: number, account: Partial<Account>) => {
const accountBalance = calculateTotalBalance(account, chainIds, balances);
return sum + (accountBalance.sum ?? 0);
}, 0);
}
Loading

0 comments on commit 3bc00fb

Please sign in to comment.