UNPKG

961 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 { mockWs, TEST_WS_URL } from '../../test/mockWs';
6
7import Ws from './index';
8
9let ws;
10let mock;
11
12function createMock (requests) {
13 mock = mockWs(requests);
14}
15
16function createWs (autoConnect) {
17 ws = new Ws(TEST_WS_URL, autoConnect);
18
19 return ws;
20}
21
22describe('subscribe', () => {
23 let globalWs;
24
25 beforeEach(() => {
26 globalWs = global.WebSocket;
27 });
28
29 afterEach(() => {
30 global.WebSocket = globalWs;
31
32 if (mock) {
33 mock.done();
34 mock = null;
35 }
36 });
37
38 it('adds subscriptions', () => {
39 createMock([{
40 id: 1,
41 method: 'test_sub',
42 reply: {
43 result: 1
44 }
45 }]);
46
47 return createWs()
48 .subscribe('test_sub', [], () => {})
49 .then((id) => {
50 expect(id).toEqual(1);
51 });
52 });
53});