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
This commit is contained in:
co-authored by
Amaury Martiny
parent
f50229db0d
commit
c01ade86a6
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Observable, from } from 'rxjs';
|
||||
|
||||
import ApiRx from '@polkadot/api/rx/Api';
|
||||
import { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
import MockProvider from '@polkadot/rpc-provider/mock';
|
||||
|
||||
@@ -10,6 +10,8 @@ import testKeyring from '@polkadot/keyring/testingPairs';
|
||||
import { IExtrinsic, IMethod } from '@polkadot/types/types';
|
||||
import { Header } from '@polkadot/types';
|
||||
|
||||
import { SubmittableResult } from './';
|
||||
|
||||
export default async function test () {
|
||||
const api = await ApiPromise.create();
|
||||
const keyring = testKeyring();
|
||||
@@ -39,7 +41,7 @@ export default async function test () {
|
||||
|
||||
const unsub = await api.tx.balances
|
||||
.transfer(keyring.bob.address(), 12345)
|
||||
.signAndSend(keyring.alice, ({ status }) => {
|
||||
.signAndSend(keyring.alice, ({ status }: SubmittableResult) => {
|
||||
console.log('transfer status:', status.type);
|
||||
|
||||
unsub();
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
import { ApiOptions } from '@polkadot/api/types';
|
||||
import { Hash, Metadata } from '@polkadot/types';
|
||||
|
||||
import Mock from '@polkadot/rpc-provider/mock/index';
|
||||
|
||||
import { ApiPromise } from './..';
|
||||
import { ApiOptions } from './../types';
|
||||
|
||||
describe.skip('Metadata queries', () => {
|
||||
let mock: Mock;
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { SubscriptionResult } from './types';
|
||||
|
||||
import Combinator from './Combinator';
|
||||
|
||||
describe('Combinator', () => {
|
||||
|
||||
+24
-21
@@ -2,10 +2,13 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Api from '../../src/promise';
|
||||
import { Extrinsic, SignedBlock } from '@polkadot/types';
|
||||
|
||||
import Api from './../../src/promise';
|
||||
|
||||
// To run these tests locally you need to run a Alexander full archive node locally
|
||||
describe.skip('alex archive queries (local)', () => {
|
||||
let api;
|
||||
let api: Api;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
@@ -23,7 +26,7 @@ describe.skip('alex archive queries (local)', () => {
|
||||
|
||||
console.error(JSON.stringify(metadata, null, 2));
|
||||
|
||||
const block = await api.rpc.chain.getBlock('0x6f6f9bba0eed8e3ae9446c37eee763f93118b52a315a7b46090453ba6288da1f')
|
||||
const block = await api.rpc.chain.getBlock('0x6f6f9bba0eed8e3ae9446c37eee763f93118b52a315a7b46090453ba6288da1f');
|
||||
|
||||
console.error(block);
|
||||
|
||||
@@ -34,25 +37,25 @@ describe.skip('alex archive queries (local)', () => {
|
||||
|
||||
// https://github.com/polkadot-js/api/issues/846
|
||||
it('handles toJSON with no issues', async (done) => {
|
||||
const signedBlock = await api.rpc.chain.getBlock('0x85c62b581f38cb81c3e443d34392672beb1fb877017fd7237cc87704113259dc');
|
||||
const failed = signedBlock.block.extrinsics.filter((extrinsic) => {
|
||||
try {
|
||||
const json = extrinsic.method.toJSON();
|
||||
return (
|
||||
api.rpc.chain.getBlock('0x85c62b581f38cb81c3e443d34392672beb1fb877017fd7237cc87704113259dc', (result: SignedBlock) => {
|
||||
const failed: Array<Extrinsic> = result.block.extrinsics.filter((extrinsic: Extrinsic) => {
|
||||
try {
|
||||
const json = extrinsic.method.toJSON();
|
||||
|
||||
console.error(json);
|
||||
console.error(json);
|
||||
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.log(extrinsic.method);
|
||||
console.log(extrinsic.method.keys());
|
||||
console.error(error);
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
expect(failed).toHaveLength(0);
|
||||
|
||||
done();
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.log(extrinsic.method);
|
||||
console.log(extrinsic.method.keys());
|
||||
console.error(error);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
expect(failed).toBeTruthy();
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,94 +0,0 @@
|
||||
// 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 { Hash, Header } from '@polkadot/types';
|
||||
|
||||
import Api from '../../src/promise';
|
||||
import WsProvider from '../../../rpc-provider/src/ws';
|
||||
|
||||
const WS_URL = 'wss://poc3-rpc.polkadot.io/';
|
||||
// const WS_URL = 'wss://substrate-rpc.parity.io/';
|
||||
|
||||
describe.skip('alex queries', () => {
|
||||
let api;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
api = await Api.create({
|
||||
provider: new WsProvider(WS_URL)
|
||||
});
|
||||
|
||||
return api;
|
||||
});
|
||||
|
||||
it('retrieves the list of validators', (done) => {
|
||||
api.query.staking.validators((res) => {
|
||||
console.error(res);
|
||||
|
||||
console.log('api.query.staking.validators():', res.toJSON());
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('retrieves a single value', (done) => {
|
||||
api.query.staking.validators('5DuiZFa184E9iCwbh4WjXYvJ88NHvWJbS8SARY8Ev1YEqrri', (res) => {
|
||||
console.error(res);
|
||||
|
||||
console.log('api.query.staking.validators(id):', res.toJSON());
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it('Gets the hash of the last finalized header', async (done) => {
|
||||
api.rpc.chain.getFinalizedHead((head) => {
|
||||
expect(head instanceof Hash).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Subscribes to the best finalized header on ALEX', async (done) => {
|
||||
api.rpc.chain.subscribeFinalizedHeads((heads) => {
|
||||
expect(heads instanceof Header).toBe(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('derives a list of the controllers', (done) => {
|
||||
api.derive.staking.controllers((res) => {
|
||||
console.log('api.derive.staking.controllers:', JSON.stringify(res));
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('makes a query at a latest block (specified)', async () => {
|
||||
const header = await api.rpc.chain.getHeader();
|
||||
const events = await api.query.system.events.at(header.hash);
|
||||
|
||||
events.forEach(({ event: { data, method, section }, phase, topics }, index) => {
|
||||
console.error(index, phase.toString(), `: ${section}.${method}`, data.toString(), topics.toString());
|
||||
});
|
||||
|
||||
expect(events.length).not.toEqual(0);
|
||||
});
|
||||
|
||||
it('subscribes to events', (done) => {
|
||||
api.query.system.events((events) => {
|
||||
console.error(JSON.stringify(events));
|
||||
|
||||
events.forEach(({ event: { data, method, section }, phase, topics }, index) => {
|
||||
console.error(index, phase.toString(), `: ${section}.${method}`, data.toString(), topics.toString());
|
||||
});
|
||||
|
||||
expect(events).not.toHaveLength(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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 WsProvider from '@polkadot/rpc-provider/ws';
|
||||
import { EventRecord, Hash, Header, Vector } from '@polkadot/types';
|
||||
|
||||
import Api from './../../src/promise';
|
||||
|
||||
const WS_URL = 'wss://poc3-rpc.polkadot.io/';
|
||||
// const WS_URL = 'wss://substrate-rpc.parity.io/';
|
||||
|
||||
describe.skip('alex queries', () => {
|
||||
let api: Api;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
api = await Api.create({
|
||||
provider: new WsProvider(WS_URL)
|
||||
});
|
||||
|
||||
return api;
|
||||
});
|
||||
|
||||
it('retrieves the list of validators', (done) => {
|
||||
return (
|
||||
api.query.staking.validators((res) => {
|
||||
console.error(res);
|
||||
console.log('api.query.staking.validators():', res.toJSON());
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
describe('retrieves a single value', () => {
|
||||
it('retrieves the list of stash validators', (done) => {
|
||||
return (
|
||||
api.query.staking.validators('5DuiZFa184E9iCwbh4WjXYvJ88NHvWJbS8SARY8Ev1YEqrri', (res) => {
|
||||
console.error(res);
|
||||
console.log('api.query.staking.validators(id):', res.toJSON());
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('Gets the hash of the last finalized header', async (done) => {
|
||||
return (
|
||||
api.rpc.chain.getFinalizedHead((head) => {
|
||||
expect(head instanceof Hash).toBe(true);
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('Subscribes to the best finalized header on ALEX', async (done) => {
|
||||
return(
|
||||
api.rpc.chain.subscribeFinalizedHeads((head) => {
|
||||
expect(head instanceof Header).toBe(true);
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('derives a list of the controllers', (done) => {
|
||||
return (
|
||||
api.derive.staking.controllers((res) => {
|
||||
console.log('api.derive.staking.controllers:', JSON.stringify(res));
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('makes a query at a latest block (specified)', async () => {
|
||||
const header = await api.rpc.chain.getHeader() as Header;
|
||||
const events = await api.query.system.events.at(header.hash) as Vector<EventRecord>;
|
||||
|
||||
expect(events.length).not.toEqual(0);
|
||||
});
|
||||
|
||||
it('subscribes to events', (done) => {
|
||||
api.query.system.events((events: Array<EventRecord>) => {
|
||||
expect(events).not.toHaveLength(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
+43
-36
@@ -5,18 +5,22 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import { ContractAbi } from '@polkadot/types';
|
||||
import { ContractAbi, Address, Hash } from '@polkadot/types';
|
||||
import { KeyringPair } from '@polkadot/keyring/types';
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
|
||||
import Api from './../../src/promise';
|
||||
import incrementer from '../data/incrementer.json';
|
||||
import erc20 from '../data/erc20.json';
|
||||
import Api from '../../src/promise';
|
||||
import { SubmittableResult } from './../../src';
|
||||
|
||||
describe.skip('e2e contracts', () => {
|
||||
let address;
|
||||
let codeHash;
|
||||
let keyring;
|
||||
let api;
|
||||
describe.skip('Promise e2e contracts', () => {
|
||||
let address: Address;
|
||||
let codeHash: Hash;
|
||||
let keyring: {
|
||||
[index: string]: KeyringPair
|
||||
};
|
||||
let api: Api;
|
||||
|
||||
beforeEach(async (done) => {
|
||||
if (!api) {
|
||||
@@ -30,7 +34,7 @@ describe.skip('e2e contracts', () => {
|
||||
});
|
||||
|
||||
describe('incrementer', () => {
|
||||
let abi;
|
||||
let abi: ContractAbi;
|
||||
|
||||
beforeEach(() => {
|
||||
abi = new ContractAbi(incrementer);
|
||||
@@ -39,60 +43,63 @@ describe.skip('e2e contracts', () => {
|
||||
it('allows putCode', (done) => {
|
||||
const code = fs.readFileSync(path.join(__dirname, '../data/incrementer-opt.wasm')).toString('hex');
|
||||
|
||||
api.tx.contract
|
||||
return(
|
||||
api.tx.contract
|
||||
.putCode(200000, `0x${code}`)
|
||||
.signAndSend(keyring.eve, (result) => {
|
||||
.signAndSend(keyring.eve, (result: SubmittableResult) => {
|
||||
console.error('putCode', JSON.stringify(result));
|
||||
|
||||
if (result.status.isFinalized) {
|
||||
const record = result.findRecord('contract', 'CodeStored');
|
||||
|
||||
if (record) {
|
||||
codeHash = record.event.data[0];
|
||||
|
||||
done();
|
||||
codeHash = record.event.data[0] as Hash;
|
||||
}
|
||||
done();
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('allows contract create', (done) => {
|
||||
expect(codeHash).toBeDefined();
|
||||
|
||||
api.tx.contract
|
||||
.create(12345, 500000, codeHash, abi.deploy(12345))
|
||||
.signAndSend(keyring.bob, (result) => {
|
||||
console.error('create', JSON.stringify(result));
|
||||
return (
|
||||
api.tx.contract
|
||||
.create(12345, 500000, codeHash, abi.deploy(12345))
|
||||
.signAndSend(keyring.bob, (result: SubmittableResult) => {
|
||||
console.error('create', JSON.stringify(result));
|
||||
if (result.status.isFinalized) {
|
||||
const record = result.findRecord('contract', 'Instantiated');
|
||||
|
||||
if (result.status.isFinalized) {
|
||||
const record = result.findRecord('contract', 'Instantiated');
|
||||
if (record) {
|
||||
address = record.event.data[1] as Address;
|
||||
|
||||
if (record) {
|
||||
address = record.event.data[1];
|
||||
|
||||
done();
|
||||
done();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('allows contract call', (done) => {
|
||||
expect(address).toBeDefined();
|
||||
|
||||
api.tx.contract
|
||||
.call(address, 12345, 500000, abi.messages.inc(123))
|
||||
.signAndSend(keyring.bob, (result) => {
|
||||
console.error('call', JSON.stringify(result));
|
||||
return (
|
||||
api.tx.contract
|
||||
.call(address, 12345, 500000, abi.messages.inc(123))
|
||||
.signAndSend(keyring.bob, (result: SubmittableResult) => {
|
||||
console.error('call', JSON.stringify(result));
|
||||
|
||||
if (result.status.isFinalized && result.findRecord('system', 'ExtrinsicSuccess')) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
if (result.status.isFinalized && result.findRecord('system', 'ExtrinsicSuccess')) {
|
||||
done();
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('erc20', () => {
|
||||
let abi;
|
||||
let abi: ContractAbi;
|
||||
|
||||
beforeEach(() => {
|
||||
abi = abi = new ContractAbi(erc20);
|
||||
@@ -1,186 +0,0 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
import { LinkageResult } from '@polkadot/types/codec/Linkage';
|
||||
|
||||
import Api from '../../src/promise';
|
||||
import WsProvider from '../../../rpc-provider/src/ws';
|
||||
|
||||
const ZERO = new BN(0);
|
||||
const WS_URL = 'ws://127.0.0.1:9944';
|
||||
// const WS_URL = 'wss://poc3-rpc.polkadot.io/';
|
||||
|
||||
describe.skip('e2e queries', () => {
|
||||
const keyring = testingPairs({ type: 'ed25519' });
|
||||
let api;
|
||||
|
||||
beforeEach(async (done) => {
|
||||
if (!api) {
|
||||
api = await Api.create({
|
||||
provider: new WsProvider(WS_URL)
|
||||
});
|
||||
}
|
||||
|
||||
jest.setTimeout(30000);
|
||||
done();
|
||||
});
|
||||
|
||||
it('makes the runtime, rpc, state & extrinsics available', () => {
|
||||
expect(api.genesisHash).toBeDefined();
|
||||
expect(api.runtimeMetadata).toBeDefined();
|
||||
expect(api.runtimeVersion).toBeDefined();
|
||||
expect(api.rpc).toBeDefined();
|
||||
expect(api.query).toBeDefined();
|
||||
expect(api.tx).toBeDefined();
|
||||
expect(api.derive).toBeDefined();
|
||||
});
|
||||
|
||||
it('queries state for a balance', async () => {
|
||||
const balance = await api.query.balances.freeBalance(keyring.alice.address());
|
||||
|
||||
expect(
|
||||
balance.isZero()
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('subscribes to rpc', (done) => {
|
||||
api.rpc.chain.subscribeNewHead((header) => {
|
||||
expect(header.blockNumber.isZero()).toBe(false);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to finalized', (done) => {
|
||||
api.rpc.chain.subscribeFinalizedHeads((header) => {
|
||||
expect(header.blockNumber.isZero()).toBe(false);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to derive', (done) => {
|
||||
api.derive.chain.subscribeNewHead((header) => {
|
||||
expect(header.blockNumber.isZero()).toBe(false);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to queries', (done) => {
|
||||
api.query.system.accountNonce(keyring.ferdie.address(), (nonce) => {
|
||||
expect(nonce instanceof BN).toBe(true);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it.skip('subscribes to queries (default)', (done) => {
|
||||
api.query.staking.validators(keyring.ferdie.address(), (prefs) => {
|
||||
expect(prefs.unstakeThreshold.toNumber()).toBe(3);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to a linked map (staking.validators)', (done) => {
|
||||
api.query.staking.validators((prefs) => {
|
||||
expect(prefs instanceof LinkageResult).toBe(true);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to multiple results (freeBalance.multi)', (done) => {
|
||||
api.query.balances.freeBalance.multi([
|
||||
keyring.alice.address(),
|
||||
keyring.bob.address(),
|
||||
'5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y',
|
||||
keyring.ferdie.address()
|
||||
], (balances) => {
|
||||
console.error(balances);
|
||||
|
||||
expect(balances).toHaveLength(4);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to multiple results (api.queryMulti)', (done) => {
|
||||
return api.queryMulti([
|
||||
[api.query.balances.freeBalance, keyring.alice.address()],
|
||||
[api.query.balances.freeBalance, keyring.bob.address()],
|
||||
[api.query.balances.freeBalance, '5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y'],
|
||||
[api.query.balances.freeBalance, keyring.ferdie.address()]
|
||||
], (balances) => {
|
||||
console.error(balances);
|
||||
|
||||
expect(balances).toHaveLength(4);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to derived balances (balances.all)', (done) => {
|
||||
api.derive.balances.all(
|
||||
keyring.alice.address(),
|
||||
(all) => {
|
||||
expect(all.accountId.toString()).toEqual(keyring.alice.address());
|
||||
|
||||
expect(all.freeBalance).toBeDefined();
|
||||
expect(all.freeBalance.gt(ZERO)).toBe(true);
|
||||
|
||||
expect(all.availableBalance).toBeDefined();
|
||||
expect(all.availableBalance.gt(ZERO)).toBe(true);
|
||||
|
||||
expect(all.reservedBalance).toBeDefined();
|
||||
expect(all.lockedBalance).toBeDefined();
|
||||
expect(all.vestedBalance).toBeDefined();
|
||||
|
||||
console.error(all);
|
||||
|
||||
done();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('makes a query at a latest block (specified)', async () => {
|
||||
const header = await api.rpc.chain.getHeader();
|
||||
const events = await api.query.system.events.at(header.hash);
|
||||
|
||||
events.forEach(({ event: { data, method, section }, phase, topics }, index) => {
|
||||
console.error(index, phase.toString(), `: ${section}.${method}`, data.toString(), topics.toString());
|
||||
});
|
||||
|
||||
expect(events.length).not.toEqual(0);
|
||||
});
|
||||
|
||||
it('subscribes to events', (done) => {
|
||||
api.query.system.events((events) => {
|
||||
events.forEach(({ event: { data, method, section }, phase, topics }, index) => {
|
||||
console.error(index, phase.toString(), `: ${section}.${method}`, data.toString(), topics.toString());
|
||||
});
|
||||
|
||||
expect(events).not.toHaveLength(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('queries state using double map key', async () => {
|
||||
// TODO Update ['any', '0x1234'] to the key of a known event topic and update '[]' to the expected value
|
||||
const eventTopics = await api.query.system.eventTopics(['any', '0x1234']);
|
||||
|
||||
expect(eventTopics.toString()).toEqual('[]');
|
||||
});
|
||||
|
||||
it('subscribes to queries using double map key', async (done) => {
|
||||
// TODO Update ['any', '0x1234'] to the key of a known event topic and update '[]' to the expected value
|
||||
api.query.system.eventTopics(['any', '0x1234'], (eventTopics) => {
|
||||
expect(eventTopics.toString()).toEqual('[]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,196 @@
|
||||
// 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 BN from 'bn.js';
|
||||
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
import { LinkageResult } from '@polkadot/types/codec/Linkage';
|
||||
import { EventRecord, Header, Vector } from '@polkadot/types';
|
||||
|
||||
import Api from './../../src/promise';
|
||||
|
||||
const ZERO = new BN(0);
|
||||
const WS_URL = 'ws://127.0.0.1:9944';
|
||||
// const WS_URL = 'wss://poc3-rpc.polkadot.io/';
|
||||
|
||||
describe.skip('Promise e2e queries', () => {
|
||||
const keyring = testingPairs({ type: 'ed25519' });
|
||||
let api: Api;
|
||||
|
||||
beforeEach(async (done) => {
|
||||
if (!api) {
|
||||
api = await Api.create({
|
||||
provider: new WsProvider(WS_URL)
|
||||
});
|
||||
}
|
||||
|
||||
jest.setTimeout(30000);
|
||||
done();
|
||||
});
|
||||
|
||||
it('makes the runtime, rpc, state & extrinsics available', () => {
|
||||
expect(api.genesisHash).toBeDefined();
|
||||
expect(api.runtimeMetadata).toBeDefined();
|
||||
expect(api.runtimeVersion).toBeDefined();
|
||||
expect(api.rpc).toBeDefined();
|
||||
expect(api.query).toBeDefined();
|
||||
expect(api.tx).toBeDefined();
|
||||
expect(api.derive).toBeDefined();
|
||||
});
|
||||
|
||||
it('queries state for a balance', async () => {
|
||||
return(
|
||||
api.query.balances.freeBalance(keyring.alice.address(), balance => {
|
||||
expect(balance).toBeInstanceOf(BN);
|
||||
expect(balance.isZero()).toBe(false);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('subscribes to rpc', (done) => {
|
||||
return(
|
||||
api.rpc.chain.subscribeNewHead((header) => {
|
||||
expect(header.blockNumber.isZero()).toBe(false);
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('subscribes to finalized', (done) => {
|
||||
return(
|
||||
api.rpc.chain.subscribeFinalizedHeads((header) => {
|
||||
expect(header.blockNumber.isZero()).toBe(false);
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('subscribes to derive', (done) => {
|
||||
return(
|
||||
api.derive.chain.subscribeNewHead((header) => {
|
||||
expect(header.blockNumber.isZero()).toBe(false);
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('subscribes to queries', (done) => {
|
||||
return(
|
||||
api.query.system.accountNonce(keyring.ferdie.address(), (nonce) => {
|
||||
expect(nonce instanceof BN).toBe(true);
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it.skip('subscribes to queries (default)', (done) => {
|
||||
return(
|
||||
api.query.staking.validators(keyring.ferdie.address(), (prefs) => {
|
||||
expect(prefs.unstakeThreshold.toNumber()).toBe(3);
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('subscribes to a linked map (staking.validators)', (done) => {
|
||||
return(
|
||||
api.query.staking.validators((prefs) => {
|
||||
expect(prefs instanceof LinkageResult).toBe(true);
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('subscribes to multiple results (freeBalance.multi)', (done) => {
|
||||
return(
|
||||
api.query.balances.freeBalance.multi([
|
||||
keyring.alice.address(),
|
||||
keyring.bob.address(),
|
||||
'5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y',
|
||||
keyring.ferdie.address()
|
||||
], (balances) => {
|
||||
expect(balances).toHaveLength(4);
|
||||
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('subscribes to multiple results (api.queryMulti)', (done) => {
|
||||
return api.queryMulti([
|
||||
[api.query.balances.freeBalance, keyring.alice.address()],
|
||||
[api.query.balances.freeBalance, keyring.bob.address()],
|
||||
[api.query.balances.freeBalance, '5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y'],
|
||||
[api.query.balances.freeBalance, keyring.ferdie.address()]
|
||||
], (balances: Array<BN>) => {
|
||||
expect(balances).toHaveLength(4);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to derived balances (balances.all)', (done) => {
|
||||
return(
|
||||
api.derive.balances.all(
|
||||
keyring.alice.address(),
|
||||
(all) => {
|
||||
expect(all.accountId.toString()).toEqual(keyring.alice.address());
|
||||
|
||||
expect(all.freeBalance).toBeDefined();
|
||||
expect(all.freeBalance.gt(ZERO)).toBe(true);
|
||||
expect(all.availableBalance).toBeDefined();
|
||||
expect(all.availableBalance.gt(ZERO)).toBe(true);
|
||||
expect(all.reservedBalance).toBeDefined();
|
||||
expect(all.lockedBalance).toBeDefined();
|
||||
expect(all.vestedBalance).toBeDefined();
|
||||
done();
|
||||
}
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it('makes a query at a latest block (specified)', async () => {
|
||||
const header = await api.rpc.chain.getHeader() as Header;
|
||||
const events = await api.query.system.events.at(header.hash) as Vector<EventRecord>;
|
||||
|
||||
expect(events.length).not.toEqual(0);
|
||||
|
||||
events.forEach(({ event: { data, method, section }, phase, topics }: EventRecord) => {
|
||||
console.error(phase.toString(), `: ${section}.${method}`, data.toString(), topics.toString());
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to events', (done) => {
|
||||
return(
|
||||
api.query.system.events((events) => {
|
||||
expect(events).not.toHaveLength(0);
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('queries state using double map key', async () => {
|
||||
// TODO Update ['any', '0x1234'] to the key of a known event topic and update '[]' to the expected value
|
||||
const eventTopics = await api.query.system.eventTopics(['any', '0x1234']);
|
||||
|
||||
expect(eventTopics.toString()).toEqual('[]');
|
||||
});
|
||||
|
||||
it('subscribes to queries using double map key', async (done) => {
|
||||
return(
|
||||
// TODO Update ['any', '0x1234'] to the key of a known event topic and update '[]' to the expected value
|
||||
api.query.system.eventTopics(['any', '0x1234'], (eventTopics) => {
|
||||
expect(eventTopics.toString()).toEqual('[]');
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
+61
-56
@@ -4,35 +4,37 @@
|
||||
|
||||
import Keyring from '@polkadot/keyring';
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
import { ExtrinsicEra } from '@polkadot/types/type';
|
||||
import { randomAsHex } from '@polkadot/util-crypto';
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
import { ExtrinsicEra, Hash, Index, SignedBlock } from '@polkadot/types';
|
||||
|
||||
import Api from '../../src/promise';
|
||||
import WsProvider from '../../../rpc-provider/src/ws';
|
||||
import SingleAccountSigner from '../util/SingleAccountSigner';
|
||||
import { SubmittableResult } from './../../src';
|
||||
import { Signer } from './../../src/types';
|
||||
import Api from './../../src/promise';
|
||||
|
||||
describe.skip('e2e transactions', () => {
|
||||
// log all events for the transfare, calling done() when finalized
|
||||
const logEvents = (done) =>
|
||||
({ events, status }) => {
|
||||
console.log('Transaction status:', status.type);
|
||||
// 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:');
|
||||
if (status.isFinalized) {
|
||||
console.log('Completed at block hash', status.value.toHex());
|
||||
console.log('Events:');
|
||||
|
||||
events.forEach(({ phase, event: { data, method, section } }) => {
|
||||
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString());
|
||||
});
|
||||
events.forEach(({ phase, event: { data, method, section } }: any) => {
|
||||
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString());
|
||||
});
|
||||
|
||||
if (events.length) {
|
||||
done();
|
||||
}
|
||||
if (events.length) {
|
||||
done();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
describe.skip('Promise e2e transactions', () => {
|
||||
const keyring = testingPairs({ type: 'ed25519' });
|
||||
let api;
|
||||
let api: Api;
|
||||
|
||||
beforeEach(async (done) => {
|
||||
if (!api) {
|
||||
@@ -50,7 +52,7 @@ describe.skip('e2e transactions', () => {
|
||||
});
|
||||
|
||||
it('can submit an extrinsic from hex', async (done) => {
|
||||
const nonce = await api.query.system.accountNonce(keyring.dave.address());
|
||||
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 })
|
||||
@@ -60,7 +62,7 @@ describe.skip('e2e transactions', () => {
|
||||
});
|
||||
|
||||
it('makes a transfer (sign, then send)', async (done) => {
|
||||
const nonce = await api.query.system.accountNonce(keyring.dave.address());
|
||||
const nonce = await api.query.system.accountNonce(keyring.dave.address()) as Index;
|
||||
|
||||
return api.tx.balances
|
||||
.transfer(keyring.eve.address(), 12345)
|
||||
@@ -69,11 +71,11 @@ describe.skip('e2e transactions', () => {
|
||||
});
|
||||
|
||||
it('makes a transfer (sign, then send - compat version)', async (done) => {
|
||||
const nonce = await api.query.system.accountNonce(keyring.dave.address());
|
||||
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)
|
||||
.sign(keyring.dave, { nonce })
|
||||
.send(logEvents(done));
|
||||
});
|
||||
|
||||
@@ -84,7 +86,7 @@ describe.skip('e2e transactions', () => {
|
||||
});
|
||||
|
||||
it('makes a transfer (signAndSend via Signer)', async (done) => {
|
||||
const signer = new SingleAccountSigner(keyring.dave);
|
||||
const signer = new SingleAccountSigner(keyring.dave) as Signer;
|
||||
|
||||
api.setSigner(signer);
|
||||
|
||||
@@ -93,28 +95,32 @@ describe.skip('e2e transactions', () => {
|
||||
.signAndSend(keyring.dave.address(), logEvents(done));
|
||||
});
|
||||
|
||||
it('makes a transfer (signAndSend via Signer) - sad path', async () => {
|
||||
//no signer
|
||||
api.setSigner();
|
||||
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');
|
||||
});
|
||||
|
||||
const signer = new SingleAccountSigner(keyring.dave);
|
||||
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
|
||||
// no callback
|
||||
await expect(api.tx.balances
|
||||
.transfer(keyring.eve.address(), 12345)
|
||||
.signAndSend(keyring.alice.address())).rejects.toThrow('does not have the keyringPair');
|
||||
});
|
||||
|
||||
//with callback
|
||||
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(), ({ events, status }) => {
|
||||
})).rejects.toThrow('does not have the keyringPair');
|
||||
.signAndSend(keyring.alice.address(), (cb: any) => { /*do nothing */ })).rejects.toThrow('does not have the keyringPair');
|
||||
});
|
||||
|
||||
it('makes a transfer (no callback)', async () => {
|
||||
@@ -128,7 +134,7 @@ describe.skip('e2e transactions', () => {
|
||||
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 = await api.tx.democracy
|
||||
const hash: Hash = await api.tx.democracy
|
||||
.propose(api.tx.consensus.setCode(randomAsHex(4096)), 10000)
|
||||
.signAndSend(keyring.bob);
|
||||
|
||||
@@ -138,13 +144,13 @@ describe.skip('e2e transactions', () => {
|
||||
it('makes a transfer, and uses new balance to transfers to new', async (done) => {
|
||||
const pair = new Keyring().addFromUri('testing123', {}, 'ed25519');
|
||||
|
||||
function doOne (cb) {
|
||||
function doOne (cb: any) {
|
||||
return api.tx.balances
|
||||
.transfer(pair.address(), 123456)
|
||||
.signAndSend(keyring.dave, logEvents(cb));
|
||||
};
|
||||
}
|
||||
|
||||
function doTwo (cb) {
|
||||
function doTwo (cb: any) {
|
||||
return api.tx.balances
|
||||
.transfer(keyring.alice.address(), 12345)
|
||||
.signAndSend(pair, logEvents(cb));
|
||||
@@ -152,47 +158,46 @@ describe.skip('e2e transactions', () => {
|
||||
|
||||
// return doTwo(done);
|
||||
return doOne(() => {
|
||||
doTwo(done)
|
||||
return doTwo(done);
|
||||
});
|
||||
});
|
||||
|
||||
it('makes a transfer with ERA (signAndSend)', async (done) => {
|
||||
const nonce = await api.query.system.accountNonce(keyring.dave.address());
|
||||
const nonce = await api.query.system.accountNonce(keyring.dave.address()) as Index;
|
||||
const signedBlock = await api.rpc.chain.getBlock();
|
||||
const currentHeight = signedBlock.block.header.number;
|
||||
const currentHeight = (signedBlock as SignedBlock).block.header.number;
|
||||
const exERA = new ExtrinsicEra({ current: currentHeight, period: 10 });
|
||||
// eraBirth - start of ERA which is always less than current block height
|
||||
// eraDeath - end of ERA validity (EXPIRY)
|
||||
const eraBirth = exERA.asMortalEra.birth(currentHeight.toNumber());
|
||||
const eraDeath = exERA.asMortalEra.death(currentHeight.toNumber());
|
||||
console.log('STARTED AT :'+eraBirth+' EXPIRED AT :'+eraDeath);
|
||||
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});
|
||||
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());
|
||||
const nonce = await api.query.system.accountNonce(keyring.alice.address()) as Index;
|
||||
const signedBlock = await api.rpc.chain.getBlock();
|
||||
const currentHeight = signedBlock.block.header.number;
|
||||
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);
|
||||
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 unsubscribe = await api.rpc.chain.subscribeNewHead(async(header) => {
|
||||
console.log(`Chain is at block: #${header.blockNumber}`);
|
||||
if (header.blockNumber.toNumber() === eraDeath-1) {
|
||||
const hash = await ex.signAndSend(keyring.alice, {blockHash: eraHash, era:exERA, nonce});
|
||||
|
||||
expect(hash).toBeUndefined();
|
||||
done();
|
||||
}
|
||||
});
|
||||
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();
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
+10
-9
@@ -2,14 +2,17 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
|
||||
import { Balance } from '@polkadot/types';
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
|
||||
import Api from '../../src/rx';
|
||||
|
||||
describe.skip('e2e queries', () => {
|
||||
describe.skip('Rx e2e queries', () => {
|
||||
const keyring = testingPairs({ type: 'ed25519' });
|
||||
let api;
|
||||
let api: Api;
|
||||
|
||||
beforeEach(async (done) => {
|
||||
api = await Api.create().toPromise();
|
||||
@@ -29,10 +32,8 @@ describe.skip('e2e queries', () => {
|
||||
|
||||
it('queries state for a balance', (done) => {
|
||||
api.query.balances.freeBalance(keyring.alice.address()).subscribe((balance) => {
|
||||
expect(
|
||||
balance.isZero()
|
||||
).toBe(false);
|
||||
|
||||
expect(balance).toBeInstanceOf(BN);
|
||||
expect((balance as Balance).isZero()).toBe(false);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -41,11 +42,11 @@ describe.skip('e2e queries', () => {
|
||||
api.rpc.chain
|
||||
.getHeader()
|
||||
.pipe(
|
||||
switchMap((header) =>
|
||||
api.query.system.events.at(header.hash)
|
||||
switchMap(({ hash }: any) =>
|
||||
api.query.system.events.at(hash)
|
||||
)
|
||||
)
|
||||
.subscribe((events) => {
|
||||
.subscribe((events: any) => {
|
||||
expect(events.length).not.toEqual(0);
|
||||
done();
|
||||
});
|
||||
@@ -3,18 +3,20 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { first, switchMap } from 'rxjs/operators';
|
||||
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
|
||||
import Api from '../../src/rx';
|
||||
import Api from './../../src/rx';
|
||||
import { SubmittableResult } from './../../src';
|
||||
|
||||
describe.skip('e2e transactions', () => {
|
||||
describe.skip('Rx e2e transactions', () => {
|
||||
const keyring = testingPairs({ type: 'ed25519' });
|
||||
let api;
|
||||
let api: Api;
|
||||
|
||||
beforeEach(async (done) => {
|
||||
api = await Api.create().toPromise();
|
||||
jest.setTimeout(30000);
|
||||
done()
|
||||
done();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -26,14 +28,14 @@ describe.skip('e2e transactions', () => {
|
||||
.accountNonce(keyring.alice.address())
|
||||
.pipe(
|
||||
first(),
|
||||
switchMap((nonce) =>
|
||||
switchMap((nonce: any) =>
|
||||
api.tx.balances
|
||||
.transfer(keyring.bob.address(), 12345)
|
||||
.sign(keyring.alice, { nonce })
|
||||
.send()
|
||||
)
|
||||
)
|
||||
.subscribe(({ status }) => {
|
||||
.subscribe(({ status }: SubmittableResult) => {
|
||||
if (status.isFinalized) {
|
||||
done();
|
||||
}
|
||||
@@ -45,14 +47,14 @@ describe.skip('e2e transactions', () => {
|
||||
.accountNonce(keyring.alice.address())
|
||||
.pipe(
|
||||
first(),
|
||||
switchMap((nonce) =>
|
||||
switchMap((nonce: any) =>
|
||||
api.tx.democracy
|
||||
.propose(api.tx.consensus.setCode('0xdeadbeef'), 10000)
|
||||
.sign(keyring.alice, { nonce })
|
||||
.send()
|
||||
)
|
||||
)
|
||||
.subscribe(({ status }) => {
|
||||
.subscribe(({ status }: SubmittableResult) => {
|
||||
if (status.isFinalized) {
|
||||
done();
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import fromMetadata from '@polkadot/storage/fromMetadata';
|
||||
import { Storage } from '@polkadot/storage/types';
|
||||
import Metadata from '@polkadot/types/Metadata';
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Ws from '@polkadot/rpc-provider/ws';
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
|
||||
import Rpc from '../../src';
|
||||
|
||||
@@ -11,7 +11,7 @@ describe.skip('e2e basics', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
api = new Rpc(new Ws('ws://127.0.0.1:9944'));
|
||||
api = new Rpc(new WsProvider('ws://127.0.0.1:9944'));
|
||||
});
|
||||
|
||||
it('retrieves the pending extrinsics', () => {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Ws from '@polkadot/rpc-provider/ws';
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
|
||||
import Rpc from '../../src';
|
||||
|
||||
@@ -11,7 +11,7 @@ describe.skip('e2e chain', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
api = new Rpc(new Ws('ws://127.0.0.1:9944'));
|
||||
api = new Rpc(new WsProvider('ws://127.0.0.1:9944'));
|
||||
});
|
||||
|
||||
it('subscribes via subscribeNewHead', (done) => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import storage from '@polkadot/storage';
|
||||
import Ws from '@polkadot/rpc-provider/ws';
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
|
||||
import Rpc from '../../src';
|
||||
|
||||
@@ -12,7 +12,7 @@ describe.skip('e2e krumme lanke', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
api = new Rpc(new Ws('ws://127.0.0.1:9944'));
|
||||
api = new Rpc(new WsProvider('ws://127.0.0.1:9944'));
|
||||
});
|
||||
|
||||
it('subscribes to storage', (done) => {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Ws from '@polkadot/rpc-provider/ws';
|
||||
import storage from '@polkadot/storage/static';
|
||||
|
||||
import Rpc from '../../src';
|
||||
@@ -14,7 +13,7 @@ describe.skip('e2e state', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
api = new Rpc(new Ws('ws://127.0.0.1:9944'));
|
||||
api = new Rpc(new WsProvider('ws://127.0.0.1:9944'));
|
||||
});
|
||||
|
||||
it('retrieves code', () => {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Ws from '@polkadot/rpc-provider/ws';
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
import storage from '@polkadot/storage/static';
|
||||
|
||||
import Rpc from '../../src';
|
||||
@@ -12,7 +12,7 @@ describe.skip('e2e subscriptions', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
api = new Rpc(new Ws('ws://127.0.0.1:9944'));
|
||||
api = new Rpc(new WsProvider('ws://127.0.0.1:9944'));
|
||||
});
|
||||
|
||||
it('retrieves current timestamp', (done) => {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { JsonRpcResponse } from '../types';
|
||||
import Coder from '.';
|
||||
import Coder from './';
|
||||
|
||||
describe('decodeResponse', () => {
|
||||
let coder: Coder;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Coder from '.';
|
||||
import Coder from './';
|
||||
|
||||
describe('encodeJson', () => {
|
||||
let coder: Coder;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Coder from '.';
|
||||
import Coder from './';
|
||||
|
||||
describe('encodeObject', () => {
|
||||
let coder: Coder;
|
||||
|
||||
+4
-5
@@ -3,11 +3,10 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { TEST_HTTP_URL } from '../../test/mockHttp';
|
||||
|
||||
import Http from '.';
|
||||
import Http from './';
|
||||
|
||||
describe('Http', () => {
|
||||
let http;
|
||||
let http: Http;
|
||||
|
||||
beforeEach(() => {
|
||||
http = new Http(TEST_HTTP_URL);
|
||||
@@ -30,13 +29,13 @@ describe('Http', () => {
|
||||
});
|
||||
|
||||
it('does not (yet) support subscribe', () => {
|
||||
return http.subscribe().catch((error) => {
|
||||
return http.subscribe('', '', [], (cb) => { expect(cb).toEqual(expect.anything()); }).catch((error) => {
|
||||
expect(error.message).toMatch(/does not have subscriptions/);
|
||||
});
|
||||
});
|
||||
|
||||
it('does not (yet) support unsubscribe', () => {
|
||||
return http.unsubscribe().catch((error) => {
|
||||
return http.unsubscribe('', '', 0).catch((error) => {
|
||||
expect(error.message).toMatch(/does not have subscriptions/);
|
||||
});
|
||||
});
|
||||
+12
-5
@@ -2,12 +2,15 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Global } from './../mock/types';
|
||||
|
||||
declare const global: Global;
|
||||
|
||||
describe('http/polyfill', () => {
|
||||
let origFetch;
|
||||
let origFetch: GlobalFetch;
|
||||
|
||||
beforeEach(() => {
|
||||
origFetch = global.fetch;
|
||||
global.fetch = null;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -15,12 +18,16 @@ describe('http/polyfill', () => {
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (without fetch)', () => {
|
||||
expect(require('./polyfill')).toBeDefined();
|
||||
(global as any).fetch = undefined;
|
||||
require('./polyfill');
|
||||
|
||||
expect(global.fetch).toBeDefined();
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (with fetch)', () => {
|
||||
global.fetch = () => true;
|
||||
(global as any).fetch = () => true;
|
||||
require('./polyfill');
|
||||
|
||||
expect(require('./polyfill')).toBeDefined();
|
||||
expect(global.fetch).toBeDefined();
|
||||
});
|
||||
});
|
||||
+5
-6
@@ -2,13 +2,13 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Http from './';
|
||||
import { Mock } from './../mock/types';
|
||||
import { mockHttp, TEST_HTTP_URL } from '../../test/mockHttp';
|
||||
|
||||
import Http from '.';
|
||||
|
||||
describe('send', () => {
|
||||
let http;
|
||||
let mock;
|
||||
let http: Http;
|
||||
let mock: Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
http = new Http(TEST_HTTP_URL);
|
||||
@@ -17,7 +17,6 @@ describe('send', () => {
|
||||
afterEach(() => {
|
||||
if (mock) {
|
||||
mock.done();
|
||||
mock = null;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -32,7 +31,7 @@ describe('send', () => {
|
||||
return http
|
||||
.send('test_body', ['param'])
|
||||
.then((result) => {
|
||||
expect(mock.body['test_body']).toEqual({
|
||||
expect((mock.body as any)['test_body']).toEqual({
|
||||
id: 1,
|
||||
jsonrpc: '2.0',
|
||||
method: 'test_body',
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import { ProviderInterface$Emitted } from '../types';
|
||||
|
||||
import Mock from '.';
|
||||
import Mock from './';
|
||||
|
||||
describe('on', () => {
|
||||
let mock: Mock;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Mock from '.';
|
||||
import Mock from './';
|
||||
|
||||
describe('send', () => {
|
||||
let mock: Mock;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Mock from '.';
|
||||
import Mock from './';
|
||||
|
||||
describe('subscribe', () => {
|
||||
let mock: Mock;
|
||||
|
||||
@@ -2,6 +2,21 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Server } from 'mock-socket';
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
export interface Global extends NodeJS.Global {
|
||||
WebSocket: Constructor<WebSocket>;
|
||||
fetch: GlobalFetch;
|
||||
}
|
||||
|
||||
export type Mock = {
|
||||
body: Object,
|
||||
requests: Number,
|
||||
server: Server
|
||||
done: () => {}
|
||||
};
|
||||
|
||||
export type MockState$Subscription$Callback = (error: Error | null, value: any) => void;
|
||||
|
||||
export type MockState$Subscriptions = {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Mock from '.';
|
||||
import Mock from './';
|
||||
|
||||
describe('unsubscribe', () => {
|
||||
let mock: Mock;
|
||||
|
||||
@@ -176,7 +176,7 @@ export default class WsProvider implements WSProviderInterface {
|
||||
* @param params Encoded paramaters as appliucable for the method
|
||||
* @param subscription Subscription details (internally used)
|
||||
*/
|
||||
async send (method: string, params: Array<any>, subscription?: SubscriptionHandler): Promise<any> {
|
||||
send (method: string, params: Array<any>, subscription?: SubscriptionHandler): Promise<any> {
|
||||
return new Promise((resolve, reject): void => {
|
||||
try {
|
||||
const json = this.coder.encodeJson(method, params);
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright 2017-2019 @polkadot/rpc-provider 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 { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import Ws from '.';
|
||||
|
||||
describe('onConnect', () => {
|
||||
let mock;
|
||||
|
||||
beforeEach(() => {
|
||||
mock = mockWs([]);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (mock) {
|
||||
mock.done();
|
||||
mock = null;
|
||||
}
|
||||
});
|
||||
|
||||
it('sets up the on* handlers', () => {
|
||||
const ws = new Ws(TEST_WS_URL, false);
|
||||
|
||||
ws.connect();
|
||||
|
||||
expect(ws.websocket.onclose[0]).toBeDefined();
|
||||
expect(ws.websocket.onerror[0]).toBeDefined();
|
||||
expect(ws.websocket.onmessage[0]).toBeDefined();
|
||||
expect(ws.websocket.onopen[0]).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright 2017-2019 @polkadot/rpc-provider 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 WsProvider from './';
|
||||
import { Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
describe('onConnect', () => {
|
||||
let mock: Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
mock = mockWs([]);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (mock) {
|
||||
mock.done();
|
||||
}
|
||||
});
|
||||
|
||||
it('Does not connect when autoConnect is false', () => {
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, false);
|
||||
// We need to access the private WsProvider property 'websocket' here which would otherwise trigger a tslint error.
|
||||
// @ts-ignore
|
||||
expect(provider.websocket).toBeNull();
|
||||
});
|
||||
|
||||
it('Does connect when autoConnect is true', () => {
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, true);
|
||||
// @ts-ignore
|
||||
expect(provider.websocket).not.toBeNull();
|
||||
});
|
||||
|
||||
it('Creates a new WebSocket instance by calling the connect() method', () => {
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, false);
|
||||
// @ts-ignore
|
||||
expect(provider.websocket).toBeNull();
|
||||
|
||||
provider.connect();
|
||||
|
||||
// @ts-ignore
|
||||
expect(provider.websocket).not.toBeNull();
|
||||
// @ts-ignore
|
||||
expect(provider.websocket instanceof WebSocket).toBe(true);
|
||||
});
|
||||
|
||||
it('Creates the on handlers', () => {
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL);
|
||||
|
||||
// @ts-ignore
|
||||
expect(provider.websocket).not.toBeNull();
|
||||
// @ts-ignore
|
||||
expect(provider.websocket).toEqual(expect.objectContaining({
|
||||
listeners: expect.objectContaining({
|
||||
close: [expect.any(Function)],
|
||||
error: [expect.any(Function)],
|
||||
message: [expect.any(Function)],
|
||||
open: [expect.any(Function)]
|
||||
})
|
||||
}));
|
||||
});
|
||||
});
|
||||
+7
-9
@@ -2,17 +2,16 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import WsProvider from './';
|
||||
import { Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import Ws from '.';
|
||||
let ws: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
let ws;
|
||||
let mock;
|
||||
|
||||
function createWs (requests, autoConnect) {
|
||||
function createWs (requests: Array<any>, autoConnect: boolean | undefined) {
|
||||
mock = mockWs(requests);
|
||||
ws = new Ws(TEST_WS_URL, autoConnect);
|
||||
|
||||
ws = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
return ws;
|
||||
}
|
||||
|
||||
@@ -20,13 +19,12 @@ describe('Ws', () => {
|
||||
afterEach(() => {
|
||||
if (mock) {
|
||||
mock.done();
|
||||
mock = null;
|
||||
}
|
||||
});
|
||||
|
||||
it('returns the connected state', () => {
|
||||
expect(
|
||||
createWs([]).isConnected()
|
||||
createWs([],true).isConnected()
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -1,24 +0,0 @@
|
||||
// Copyright 2017-2019 @polkadot/rpc-provider 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 Ws from '.';
|
||||
|
||||
describe('onMessageResult', () => {
|
||||
let ws;
|
||||
|
||||
beforeEach(() => {
|
||||
ws = new Ws('ws://127.0.0.1:1234', false);
|
||||
});
|
||||
|
||||
it('calls the handler when found', (done) => {
|
||||
ws.handlers[5] = {
|
||||
callback: (_, result) => {
|
||||
expect(result).toEqual('test');
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
ws.onSocketMessage({ data: '{"jsonrpc":"2.0","id":5,"result":"test"}' });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2019 @polkadot/rpc-provider 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 WsProvider from './';
|
||||
|
||||
describe('WsProvider', () => {
|
||||
let provider: WsProvider;
|
||||
|
||||
beforeEach(() => {
|
||||
provider = new WsProvider('ws://127.0.0.1:1234', false);
|
||||
});
|
||||
|
||||
it('calls the handler when found', (done) => {
|
||||
// We need to access the private WsProvider property 'handlers' here which otherwise trigger a tslint error..
|
||||
// @ts-ignore
|
||||
provider.handlers[5] = {
|
||||
callback: (_: any, result: any) => {
|
||||
expect(result).toEqual('test');
|
||||
done();
|
||||
},
|
||||
method: '',
|
||||
params: []
|
||||
};
|
||||
|
||||
// We need to access the private WsProvider property 'onSocketMessage' here which would otherwise trigger a tslint error.
|
||||
// @ts-ignore
|
||||
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","id":5,"result":"test"}' }));
|
||||
});
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
// Copyright 2017-2019 @polkadot/rpc-provider 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 Ws from '.';
|
||||
|
||||
describe('onMessageSubscribe', () => {
|
||||
let ws;
|
||||
|
||||
beforeEach(() => {
|
||||
ws = new Ws('ws://127.0.0.1:1234', false);
|
||||
});
|
||||
|
||||
it('calls the subscriber with data', (done) => {
|
||||
ws.handlers[11] = {
|
||||
callback: (_, id) => {},
|
||||
method: 'test',
|
||||
subscription: {
|
||||
callback: (_, result) => {
|
||||
expect(result).toEqual('test');
|
||||
done();
|
||||
},
|
||||
type: 'test'
|
||||
}
|
||||
};
|
||||
|
||||
ws.onSocketMessage({ data: '{"jsonrpc":"2.0","id":11,"result":22}' });
|
||||
ws.onSocketMessage({ data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"result":"test"}}' });
|
||||
});
|
||||
|
||||
it('calls the subscriber with error', (done) => {
|
||||
ws.handlers[11] = {
|
||||
callback: (_, id) => {},
|
||||
method: 'test',
|
||||
subscription: {
|
||||
callback: (error) => {
|
||||
expect(error.message).toMatch(/test/);
|
||||
done();
|
||||
},
|
||||
type: 'test'
|
||||
}
|
||||
};
|
||||
|
||||
ws.onSocketMessage({ data: '{"jsonrpc":"2.0","id":11,"result":22}' });
|
||||
ws.onSocketMessage({ data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"error":{"message":"test"}}}' });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright 2017-2019 @polkadot/rpc-provider 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 WsProvider from './';
|
||||
|
||||
describe('onMessageSubscribe', () => {
|
||||
let provider: WsProvider;
|
||||
|
||||
beforeEach(() => {
|
||||
provider = new WsProvider('ws://127.0.0.1:1234', false);
|
||||
});
|
||||
|
||||
it('calls the subscriber with data', (done) => {
|
||||
// We need to access the private WsProvider property 'handlers' here which otherwise triggers a tslint error..
|
||||
// @ts-ignore
|
||||
provider.handlers[11] = {
|
||||
callback: (_: any, id: number) => {
|
||||
expect(typeof id).toBe('number');
|
||||
},
|
||||
method: 'test',
|
||||
params: [],
|
||||
subscription: {
|
||||
callback: (_: any, result: any) => {
|
||||
expect(result).toEqual('test');
|
||||
done();
|
||||
},
|
||||
type: 'test'
|
||||
}
|
||||
};
|
||||
|
||||
// We need to access the private WsProvider property 'onSocketMessage' here which would otherwise trigger a tslint error.
|
||||
// @ts-ignore
|
||||
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","id":11,"result":22}' }));
|
||||
// @ts-ignore
|
||||
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"result":"test"}}' }));
|
||||
});
|
||||
|
||||
it('calls the subscriber with error', (done) => {
|
||||
// We need to access the private WsProvider property 'handlers' here which otherwise trigger a tslint error..
|
||||
// @ts-ignore
|
||||
provider.handlers[11] = {
|
||||
callback: (_: any, id: number) => {
|
||||
expect(typeof id).toBe('number');
|
||||
},
|
||||
method: 'test',
|
||||
params: [],
|
||||
subscription: {
|
||||
callback: (error: any) => {
|
||||
expect(error.message).toMatch(/test/);
|
||||
done();
|
||||
},
|
||||
type: 'test'
|
||||
}
|
||||
};
|
||||
|
||||
// We need to access the private WsProvider property 'onSocketMessage' here which would otherwise trigger a tslint error.
|
||||
// @ts-ignore
|
||||
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","id":11,"result":22}' }));
|
||||
// @ts-ignore
|
||||
provider.onSocketMessage(new MessageEvent('test', { data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"error":{"message":"test"}}}' }));
|
||||
});
|
||||
});
|
||||
+6
-7
@@ -2,16 +2,16 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import WsProvider from './';
|
||||
import { Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import Ws from '.';
|
||||
let ws: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
let ws;
|
||||
let mock;
|
||||
|
||||
function createWs (requests, autoConnect) {
|
||||
function createWs (requests: Array<any>, autoConnect: boolean | undefined) {
|
||||
mock = mockWs(requests);
|
||||
ws = new Ws(TEST_WS_URL, autoConnect);
|
||||
ws = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
return ws;
|
||||
}
|
||||
@@ -20,7 +20,6 @@ describe('onOpen', () => {
|
||||
afterEach(() => {
|
||||
if (mock) {
|
||||
mock.done();
|
||||
mock = null;
|
||||
}
|
||||
});
|
||||
|
||||
+16
-7
@@ -2,25 +2,34 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import { Global } from './../mock/types';
|
||||
|
||||
declare let global: Global;
|
||||
|
||||
describe('ws/polyfill', () => {
|
||||
let origWs;
|
||||
let origWs: Constructor<WebSocket>;
|
||||
|
||||
beforeEach(() => {
|
||||
origWs = global.WebSocket;
|
||||
global.WebSocket = null;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
global.WebSocket = origWs;
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (without WebSocket)', () => {
|
||||
expect(require('./polyfill')).toBeDefined();
|
||||
it('polyfills with no exceptions (with WebSocket)', () => {
|
||||
(global as any).WebSocket = undefined;
|
||||
require('./polyfill');
|
||||
|
||||
expect(global.WebSocket).toBeDefined();
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (with WebSocket)', () => {
|
||||
global.WebSocket = () => true;
|
||||
it('polyfills with no exceptions (without WebSocket)', () => {
|
||||
(global as any).WebSocket = () => true;
|
||||
require('./polyfill');
|
||||
|
||||
expect(require('./polyfill')).toBeDefined();
|
||||
expect(global.WebSocket).toBeDefined();
|
||||
});
|
||||
});
|
||||
+22
-25
@@ -2,25 +2,28 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import WsProvider from './';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import Ws from '.';
|
||||
declare const global: Global;
|
||||
let provider: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
let ws;
|
||||
let mock;
|
||||
|
||||
function createMock (requests) {
|
||||
function createMock (requests: Array<any>) {
|
||||
mock = mockWs(requests);
|
||||
}
|
||||
|
||||
function createWs (autoConnect) {
|
||||
ws = new Ws(TEST_WS_URL, autoConnect);
|
||||
function createWs (autoConnect: boolean = true) {
|
||||
provider = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
return ws;
|
||||
return provider;
|
||||
}
|
||||
|
||||
describe('send', () => {
|
||||
let globalWs;
|
||||
let globalWs: Constructor<WebSocket>;
|
||||
|
||||
beforeEach(() => {
|
||||
globalWs = global.WebSocket;
|
||||
@@ -31,29 +34,23 @@ describe('send', () => {
|
||||
|
||||
if (mock) {
|
||||
mock.done();
|
||||
mock = null;
|
||||
}
|
||||
});
|
||||
|
||||
it('handles internal errors', () => {
|
||||
createMock([]);
|
||||
|
||||
let websocket;
|
||||
|
||||
global.WebSocket = class {
|
||||
constructor () {
|
||||
websocket = this;
|
||||
}
|
||||
|
||||
(global as any).WebSocket = class {
|
||||
send () {
|
||||
throw new Error('send error');
|
||||
}
|
||||
};
|
||||
|
||||
ws = createWs();
|
||||
websocket.onopen();
|
||||
provider = createWs(true);
|
||||
// @ts-ignore Accessing private method
|
||||
provider.websocket.onopen();
|
||||
|
||||
return ws
|
||||
return provider
|
||||
.send('test_encoding', ['param'])
|
||||
.catch((error) => {
|
||||
expect(error.message).toEqual('send error');
|
||||
@@ -69,11 +66,11 @@ describe('send', () => {
|
||||
}
|
||||
}]);
|
||||
|
||||
return createWs()
|
||||
return createWs(true)
|
||||
.send('test_body', ['param'])
|
||||
.then((result) => {
|
||||
expect(
|
||||
mock.body['test_body']
|
||||
(mock.body as any)['test_body']
|
||||
).toEqual('{"id":1,"jsonrpc":"2.0","method":"test_body","params":["param"]}');
|
||||
});
|
||||
});
|
||||
@@ -87,7 +84,7 @@ describe('send', () => {
|
||||
}
|
||||
}]);
|
||||
|
||||
return createWs()
|
||||
return createWs(true)
|
||||
.send('test_error', [])
|
||||
.catch((error) => {
|
||||
expect(error.message).toMatch(/666: error/);
|
||||
@@ -103,8 +100,8 @@ describe('send', () => {
|
||||
}
|
||||
}]);
|
||||
|
||||
return createWs()
|
||||
.send('test_sub', [], () => {})
|
||||
return createWs(true)
|
||||
.send('test_sub', [])
|
||||
.then((id) => {
|
||||
expect(id).toEqual(1);
|
||||
});
|
||||
+3
-3
@@ -2,18 +2,18 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Ws from '.';
|
||||
import WsProvider from './';
|
||||
|
||||
describe('state', () => {
|
||||
it('requires an ws:// prefixed endpoint', () => {
|
||||
expect(
|
||||
() => new Ws('http://', false)
|
||||
() => new WsProvider('http://', false)
|
||||
).toThrow(/with 'ws/);
|
||||
});
|
||||
|
||||
it('allows wss:// endpoints', () => {
|
||||
expect(
|
||||
() => new Ws('wss://', false)
|
||||
() => new WsProvider('wss://', false)
|
||||
).not.toThrow();
|
||||
});
|
||||
});
|
||||
+13
-11
@@ -2,25 +2,28 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import WsProvider from './';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import Ws from '.';
|
||||
declare var global: Global;
|
||||
let ws: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
let ws;
|
||||
let mock;
|
||||
|
||||
function createMock (requests) {
|
||||
function createMock (requests: Array<any>) {
|
||||
mock = mockWs(requests);
|
||||
}
|
||||
|
||||
function createWs (autoConnect) {
|
||||
ws = new Ws(TEST_WS_URL, autoConnect);
|
||||
function createWs (autoConnect: boolean = true) {
|
||||
ws = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
return ws;
|
||||
}
|
||||
|
||||
describe('subscribe', () => {
|
||||
let globalWs;
|
||||
let globalWs: Constructor<WebSocket>;
|
||||
|
||||
beforeEach(() => {
|
||||
globalWs = global.WebSocket;
|
||||
@@ -31,7 +34,6 @@ describe('subscribe', () => {
|
||||
|
||||
if (mock) {
|
||||
mock.done();
|
||||
mock = null;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -44,8 +46,8 @@ describe('subscribe', () => {
|
||||
}
|
||||
}]);
|
||||
|
||||
return createWs()
|
||||
.subscribe('test_sub', [], () => {})
|
||||
return createWs(true)
|
||||
.subscribe('type', 'test_sub', [], (cb) => { expect(cb).toEqual(expect.anything()); })
|
||||
.then((id) => {
|
||||
expect(id).toEqual(1);
|
||||
});
|
||||
+15
-13
@@ -2,25 +2,28 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import WsProvider from './';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
|
||||
import Ws from '.';
|
||||
declare var global: Global;
|
||||
let ws: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
let ws;
|
||||
let mock;
|
||||
|
||||
function createMock (requests) {
|
||||
function createMock (requests: any) {
|
||||
mock = mockWs(requests);
|
||||
}
|
||||
|
||||
function createWs (autoConnect) {
|
||||
ws = new Ws(TEST_WS_URL, autoConnect);
|
||||
function createWs (autoConnect: boolean = true) {
|
||||
ws = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
return ws;
|
||||
}
|
||||
|
||||
describe('subscribe', () => {
|
||||
let globalWs;
|
||||
let globalWs: Constructor<WebSocket>;
|
||||
|
||||
beforeEach(() => {
|
||||
globalWs = global.WebSocket;
|
||||
@@ -31,7 +34,6 @@ describe('subscribe', () => {
|
||||
|
||||
if (mock) {
|
||||
mock.done();
|
||||
mock = null;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -53,10 +55,10 @@ describe('subscribe', () => {
|
||||
}
|
||||
]);
|
||||
|
||||
const ws = createWs();
|
||||
const ws = createWs(true);
|
||||
|
||||
return ws
|
||||
.subscribe('test', 'subscribe_test', [], () => {})
|
||||
.subscribe('test', 'subscribe_test', [], (cb) => { expect(cb).toEqual(expect.anything()); })
|
||||
.then((id) => {
|
||||
return ws.unsubscribe('test', 'subscribe_test', id);
|
||||
});
|
||||
@@ -71,10 +73,10 @@ describe('subscribe', () => {
|
||||
}
|
||||
}]);
|
||||
|
||||
const ws = createWs();
|
||||
const ws = createWs(true);
|
||||
|
||||
return ws
|
||||
.subscribe('test', 'subscribe_test', [], () => {})
|
||||
.subscribe('test', 'subscribe_test', [], (cb) => { expect(cb).toEqual(expect.anything()); })
|
||||
.then((id) => {
|
||||
return ws.unsubscribe('test', 'subscribe_test', 111);
|
||||
})
|
||||
@@ -46,11 +46,12 @@ function mockWs (requests: Array<{ method: string }>) {
|
||||
server = new Server(TEST_WS_URL);
|
||||
|
||||
let requestCount = 0;
|
||||
const scope: { body: { [index: string]: {} }, requests: number, server: Server, done: any } = {
|
||||
body: {},
|
||||
requests: 0,
|
||||
server,
|
||||
done: () =>
|
||||
const scope: {
|
||||
body: { [index: string]: {} },
|
||||
requests: number,
|
||||
server: Server,
|
||||
done: any
|
||||
} = { body: {}, requests: 0, server, done: () =>
|
||||
server.stop(() => {
|
||||
// ignore
|
||||
})
|
||||
@@ -58,7 +59,7 @@ function mockWs (requests: Array<{ method: string }>) {
|
||||
|
||||
server.on('connection', (socket) => {
|
||||
// @ts-ignore definitions are wrong, this is 'on', not 'onmessage'
|
||||
socket.on('message', (body: {}) => {
|
||||
socket.on('message', (body: { [index: string]: {} }) => {
|
||||
const request = requests[requestCount];
|
||||
// @ts-ignore Yes, SHOULD be fixed, this is a mess
|
||||
const response = request.error
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
import { RpcInterface$Section } from '@polkadot/rpc-core/types';
|
||||
import { AccountId } from '@polkadot/types';
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import { RpcInterface$Section } from '@polkadot/rpc-core/types';
|
||||
|
||||
jest.mock('@polkadot/rpc-provider/ws', () => class {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Ws from '@polkadot/rpc-provider/ws';
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
import storage from '@polkadot/storage/static';
|
||||
|
||||
import RpcRx from '../../src';
|
||||
@@ -13,7 +13,7 @@ describe.skip('e2e subscriptions', () => {
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
|
||||
api = new RpcRx(new Ws('ws://127.0.0.1:9944'));
|
||||
api = new RpcRx(new WsProvider('ws://127.0.0.1:9944'));
|
||||
});
|
||||
|
||||
it('retrieves current timestamp', (done) => {
|
||||
|
||||
+19
-17
@@ -2,57 +2,59 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { RpcInterface } from '@polkadot/rpc-core/types';
|
||||
import { SubmittableResult } from '@polkadot/api/SubmittableExtrinsic';
|
||||
import Rpc from '@polkadot/rpc-core';
|
||||
import Ws from '@polkadot/rpc-provider/ws';
|
||||
import WsProvider from '@polkadot/rpc-provider/ws';
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
|
||||
import extrinsics from '../../src/static';
|
||||
import extrinsics from './../../src/static';
|
||||
|
||||
const keyring = testingPairs({ type: 'ed25519' });
|
||||
|
||||
// @TODO 1: All three tests below are broken, fix or find better way to test transfers.
|
||||
// @TODO 2: Move tests out of ./type-extrinsics to ./api folder
|
||||
describe.skip('e2e transfer', () => {
|
||||
let api;
|
||||
let api: RpcInterface;
|
||||
|
||||
beforeAll(() => {
|
||||
api = new Rpc(new Ws('ws://127.0.0.1:9944'));
|
||||
api = new Rpc(new WsProvider('ws://127.0.0.1:9944'));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.setTimeout(30000);
|
||||
});
|
||||
|
||||
// Error: [1002]: Inherent transactions cannot be queued.
|
||||
it.skip('inherent test', () => {
|
||||
it('inherent test', () => {
|
||||
const inherent = extrinsics.timestamp.set(Math.round(Date.now() / 1000));
|
||||
|
||||
return api.author.submitExtrinsic(inherent.toU8a()).then(console.log);
|
||||
});
|
||||
|
||||
it.skip('makes a transfer for a transaction', () => {
|
||||
it('makes a transfer for a transaction', () => {
|
||||
return api.chain
|
||||
.getBlockHash(0)
|
||||
.then((genesisHash) => {
|
||||
const extrinsic = extrinsics.balances.transfer(keyring.bob.publicKey(), 6969);
|
||||
|
||||
const extrinsic = extrinsics.balances.transfer(keyring.bob.publicKey(), 6969) as any;
|
||||
extrinsic.sign(keyring.alice, { blockHash: genesisHash, nonce: 0 });
|
||||
|
||||
return api.author.submitExtrinsic(extrinsic.toU8a());
|
||||
})
|
||||
.then(console.log);
|
||||
});
|
||||
});
|
||||
|
||||
it('makes a transfer via watch', (done) => {
|
||||
return api.chain
|
||||
.getBlockHash(0)
|
||||
.then((genesisHash) => {
|
||||
const extrinsic = extrinsics.balances.transfer(keyring.bob.publicKey(), 6969);
|
||||
|
||||
const extrinsic = extrinsics.balances.transfer(keyring.bob.publicKey(), 6969) as any;
|
||||
extrinsic.sign(keyring.alice, { blockHash: genesisHash, nonce: 0 });
|
||||
|
||||
return api.author.submitAndWatchExtrinsic(extrinsic, (result) => {
|
||||
console.log(result);
|
||||
|
||||
return api.author.submitAndWatchExtrinsic(extrinsic, (result: SubmittableResult) => {
|
||||
if (result.status.isFinalized) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(console.log);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import interfaces from '.';
|
||||
import interfaces from './';
|
||||
|
||||
describe('jsonrpc', () => {
|
||||
it('exports the available interfaces', () => {
|
||||
@@ -7,7 +7,7 @@ import { Metadata } from '@polkadot/types';
|
||||
import json from '@polkadot/types/Metadata/v4/static';
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import fromMetadata from '.';
|
||||
import fromMetadata from './';
|
||||
|
||||
const keyring = testingPairs({ type: 'ed25519' });
|
||||
// Use the pre-generated metadata
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from './types';
|
||||
|
||||
import extrinsics from '@polkadot/extrinsics/static';
|
||||
|
||||
import { Constructor } from './types';
|
||||
import * as Classes from './index.types';
|
||||
|
||||
const Types = Classes as { [index: string]: Constructor };
|
||||
|
||||
Reference in New Issue
Block a user