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:
Stefanie Doll
2019-06-12 16:45:45 +12:00
committed by Ian He
co-authored by Amaury Martiny
parent e8624244ea
commit 1ce536008d
53 changed files with 769 additions and 658 deletions
@@ -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;
@@ -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/);
});
});
@@ -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();
});
});
@@ -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',
+1 -1
View File
@@ -4,7 +4,7 @@
import { ProviderInterface$Emitted } from '../types';
import Mock from '.';
import Mock from './';
describe('on', () => {
let mock: Mock;
+1 -1
View File
@@ -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;
+15
View File
@@ -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;
+1 -1
View File
@@ -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)]
})
}));
});
});
@@ -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"}}}' }));
});
});
@@ -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;
}
});
@@ -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();
});
});
@@ -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);
});
@@ -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();
});
});
@@ -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);
});
@@ -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);
})