134 lines
4.0 KiB
TypeScript
134 lines
4.0 KiB
TypeScript
// Copyright 2017-2021 @polkadot/page-accounts authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import { render, RenderResult, screen, within } from '@testing-library/react';
|
|
import BN from 'bn.js';
|
|
import React, { Suspense } from 'react';
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
import { ThemeProvider } from 'styled-components';
|
|
|
|
import { lightTheme } from '@polkadot/apps/themes';
|
|
import { POLKADOT_GENESIS } from '@polkadot/apps-config';
|
|
import { ApiContext } from '@polkadot/react-api';
|
|
import { ApiProps } from '@polkadot/react-api/types';
|
|
import { QueueProvider } from '@polkadot/react-components/Status/Context';
|
|
import { PartialQueueTxExtrinsic, QueueProps, QueueTxExtrinsicAdd } from '@polkadot/react-components/Status/types';
|
|
import { TypeRegistry } from '@polkadot/types/create';
|
|
import { Balance, BlockNumber } from '@polkadot/types/interfaces';
|
|
import { formatBalance } from '@polkadot/util';
|
|
|
|
import { AccountOverrides, mockAccountHooks } from '../hooks/default';
|
|
import Overview from '../pages/../../src/Accounts/index';
|
|
|
|
let queueExtrinsic: (value: PartialQueueTxExtrinsic) => void;
|
|
|
|
class NotYetRendered extends Error {
|
|
}
|
|
|
|
export type AccountRow = HTMLElement;
|
|
|
|
jest.mock('@polkadot/react-hooks/useAccounts', () => ({
|
|
useAccounts: () => mockAccountHooks.useAccounts
|
|
}));
|
|
|
|
jest.mock('@polkadot/react-hooks/useLoadingDelay', () => ({
|
|
useLoadingDelay: () => false
|
|
}));
|
|
|
|
jest.mock('@polkadot/react-hooks/useBalancesAll', () => ({
|
|
useBalancesAll: (address: string) => mockAccountHooks.accountsMap[address].balance
|
|
}));
|
|
|
|
jest.mock('@polkadot/react-hooks/useStakingInfo', () => ({
|
|
useStakingInfo: (address: string) => mockAccountHooks.accountsMap[address].staking
|
|
}));
|
|
|
|
jest.mock('@polkadot/react-hooks/useBestNumber', () => ({
|
|
useBestNumber: () => 1
|
|
}));
|
|
|
|
export class AccountsPage {
|
|
private renderResult?: RenderResult
|
|
|
|
renderPage (accounts: [string, AccountOverrides][]): void {
|
|
mockAccountHooks.setAccounts(accounts);
|
|
const mockApi: ApiProps = {
|
|
api: {
|
|
derive: {
|
|
accounts: {
|
|
info: () => Promise.resolve(() => { /**/
|
|
})
|
|
},
|
|
balances: {
|
|
all: () => ({
|
|
accountNonce: mockAccountHooks.nonce,
|
|
additional: [],
|
|
freeBalance: 0,
|
|
lockedBreakdown: [],
|
|
reservedBalance: 0
|
|
})
|
|
},
|
|
chain: {
|
|
bestNumber: () => new BN(1) as BlockNumber
|
|
}
|
|
},
|
|
genesisHash: new TypeRegistry().createType('Hash', POLKADOT_GENESIS),
|
|
query: {},
|
|
registry: { chainDecimals: [12], chainTokens: ['Unit'] },
|
|
tx: {
|
|
council: {
|
|
}
|
|
}
|
|
},
|
|
systemName: 'substrate'
|
|
} as unknown as ApiProps;
|
|
|
|
queueExtrinsic = jest.fn() as QueueTxExtrinsicAdd;
|
|
const queue = {
|
|
queueExtrinsic
|
|
} as QueueProps;
|
|
|
|
this.renderResult = render(
|
|
<>
|
|
<div id='tooltips'/>
|
|
<Suspense fallback='...'>
|
|
<QueueProvider value={queue}>
|
|
<MemoryRouter>
|
|
<ThemeProvider theme={lightTheme}>
|
|
<ApiContext.Provider value={mockApi}>
|
|
<Overview/>
|
|
</ApiContext.Provider>
|
|
</ThemeProvider>
|
|
</MemoryRouter>
|
|
</QueueProvider>
|
|
</Suspense>
|
|
</>
|
|
);
|
|
}
|
|
|
|
async findAccountsTable (): Promise<HTMLElement> {
|
|
this.assertRendered();
|
|
|
|
return await screen.findByRole('table');
|
|
}
|
|
|
|
async findAccountRows (): Promise<AccountRow[]> {
|
|
this.assertRendered();
|
|
const table = await this.findAccountsTable();
|
|
const tableBody = table.getElementsByTagName('tbody')[0];
|
|
const rows = await within(tableBody).findAllByRole('row');
|
|
|
|
return rows.filter((r) => r.className.startsWith('Account-'));
|
|
}
|
|
|
|
format (amount: Balance | BN): string {
|
|
return formatBalance(amount, { decimals: 12, forceUnit: '-', withUnit: true });
|
|
}
|
|
|
|
private assertRendered () {
|
|
if (this.renderResult === undefined) {
|
|
throw new NotYetRendered();
|
|
}
|
|
}
|
|
}
|