solana/web3.js/test/__mocks__/rpc-websockets.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-10-26 21:37:39 -07:00
import {Client as RpcWebSocketClient} from 'rpc-websockets';
2018-11-01 14:41:06 -07:00
// Define TEST_LIVE in the environment to test against the real full node
2018-10-26 21:37:39 -07:00
// identified by `url` instead of using the mock
2018-11-01 14:41:06 -07:00
export const mockRpcEnabled = !process.env.TEST_LIVE;
2018-10-26 21:37:39 -07:00
let mockNotice = true;
export class Client {
client: RpcWebSocketClient;
constructor(url, options) {
//console.log('MockClient', url, options);
if (!mockRpcEnabled) {
if (mockNotice) {
2018-11-04 11:41:21 -08:00
console.log(
'Note: rpc-websockets mock is disabled, testing live against',
url,
);
2018-10-26 21:37:39 -07:00
mockNotice = false;
}
this.client = new RpcWebSocketClient(url, options);
}
}
connect() {
if (!mockRpcEnabled) {
return this.client.connect();
}
}
close() {
if (!mockRpcEnabled) {
return this.client.close();
}
}
on(event: string, callback: Function) {
if (!mockRpcEnabled) {
return this.client.on(event, callback);
}
//console.log('on', event);
}
async call(method: string, params: Object): Promise<Object> {
if (!mockRpcEnabled) {
return await this.client.call(method, params);
}
throw new Error('call unsupported');
}
}