* Retrofitting basic unit tests to Accounts Extraction of useAccounts and useBalance hooks * Retrofitting basic unit tests to Accounts Extraction of useAccounts and useBalance hooks * Fixing accounts unit tests - basic account balances * Cleaning up after PR remarks * Fixing arrangements of balances 1/3 Moving ArrangedAccount around * Add useStakingInfo hook * Adopt linter suggestions * Fixing arrangements of balances 2/3 Linking arranged balances values to mocked balances calls. * Fixing arrangements of balances 3/3 Formatting utils made consistent * Details row odd-even + (un)hide * Improving th/td/footer layout * Add tags to details row * Changing layout of account balance with details * Add total/details balances unit tests * Add tags unit tests * Clean up of TODOs, adding comments * Improve tags unit testing * Alternate account row details coloring unit test * Row details toggling unit tests * Row details toggling unit tests - bad commit fix * Style address balances (#5897) * Fix jumping column problem * Improve balances positioning * Format sidebar * Position header * Extract common formatting of balances in tests. Thx @vitaliyworks, great hint. * Revert Sidebar styling * Use rems * Lint * Use useToggle for expanding account row * Add tags header * Apply FormatBalance review comments * Display parent account info above account name (#5947) * Display parent address * Improve layout * Set keyring in tests * Remove redundant code from components * Use derivedAccount only in specific test * Apply lint fixes * Remove parent column from expanded row * Apply review suggestions * Improve styling in ParentAddress component * Improve layout of parent address and add short address * Apply review suggestions * Use map instead of for loops * Fix display of AddressSmall * Fix height of icons in AddressSmall * Fix bouncing cells in address row * Fix density of accounts display Co-authored-by: Marcin Wachulski <marcin@ethworks.io> Co-authored-by: Vitaliy Mysak <vitaliy@ethworks.io> Co-authored-by: MiZiet <55240109+MiZiet@users.noreply.github.com> Co-authored-by: MiZiet <michalzietalak@o2.pl> Co-authored-by: Jaco Greeff <jacogr@gmail.com>
128 lines
4.1 KiB
TypeScript
128 lines
4.1 KiB
TypeScript
// Copyright 2017-2021 @polkadot/page-accounts authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { DeriveBalancesAll, DeriveStakingAccount } from '@polkadot/api-derive/types';
|
|
import type { UseAccountInfo } from '@polkadot/react-hooks/types';
|
|
|
|
import BN from 'bn.js';
|
|
|
|
import { UseAccounts } from '@polkadot/react-hooks/useAccounts';
|
|
import { balanceOf } from '@polkadot/test-support/creation/balance';
|
|
import { makeStakingLedger } from '@polkadot/test-support/creation/stakingInfo/stakingLedger';
|
|
|
|
export interface Account {
|
|
balance: DeriveBalancesAll,
|
|
info: UseAccountInfo,
|
|
staking: DeriveStakingAccount
|
|
}
|
|
|
|
export type AccountsMap = { [address: string]: Account };
|
|
|
|
/**
|
|
* Test inputs structure
|
|
*/
|
|
export interface AccountOverrides {
|
|
balance?: {
|
|
[P in keyof DeriveBalancesAll]?: DeriveBalancesAll[P];
|
|
};
|
|
staking?: {
|
|
[P in keyof DeriveStakingAccount]?: DeriveStakingAccount[P];
|
|
};
|
|
info?: {
|
|
[P in keyof UseAccountInfo]?: UseAccountInfo[P];
|
|
}
|
|
}
|
|
|
|
export const emptyAccounts: UseAccounts = {
|
|
allAccounts: [],
|
|
allAccountsHex: [],
|
|
areAccountsLoaded: true,
|
|
hasAccounts: false,
|
|
isAccount: () => true
|
|
};
|
|
|
|
// here it's extremely hard to reconstruct the entire DeriveBalancesAll upfront, so we incrementally add properties
|
|
// instead along the way; thus the need to tell the tsc we know what we are doing here
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
export const defaultBalanceAccount: DeriveBalancesAll = {
|
|
accountNonce: new BN(1),
|
|
additional: [],
|
|
availableBalance: balanceOf(0),
|
|
freeBalance: balanceOf(0),
|
|
lockedBalance: balanceOf(0),
|
|
lockedBreakdown: [],
|
|
reservedBalance: balanceOf(0)
|
|
} as any;
|
|
|
|
// here it's extremely hard to reconstruct the entire DeriveStakingAccount upfront,
|
|
// so we set just the properties that we use in page-accounts
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
export const defaultStakingAccount: DeriveStakingAccount = {
|
|
nextSessionIds: [],
|
|
nominators: [],
|
|
redeemable: balanceOf(0),
|
|
sessionIds: [],
|
|
stakingLedger: makeStakingLedger(0),
|
|
unlocking: [
|
|
{
|
|
remainingEras: new BN('1000000000'),
|
|
value: balanceOf(0)
|
|
},
|
|
{
|
|
remainingEras: new BN('2000000000'),
|
|
value: balanceOf(0)
|
|
},
|
|
{
|
|
remainingEras: new BN('3000000000'),
|
|
value: balanceOf(0)
|
|
}
|
|
]
|
|
} as any;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
export const defaultAccountInfo: UseAccountInfo = {
|
|
flags: {},
|
|
tags: []
|
|
} as any;
|
|
|
|
class MockAccountHooks {
|
|
public useAccounts: UseAccounts = emptyAccounts;
|
|
public accountsMap: AccountsMap = {};
|
|
|
|
public nonce: BN = new BN(1);
|
|
|
|
public setAccounts (accounts: [string, AccountOverrides][]): void {
|
|
this.useAccounts = {
|
|
allAccounts: accounts.map(([address]) => address),
|
|
allAccountsHex: [],
|
|
areAccountsLoaded: true,
|
|
hasAccounts: accounts && accounts.length !== 0,
|
|
isAccount: () => true
|
|
};
|
|
|
|
for (const [address, props] of accounts) {
|
|
const staking = { ...defaultStakingAccount };
|
|
const balance = { ...defaultBalanceAccount };
|
|
const info = { ...defaultAccountInfo };
|
|
|
|
// Typescript does not recognize that keys and values from Object.entries are safe,
|
|
// so we have to use "any" here.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
Object.entries(props.balance || balance).forEach(function ([key, value]) { (balance as any)[key] = value; });
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
Object.entries(props.staking || staking).forEach(function ([key, value]) { (staking as any)[key] = value; });
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
Object.entries(props.info || info).forEach(function ([key, value]) { (info as any)[key] = value; });
|
|
|
|
this.accountsMap[address] = {
|
|
balance: balance,
|
|
info: info,
|
|
staking: staking
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
export const mockAccountHooks = new MockAccountHooks();
|