UNPKG

958 BJavaScriptView Raw
1// Copyright 2017-2018 @polkadot/api-provider authors & contributors
2// This software may be modified and distributed under the terms
3// of the ISC license. See the LICENSE file for details.
4
5import Ws from './index';
6
7describe('onMessageResult', () => {
8 let ws;
9 let errorSpy;
10
11 beforeEach(() => {
12 ws = new Ws('ws://127.0.0.1:1234', false);
13 errorSpy = jest.spyOn(console, 'error');
14 });
15
16 afterEach(() => {
17 errorSpy.mockRestore();
18 });
19
20 it('fails with log when handler not found', () => {
21 ws.onSocketMessage({ data: '{"id":2}' });
22
23 expect(errorSpy).toHaveBeenCalledWith(
24 expect.anything(), expect.anything(), 'Unable to find handler for id=2'
25 );
26 });
27
28 it('calls the handler when found', (done) => {
29 ws.handlers[5] = {
30 callback: (_, result) => {
31 expect(result).toEqual('test');
32 done();
33 }
34 };
35
36 ws.onSocketMessage({ data: '{"jsonrpc":"2.0","id":5,"result":"test"}' });
37 });
38});