E2E testing (#177)

* Move e2e tests to provider

* Rx subscriptions

* Import @polkadot/api-provider, fix package names

* Decrease verbosity, skip all e2e tests
This commit is contained in:
Jaco Greeff
2018-10-01 14:11:43 +02:00
committed by GitHub
parent d230c05d16
commit 876c34c532
11 changed files with 204 additions and 23 deletions
+2 -2
View File
@@ -6,7 +6,7 @@ import hexToU8a from '@polkadot/util/hex/toU8a';
import isHex from '@polkadot/util/is/hex';
import u8aConcat from '@polkadot/util/u8a/concat';
import Base, { l } from './Base';
import Base from './Base';
// A Struct defines an Object with key/values - where the values are Base<T> values. It removes
// a lot of repetition from the actual coding, define a structure type, pass it the key/Base<T>
@@ -43,7 +43,7 @@ export default class Struct <
}
static decode <S, V, T> (Types: S, value: V | Array<any>, isTuple: boolean): T {
l.debug(() => ['Struct.decode', { Types, value }]);
// l.debug(() => ['Struct.decode', { Types, value }]);
return Object
.keys(Types)
+5 -5
View File
@@ -5,7 +5,7 @@
import assert from '@polkadot/util/assert';
import * as Types from '../index';
import Base, { l } from './Base';
import Base from './Base';
import Tuple from './Tuple';
import Vector from './Vector';
@@ -64,7 +64,7 @@ export function typeSplit (type: string): Array<string> {
// Handle Vector types, i.e Vec<AccountId> or Vec<(AccountId, Balance)>
export function getVectorType (type: string): Constructor {
l.debug(() => ['getVectorType', { type }]);
// l.debug(() => ['getVectorType', { type }]);
assert(type.substr(0, 4) === 'Vec<' && type[type.length - 1] === '>', `Expected Vec wrapped with <>`);
@@ -79,7 +79,7 @@ export function getVectorType (type: string): Constructor {
// Handle tuple types, (u32, String, AccountId). It could be nested and wrapped in other
// types, i.e. (u32, Vec<(AccountId, Balance)>)
export function getTupleType (type: string): Constructor {
l.debug(() => ['getTupleType', { type }]);
// l.debug(() => ['getTupleType', { type }]);
assert(type[0] === '(' && type[type.length - 1] === ')', `Expected tuple wrapped with ()`);
@@ -99,7 +99,7 @@ export function getTupleType (type: string): Constructor {
export function getType (_type: string): Constructor {
const type = _type.trim();
l.debug(() => ['getType', { type }]);
// l.debug(() => ['getType', { type }]);
if (type[0] === '(') {
return getTupleType(type);
@@ -115,7 +115,7 @@ export function getType (_type: string): Constructor {
}
export default function createType (type: string, value?: any): Base {
l.debug(() => ['createType', { type, value }]);
// l.debug(() => ['createType', { type, value }]);
const Type = getType(type);
+4 -4
View File
@@ -28,10 +28,6 @@
"url": "https://github.com/polkadot-js/api/issues"
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-provider#readme",
"devDependencies": {
"mock-socket": "^8.0.2",
"nock": "^9.6.1"
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/storage": "^0.30.5",
@@ -42,5 +38,9 @@
"eventemitter3": "^3.1.0",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.25"
},
"devDependencies": {
"mock-socket": "^8.0.2",
"nock": "^9.6.1"
}
}
@@ -0,0 +1,32 @@
// Copyright 2017-2018 @polkadot/api-rx authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import storage from '@polkadot/storage/testing';
import Ws from '@polkadot/api-provider/ws';
import RxApi from '../../src';
describe.skip('e2e subscriptions', () => {
let api;
beforeEach(() => {
jest.setTimeout(30000);
api = new RxApi(new Ws('ws://127.0.0.1:9944'));
});
it('retrieves current timestamp', (done) => {
let count = 0;
api.state
.storage([[storage.timestamp.now]])
.subscribe((data) => {
console.error('subscribe', data);
if (++count === 4) {
done();
}
});
});
});
@@ -1,9 +1,10 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// Copyright 2017-2018 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import Api from '../../api/src';
import Ws from '../src/ws';
import Ws from '@polkadot/api-provider/ws';
import Api from '../../src';
describe.skip('e2e basics', () => {
let api;
@@ -1,9 +1,10 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// Copyright 2017-2018 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import Api from '../../api/src';
import Ws from '../src/ws';
import Ws from '@polkadot/api-provider/ws';
import Api from '../../src';
describe.skip('e2e chain', () => {
let api;
@@ -1,11 +1,11 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// Copyright 2017-2018 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import storage from '@polkadot/storage';
import Ws from '@polkadot/api-provider/ws';
import Api from '../../api/src';
import Ws from '../src/ws';
import Api from '../../src';
describe.skip('e2e krumme lanke', () => {
let api;
@@ -1,11 +1,11 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// Copyright 2017-2018 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import storage from '@polkadot/storage/testing';
import Ws from '@polkadot/api-provider/ws';
import Api from '../../api/src';
import Ws from '../src/ws';
import Api from '../../src';
const ALICE = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';
+37
View File
@@ -0,0 +1,37 @@
// Copyright 2017-2018 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import storage from '@polkadot/storage/testing';
import Ws from '@polkadot/api-provider/ws';
import Api from '../../src';
describe.skip('e2e subscriptions', () => {
let api;
beforeEach(() => {
jest.setTimeout(30000);
api = new Api(new Ws('ws://127.0.0.1:9944'));
});
it('retrieves current timestamp', (done) => {
let count = 0;
return api.state
.storage([[storage.timestamp.now]], (error, data) => {
if (error) {
return done(error);
}
expect(data).toBeDefined();
if (++count === 3) {
done();
}
})
.then((subscriptionId) => {
console.log('newHead: subscriptionId =', subscriptionId);
});
});
});
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import nock from 'nock';
const TEST_HTTP_URL = 'http://localhost:9944';
function mockHttp (requests: any[]) {
nock.cleanAll();
return requests.reduce((scope, request: any, index: number) => {
return scope
.post('/')
.reply(request.code || 200, (uri: string, body: { id: string }) => {
scope.body = scope.body || {};
scope.body[request.method] = body;
return Object.assign({ id: body.id, jsonrpc: '2.0' }, request.reply || {});
});
}, nock(TEST_HTTP_URL));
}
export {
TEST_HTTP_URL,
mockHttp
};
+83
View File
@@ -0,0 +1,83 @@
// Copyright 2017-2018 @polkadot/api-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Server } from 'mock-socket';
const TEST_WS_URL = 'ws://localhost:9955';
let server: Server;
type ErrorDef = {
id: number,
error: {
code: number,
message: string
}
};
type ReplyDef = {
id: number,
reply: {
result: any
}
};
function createError ({ id, error: { code, message } }: ErrorDef) {
return {
id,
jsonrpc: '2.0',
error: {
code,
message
}
};
}
function createReply ({ id, reply: { result } }: ReplyDef) {
return {
id,
jsonrpc: '2.0',
result
};
}
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: () =>
server.stop(() => {
// ignore
})
};
server.on('connection', (socket) => {
// @ts-ignore definitions are wrong, this is 'on', not 'onmessage'
socket.on('message', (body: {}) => {
const request = requests[requestCount];
// @ts-ignore Yes, SHOULD be fixed, this is a mess
const response = request.error
// @ts-ignore Yes, SHOULD be fixed, this is a mess
? createError(request)
// @ts-ignore Yes, SHOULD be fixed, this is a mess
: createReply(request);
scope.body[request.method] = body;
requestCount++;
socket.send(JSON.stringify(response));
});
});
return scope;
}
export {
TEST_WS_URL,
mockWs
};