ghost-portal/packages/test-support/src/pages/Page.tsx
MiZiet 053e0efca2
Split registrars display (#6157)
* Add subs modal

* Add tests

* Add Translations

* Improve closing modal

* Improve tests

* Improve styles

* Style balances & identity

* Optimize imports

* Improve styles

* Set greater width to balance section

* Rename subs modal toggle

* Change 'amount' to 'count' in test name

* Set make modal header fixed while content is scrollable

* Style multisig

* Update multisig styles

* Capitalize headers

* Move sidebar tests to Sidebar.spec.tsx

* Remove duplications in tests

* Add UserIcon component

* Revert subs modal changes and add scrollable section in Sidebar

* Remove unused styles

* Use accountsPage clearAccount method

* Wrap subs into Expander

* Add useSubidentities hook

* Adjust api mock in tests

* WIP - split judgements

* Display judgements

* Handle no judgements case

* [WIP] Add popup test

* Extract JudgmentTag component

* Add popup test

* Refactor tests and remove pointer from no judgements tag

* Extract JudgementTag test class

* Close popup on scroll

* Remove old section

* Clean registration

* Clean up identity in useAccountInfo and mock in Page

* Remove fragment tag

* Fix imports

* Move SortedJudgements to /types

* Move Registrar interface to types

* Refactor clickRegistrar fcn

* Move hasAccount check to isRegistrar

* Add useJudgements hook

* Rename and move getJudgementColor to utils

* Fix import and casting in tests

* Sort imports

* Add review fixes

* Fix tests

Co-authored-by: Krzysztof Jelski <krzysztof@ethworks.io>
Co-authored-by: Ewa Kowalska <ewcia.kowalska@gmail.com>
Co-authored-by: Jaco Greeff <jacogr@gmail.com>
2021-09-29 10:45:34 +02:00

187 lines
5.6 KiB
TypeScript

// Copyright 2017-2021 @polkadot/page-accounts authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { render, RenderResult, screen } from '@testing-library/react';
import React, { Suspense } from 'react';
import { MemoryRouter } from 'react-router-dom';
import { ThemeProvider } from 'styled-components';
import AccountSidebar from '@polkadot/app-accounts/Sidebar';
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 { UseAccountInfo } from '@polkadot/react-hooks/types';
import { mockApiHooks } from '@polkadot/test-support/utils/mockApiHooks';
import { TypeRegistry } from '@polkadot/types/create';
import { keyring } from '@polkadot/ui-keyring';
import { alice, bob, charlie, ferdie } from '../keyring';
import { Table } from '../pagesElements';
import { AccountOverrides, mockAccountHooks } from '../utils/accountDefaults';
let queueExtrinsic: (value: PartialQueueTxExtrinsic) => void;
class NotYetRendered extends Error {
}
jest.mock('@polkadot/react-hooks/useAccounts', () => ({
useAccounts: () => mockAccountHooks.useAccounts
}));
jest.mock('@polkadot/react-hooks/useAccountInfo', () => {
// eslint-disable-next-line func-call-spacing
const actual = jest.requireActual<{useAccountInfo: (address: string) => UseAccountInfo}>('@polkadot/react-hooks/useAccountInfo');
return ({
useAccountInfo: (address: string) => {
const mockInfo = mockAccountHooks.accountsMap[address];
return mockInfo
? {
...actual.useAccountInfo(address),
flags: { ...actual.useAccountInfo(address).flags, ...(mockInfo.info.flags) },
identity: {
...actual.useAccountInfo(address).identity,
...(mockInfo.info.identity),
judgements: [
...(actual.useAccountInfo(address).identity?.judgements || []),
...(mockApiHooks.judgements || [])
]
},
tags: [...actual.useAccountInfo(address).tags, ...(mockInfo.info.tags)]
}
: actual.useAccountInfo(address);
}
});
});
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
}));
jest.mock('@polkadot/react-hooks/useSubidentities', () => ({
useSubidentities: () => mockApiHooks.subs
}));
jest.mock('@polkadot/react-hooks/useSubidentities', () => ({
useSubidentities: () => mockApiHooks.subs
}));
jest.mock('@polkadot/react-hooks/useRegistrars', () => ({
useRegistrars: () => ({
isRegistrar: false,
registrars: mockApiHooks.registrars
})
}));
export abstract class Page {
private renderResult?: RenderResult
protected readonly defaultAddresses = [alice, bob, charlie, ferdie];
protected constructor (private readonly overview: React.ReactElement, private readonly rowClassName: string) {
this.overview = overview;
this.rowClassName = rowClassName;
}
render (accounts: [string, AccountOverrides][]): void {
mockAccountHooks.setAccounts(accounts);
accounts.forEach(([address, { meta }]) => {
keyring.addExternal(address, meta);
});
const noop = () => Promise.resolve(() => { /**/ });
const mockApi: ApiProps = {
api: {
derive: {
accounts: {
info: noop
},
balances: {
all: noop
},
chain: {
bestNumber: noop
},
democracy: {
locks: noop
},
staking: {
account: noop
}
},
genesisHash: new TypeRegistry().createType('Hash', POLKADOT_GENESIS),
query: {
democracy: {
votingOf: noop
},
identity: {
identityOf: noop
}
},
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}>
<AccountSidebar>
{React.cloneElement(this.overview, { onStatusChange: noop }) }
</AccountSidebar>
</ApiContext.Provider>
</ThemeProvider>
</MemoryRouter>
</QueueProvider>
</Suspense>
</>
);
}
async getTable (): Promise<Table> {
this.assertRendered();
return new Table(await screen.findByRole('table'), this.rowClassName);
}
clearAccounts (): void {
this.defaultAddresses.forEach((address) => keyring.forgetAccount(address));
}
protected assertRendered (): void {
if (this.renderResult === undefined) {
throw new NotYetRendered();
}
}
}