Files
api/packages/api-provider/src/ws/send.spec.js
T
Jaco Greeff 21c14bd85c Add pubsub interface definition (#54)
* Add pubsub interface definition

* Functional JsonRpcCoder

* Functional HttpProvider

* Functional WsProvider

* Functional split completed

* Split actual method subscription

* Update flow

* Flatten no user-specified array in sub call
2018-03-15 12:59:49 +01:00

95 lines
1.7 KiB
JavaScript

// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { mockWs, TEST_WS_URL } = require('../../test/mockWs');
let ws;
let mock;
function createMock (requests) {
mock = mockWs(requests);
}
function createWs (autoConnect) {
ws = require('./index')(TEST_WS_URL, autoConnect);
return ws;
}
describe('send', () => {
let globalWs;
beforeEach(() => {
globalWs = global.WebSocket;
});
afterEach(() => {
global.WebSocket = globalWs;
if (mock) {
mock.done();
mock = null;
}
});
it('handles internal errors', () => {
createMock([]);
let websocket;
global.WebSocket = class {
constructor () {
websocket = this;
}
send () {
throw new Error('send error');
}
};
ws = createWs();
websocket.onopen();
return ws
.send('test_encoding', ['param'])
.catch((error) => {
expect(error.message).toEqual('send error');
});
});
it('passes the body through correctly', () => {
createMock([{
id: 1,
method: 'test_body',
reply: {
result: 'ok'
}
}]);
return createWs()
.send('test_body', ['param'])
.then((result) => {
expect(
mock.body['test_body']
).toEqual('{"id":1,"jsonrpc":"2.0","method":"test_body","params":["param"]}');
});
});
it('throws error when !response.ok', () => {
createMock([{
id: 1,
error: {
code: 666,
message: 'error'
}
}]);
return createWs()
.send('test_error', [])
.catch((error) => {
expect(error.message).toMatch(/\[666\]: error/);
});
});
});