Files
api/packages/api/test/e2e/promise-tx.spec.ts
T
Stefanie DollandAmaury Martiny 1ce536008d Convert tests to ts and fix existing (#936)
* Add jest watch option to package.json

* convert promise-alex-spec to ts & add types

* convert ./api tests from JS to TS

* convert ./api tests from JS to TS pt. 2

* More convert tests from JS to TS

* rpc-provider convert tests from js to ts

* rpc-provider tests js => ts

* rpc-provider tests js => ts

* linting issues

* tslint fixes

* 101 => 68 typescript errors

* WIP

* connect() e2e tests

* more fixes

* Fix linting errors of new ERA tests after merge

* test for listeners in connect

* formatting

* getting there...

* re-add skips

* test refactoring & codeclimate fixes

* more fixed tests

* mock.body workaround

* Fixed wrong test description

* ApiPromiseInterface => Api

* ws polyfill / send

* bring back isZero() test

* .then().catch() => .catch()

* Removed leftover body

* Type ProviderInterface => Http

* Fixed polyfill tests

* trailing whitespce

* (mock.body as any)['test_body'] and Balance

* Fix autoconnect in WsProvider

* Fixed part of linting errors in ERA tests

* call unused const unsubscribe

* added ts-ignores for testing private properties/ methods of Provider

* Archive node test

* Fixed linting for ERA tests

* Further changes

* Update packages/rpc-provider/src/ws/polyfill.spec.ts

Co-Authored-By: Amaury Martiny <amaury.martiny@protonmail.com>

* relative paths for imports inside module & reordering of imports pt. 1

* relative paths for imports inside module & reordering of imports pt. 2

* added skip back to e2e transactions

* Fix some more tests in TS (#952)

* Fix some more tests in ts

* Add more constructor

* More typing in ./api

* .skip

* <SubmittableResult>'s

* .catch() -> return()

* Extrinsics tests

* Left TODO's for extrinsic e2e tests

* Workaround for failing Type Extrinsic tests
2019-06-12 16:45:45 +12:00

204 lines
7.1 KiB
TypeScript

// Copyright 2017-2019 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import Keyring from '@plugnet/keyring';
import testingPairs from '@plugnet/keyring/testingPairs';
import { randomAsHex } from '@plugnet/util-crypto';
import WsProvider from '@plugnet/rpc-provider/ws';
import { ExtrinsicEra, Hash, Index, SignedBlock } from '@plugnet/types';
import SingleAccountSigner from '../util/SingleAccountSigner';
import { SubmittableResult } from './../../src';
import { Signer } from './../../src/types';
import Api from './../../src/promise';
// log all events for the transfare, calling done() when finalized
const logEvents = (done: () => {}) =>
({ events, status }: SubmittableResult) => {
console.log('Transaction status:', status.type);
if (status.isFinalized) {
console.log('Completed at block hash', status.value.toHex());
console.log('Events:');
events.forEach(({ phase, event: { data, method, section } }: any) => {
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString());
});
if (events.length) {
done();
}
}
};
describe.skip('Promise e2e transactions', () => {
const keyring = testingPairs({ type: 'ed25519' });
let api: Api;
beforeEach(async (done) => {
if (!api) {
api = await Api.create({
provider: new WsProvider('ws://127.0.0.1:9944')
});
}
jest.setTimeout(30000);
done();
});
afterEach(() => {
jest.setTimeout(5000);
});
it('can submit an extrinsic from hex', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.dave.address()) as Index;
const hex = api.tx.balances
.transfer(keyring.eve.address(), 12345)
.sign(keyring.dave, { nonce })
.toHex();
return api.tx(hex).send(logEvents(done));
});
it('makes a transfer (sign, then send)', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.dave.address()) as Index;
return api.tx.balances
.transfer(keyring.eve.address(), 12345)
.sign(keyring.dave, { nonce })
.send(logEvents(done));
});
it('makes a transfer (sign, then send - compat version)', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.dave.address()) as Index;
return api.tx.balances
.transfer(keyring.eve.address(), 12345)
.sign(keyring.dave, { nonce })
.send(logEvents(done));
});
it('makes a transfer (signAndSend)', async (done) => {
return api.tx.balances
.transfer(keyring.eve.address(), 12345)
.signAndSend(keyring.dave, logEvents(done));
});
it('makes a transfer (signAndSend via Signer)', async (done) => {
const signer = new SingleAccountSigner(keyring.dave) as Signer;
api.setSigner(signer);
return api.tx.balances
.transfer(keyring.eve.address(), 12345)
.signAndSend(keyring.dave.address(), logEvents(done));
});
it('makes a transfer (signAndSend via Signer) with undefined Signer', async () => {
const signer: any = undefined;
// no signer
api.setSigner(signer);
await expect(api.tx.balances
.transfer(keyring.eve.address(), 12345)
.signAndSend(keyring.alice.address())).rejects.toThrow('no signer exists');
});
it('makes a transfer (signAndSend via Signer) with the wrong keyring pair', async () => {
const signer: Signer = new SingleAccountSigner(keyring.dave);
api.setSigner(signer);
// no callback
await expect(api.tx.balances
.transfer(keyring.eve.address(), 12345)
.signAndSend(keyring.alice.address())).rejects.toThrow('does not have the keyringPair');
});
it('makes a transfer (signAndSend via Signer) with the wrong keyring pair with a callback', async () => {
// with callback
await expect(api.tx.balances
.transfer(keyring.eve.address(), 12345)
.signAndSend(keyring.alice.address(), (cb: any) => { /*do nothing */ })).rejects.toThrow('does not have the keyringPair');
});
it('makes a transfer (no callback)', async () => {
const hash = await api.tx.balances
.transfer(keyring.eve.address(), 12345)
.signAndSend(keyring.dave);
expect(hash.toHex()).toHaveLength(66);
});
it('makes a proposal', async () => {
// don't wait for status, just get hash. Here we generate a large-ish payload
// to ensure that we can sign with the hashed version as well (and have it accepted)
const hash: Hash = await api.tx.democracy
.propose(api.tx.consensus.setCode(randomAsHex(4096)), 10000)
.signAndSend(keyring.bob);
expect(hash.toHex()).toHaveLength(66);
});
it('makes a transfer, and uses new balance to transfers to new', async (done) => {
const pair = new Keyring().addFromUri('testing123', {}, 'ed25519');
function doOne (cb: any) {
return api.tx.balances
.transfer(pair.address(), 123456)
.signAndSend(keyring.dave, logEvents(cb));
}
function doTwo (cb: any) {
return api.tx.balances
.transfer(keyring.alice.address(), 12345)
.signAndSend(pair, logEvents(cb));
}
// return doTwo(done);
return doOne(() => {
return doTwo(done);
});
});
it('makes a transfer with ERA (signAndSend)', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.dave.address()) as Index;
const signedBlock = await api.rpc.chain.getBlock();
const currentHeight = (signedBlock as SignedBlock).block.header.number;
const exERA = new ExtrinsicEra({ current: currentHeight, period: 10 });
const eraBirth = exERA.asMortalEra.birth(currentHeight.toNumber());
const eraDeath = exERA.asMortalEra.death(currentHeight.toNumber());
console.log('STARTED AT :' + eraBirth + ' EXPIRED AT :' + eraDeath);
const eraHash = await api.rpc.chain.getBlockHash(eraBirth);
const ex = api.tx.balances.transfer(keyring.eve.address(), 12345);
const hash = await ex.signAndSend(keyring.dave, { blockHash: eraHash, era: exERA, nonce } as any);
expect(hash.toHex()).toHaveLength(66);
done();
});
it('makes a transfer with ERA (signAndSend) with invalid time', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.alice.address()) as Index;
const signedBlock = await api.rpc.chain.getBlock();
const currentHeight = (signedBlock as SignedBlock).block.header.number;
const exERA = new ExtrinsicEra({ current: currentHeight, period: 4 });
const eraBirth = exERA.asMortalEra.birth(currentHeight.toNumber());
const eraDeath = exERA.asMortalEra.death(currentHeight.toNumber());
console.log('STARTED AT :' + eraBirth + ' EXPIRED AT :' + eraDeath);
const eraHash = await api.rpc.chain.getBlockHash(eraBirth);
const ex = api.tx.balances.transfer(keyring.eve.address(), 12345);
return(
api.rpc.chain.subscribeNewHead(async (header) => {
if (header.blockNumber.toNumber() === eraDeath - 1) {
const hash = await ex.signAndSend(keyring.alice, { blockHash: eraHash, era: exERA, nonce } as any);
expect(hash).toBeUndefined();
done();
}
})
);
});
});