Added derive e2e tests and documentation (#898)
* export IExtrinsic * Switch to localhos & enable tests in promise.spec * Derive.accounts docs (copy from my other branch) + code examples * Fixed existing accounts tests and added tests for all accounts.derive methods * e2e tests and docs/ examples for balances.all and balances.fees * Removed emty lines below jsdoc info * tests & documentation dor derive.chain * Addressed comments from former PR https://github.com/polkadot-js/api/pull/869 * Removed unused import * obseralbe -> observable * undo keith' commit * use correct jsdocs syntax * Re-enable skip
This commit is contained in:
committed by
Jaco Greeff
parent
4da52c8d76
commit
c30d3a80e8
@@ -32,7 +32,7 @@ describe.skip('derive e2e', () => {
|
||||
|
||||
it('subscribes to newHead, retrieving the actual validator', (done) => {
|
||||
return api.derive.chain.subscribeNewHead(({ author }) => {
|
||||
console.error('author', author.toString());
|
||||
console.log('author', author.toString());
|
||||
|
||||
if (author) {
|
||||
done();
|
||||
|
||||
@@ -3,14 +3,19 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import ApiRx from '@polkadot/api/rx/Api';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import { BlockNumber } from '@polkadot/types';
|
||||
import { AccountId, AccountIndex, Balance, BlockNumber } from '@polkadot/types';
|
||||
import { WsProvider } from '@polkadot/rpc-provider';
|
||||
|
||||
const WS_LOCAL = 'ws://127.0.0.1:9944/';
|
||||
// const WS_POC3 = 'wss://poc3-rpc.polkadot.io/';
|
||||
|
||||
// Dev account Alice
|
||||
const ID = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
|
||||
const IX = 'F7Hs';
|
||||
|
||||
describe.skip('derive e2e', () => {
|
||||
let api: ApiInterface$Rx;
|
||||
|
||||
@@ -23,49 +28,192 @@ describe.skip('derive e2e', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
it('derive.chain.bestNumber', async (done) => {
|
||||
api.derive.chain.bestNumber().subscribe((blockNumber) => {
|
||||
expect(blockNumber instanceof BlockNumber).toBe(true);
|
||||
expect((blockNumber as BlockNumber).gten(0)).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('derive.session.sessionProgress', async (done) => {
|
||||
api.derive.session.sessionProgress().subscribe((progress) => {
|
||||
expect(progress instanceof BN).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns the intentions with balances', async (done) => {
|
||||
api.derive.staking.intentionsBalances().subscribe((balances) => {
|
||||
expect(Object.keys(balances as object)).not.toHaveLength(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
// these only work on localhost, not the poc-3 URL
|
||||
// These derive.accounts tests only work on localhost, not the poc-3 URL
|
||||
// (and it is assuming it sent at least 1 tx)
|
||||
describe('accounts', () => {
|
||||
const ID = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
|
||||
const IX = 'F7Gh';
|
||||
describe('derive.accounts', () => {
|
||||
|
||||
it('looks up id & index from id', async (done) => {
|
||||
// @ts-ignore silence warning until we have static types here
|
||||
api.derive.accounts.idAndIndex(ID).subscribe(([id, ix]) => {
|
||||
expect(id.toString()).toEqual(ID);
|
||||
expect(ix.toString()).toEqual(IX);
|
||||
done();
|
||||
describe('idAndIndex', () => {
|
||||
it('looks up AccountId & AccountIndex from AccountId', async (done) => {
|
||||
// @ts-ignore silence warning until we have static types here
|
||||
api.derive.accounts.idAndIndex(ID).subscribe(([accountId, accountIndex]) => {
|
||||
expect(accountId.toString()).toEqual(ID);
|
||||
// The first emitted value for ix is undefined when passing the ID
|
||||
if (accountIndex) {
|
||||
expect(accountIndex.toString()).toEqual(IX);
|
||||
} else {
|
||||
expect(accountIndex).toEqual(undefined);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('looks up AccountId & AccountIndex from AccountIndex', async (done) => {
|
||||
// @ts-ignore silence warning until we have static types here
|
||||
api.derive.accounts.idAndIndex(IX).subscribe(([accountId, accountIndex]) => {
|
||||
// The first emitted value for id is undefined when passing the IX
|
||||
if (accountId) {
|
||||
expect(accountId.toString()).toEqual(ID);
|
||||
} else {
|
||||
expect(accountId).toEqual(undefined);
|
||||
}
|
||||
expect(accountIndex.toString()).toEqual(IX);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('looks up id & index from index', async (done) => {
|
||||
// @ts-ignore silence warning until we have static types here
|
||||
api.derive.accounts.idAndIndex(IX).subscribe(([id, ix]) => {
|
||||
expect(id.toString()).toEqual(ID);
|
||||
expect(ix.toString()).toEqual(IX);
|
||||
done();
|
||||
describe('indexToId', () => {
|
||||
it('looks up AccountId from AccountIndex', async (done) => {
|
||||
// @ts-ignore silence warning until we have static types here
|
||||
api.derive.accounts.indexToId(IX).subscribe((accountId) => {
|
||||
// The first emitted value for accountId is undefined when passing the IX
|
||||
if (accountId) {
|
||||
expect(accountId instanceof AccountId).toBe(true);
|
||||
expect(accountId.toString()).toEqual(ID);
|
||||
} else {
|
||||
expect(accountId).toEqual(undefined);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('idToIndex', () => {
|
||||
it('looks up AccountIndex from AccountId', async (done) => {
|
||||
// @ts-ignore silence warning until we have static types here
|
||||
api.derive.accounts.idToIndex(ID).subscribe((accountIndex) => {
|
||||
// The first emitted value for AccountIndex is undefined when passing the ID
|
||||
if (accountIndex) {
|
||||
expect(accountIndex instanceof AccountIndex).toBe(true);
|
||||
expect(accountIndex.toString()).toEqual(IX);
|
||||
} else {
|
||||
expect(accountIndex).toEqual(undefined);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('indexes', () => {
|
||||
it('looks up all AccountIndexes', async (done) => {
|
||||
// @ts-ignore silence warning until we have static types here
|
||||
api.derive.accounts.indexes().subscribe((accountIndexes) => {
|
||||
// A local dev chain should have the AccountIndex of Alice
|
||||
expect(accountIndexes).toHaveProperty(
|
||||
ID,
|
||||
new AccountIndex(IX)
|
||||
);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// these only work on localhost, not the poc-3 URL
|
||||
// (and it is assuming it sent at least 1 tx)
|
||||
describe('derive.balances', () => {
|
||||
describe('all', () => {
|
||||
it('It returns an object with all relevant balance information of an account', async (done) => {
|
||||
api.derive.balances.all(ID).subscribe((balances) => {
|
||||
expect(balances).toEqual(expect.objectContaining({
|
||||
accountId: expect.any(AccountId),
|
||||
availableBalance: expect.any(Balance),
|
||||
freeBalance: expect.any(Balance),
|
||||
lockedBalance: expect.any(Balance),
|
||||
reservedBalance: expect.any(Balance),
|
||||
vestedBalance: expect.any(Balance),
|
||||
votingBalance: expect.any(Balance)
|
||||
}));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('fees', () => {
|
||||
it('fees: It returns an object with all relevant fees of type BN', async (done) => {
|
||||
api.derive.balances.fees().subscribe((fees) => {
|
||||
expect(fees).toEqual(expect.objectContaining({
|
||||
creationFee: expect.any(BN),
|
||||
existentialDeposit: expect.any(BN),
|
||||
transactionBaseFee: expect.any(BN),
|
||||
transactionByteFee: expect.any(BN),
|
||||
transferFee: expect.any(BN)
|
||||
}));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('derive.chain', () => {
|
||||
describe('bestNumber', () => {
|
||||
it('Get the latest block number', async (done) => {
|
||||
api.derive.chain.bestNumber().subscribe((blockNumber) => {
|
||||
expect(blockNumber instanceof BlockNumber).toBe(true);
|
||||
expect((blockNumber as BlockNumber).gten(0)).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bestNumberFinalized', () => {
|
||||
it('Get the latest finalised block number', async (done) => {
|
||||
api.derive.chain.bestNumberFinalized().subscribe((blockNumber) => {
|
||||
expect(blockNumber instanceof BlockNumber).toBe(true);
|
||||
expect((blockNumber as BlockNumber).gten(0)).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bestNumberLag', () => {
|
||||
it('lag between finalised head and best head', async (done) => {
|
||||
api.derive.chain.bestNumberLag().subscribe((numberLag) => {
|
||||
expect(numberLag instanceof BlockNumber).toBe(true);
|
||||
expect((numberLag as BlockNumber).gten(0)).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getHeader', () => {
|
||||
it('gets a specific block header and extended with it\`s author', async (done) => {
|
||||
api.derive.chain.getHeader().subscribe((headerExtended) => {
|
||||
// WIP
|
||||
expect(headerExtended).toEqual(expect.arrayContaining([]));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('subscribeNewHead', () => {
|
||||
it('gets an observable of the current block header and it\'s author', async (done) => {
|
||||
api.derive.chain.subscribeNewHead().subscribe((headerExtended) => {
|
||||
// WIP
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('derive.session', () => {
|
||||
describe('sessionProgress', () => {
|
||||
it('derive.session.sessionProgress', async (done) => {
|
||||
api.derive.session.sessionProgress().subscribe((progress) => {
|
||||
expect(progress instanceof BN).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('derive.staking', () => {
|
||||
describe('intentionsBalances', () => {
|
||||
it('returns the intentions with balances', async (done) => {
|
||||
api.derive.staking.intentionsBalances().subscribe((balances) => {
|
||||
expect(Object.keys(balances as object)).not.toHaveLength(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user