UNPKG

1.75 kBJavaScriptView 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('onMessageSubscribe', () => {
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: '{"method":"test","params":{"subscription":2}}' });
22
23 expect(errorSpy).toHaveBeenCalledWith(
24 expect.anything(), expect.anything(), 'Unable to find handler for subscription=test::2'
25 );
26 });
27
28 it('calls the subscriber with data', (done) => {
29 ws.handlers[11] = {
30 callback: (_, id) => {},
31 method: 'test',
32 subscription: {
33 callback: (_, result) => {
34 expect(result).toEqual('test');
35 done();
36 },
37 type: 'test'
38 }
39 };
40
41 ws.onSocketMessage({ data: '{"jsonrpc":"2.0","id":11,"result":22}' });
42 ws.onSocketMessage({ data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"result":"test"}}' });
43 });
44
45 it('calls the subscriber with error', (done) => {
46 ws.handlers[11] = {
47 callback: (_, id) => {},
48 method: 'test',
49 subscription: {
50 callback: (error) => {
51 expect(error.message).toMatch(/test/);
52 done();
53 },
54 type: 'test'
55 }
56 };
57
58 ws.onSocketMessage({ data: '{"jsonrpc":"2.0","id":11,"result":22}' });
59 ws.onSocketMessage({ data: '{"jsonrpc":"2.0","method":"test","params":{"subscription":22,"error":{"message":"test"}}}' });
60 });
61});