-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test scenarios for wallet balance fetching
- Loading branch information
Showing
16 changed files
with
1,008 additions
and
176 deletions.
There are no files selected for viewing
475 changes: 475 additions & 0 deletions
475
...ound/services/balances/handlers/getTotalBalanceForWallet/getTotalBalanceForWallet.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 0 additions & 167 deletions
167
src/background/services/balances/handlers/getTotalBalanceForWallet/helpers.ts
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
...lances/handlers/getTotalBalanceForWallet/helpers/calculateTotalBalanceForAccounts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
...es/balances/handlers/getTotalBalanceForWallet/helpers/calculateTotalBalanceForAccounts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.