ghost-portal/packages/page-accounts/test/hooks/default.ts
Vitaliy 734c3c653a
Add sorting for accounts view (#5923)
* Show basic balance info

* Show bonded summary

* Show redeemable summary

* Show unbonding summary

* Adopt linter suggestions

* Hide cards with zero balance

* Apply simplifications

Co-authored-by: Jaco <jacogr@gmail.com>

* Remove dead code

* Simplify balance==undefined case

* Add summary bar tests (#5890)

* Add tests for summary bar

* Improve test case name

Co-authored-by: Krzysztof Jelski <kjelski@gmail.com>

* Factor empty render into a beforeEach block

* Improve name of helper variable

Co-authored-by: Krzysztof Jelski <kjelski@gmail.com>

* Construct correct instance for StakingLedger.active

* Override only the relevant parts in each test case

* Simplify test cases

Co-authored-by: Krzysztof Jelski <kjelski@gmail.com>

* Sort by total balance and by type

* Rename "clas" to "htmlClass"

* Extract DEFAULT_SORT_CONTROLS constant

* Move stableSort to its own file

* Optimize sorting on accounts page

* Adopt new sorting design

* Apply suggestions from code review

Co-authored-by: Jaco <jacogr@gmail.com>

* Remove stableSort function

* Move Dropdown css to Account styles

* Add tests for sorting

* Adjust tests and apply lint fixes

* Apply lint fixes

* Improve row coloring and styling of button for sort direction

* Improve styling

* Fix tests setup and bug connected with cloning accounts

* Restore condition

* Fix imports

Co-authored-by: Jaco <jacogr@gmail.com>
Co-authored-by: Krzysztof Jelski <kjelski@gmail.com>
Co-authored-by: MiZiet <michalzietalak@o2.pl>
Co-authored-by: Ewa Kowalska <ewcia.kowalska@gmail.com>
2021-09-06 14:14:56 +02:00

132 lines
4.4 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';
import { KeyringJson$Meta } from '@polkadot/ui-keyring/types';
export interface Account {
balance: DeriveBalancesAll,
info: UseAccountInfo,
staking: DeriveStakingAccount
}
export type AccountsMap = { [address: string]: Account };
export type Override<T> = {
[P in keyof T]?: T[P];
}
/**
* Test inputs structure
*/
export interface AccountOverrides {
meta?: Override<KeyringJson$Meta>;
balance?: Override<DeriveBalancesAll>;
staking?: Override<DeriveStakingAccount>;
info?: Override<UseAccountInfo>;
}
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;
export const defaultMeta: KeyringJson$Meta = {};
// 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 meta = { ...defaultMeta };
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.meta || meta).forEach(function ([key, value]) { (meta as any)[key] = value; });
// 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();