1 |
|
2 |
|
3 |
|
4 |
|
5 | import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
6 |
|
7 | import Ws from './index';
|
8 |
|
9 | let ws;
|
10 | let mock;
|
11 |
|
12 | function createMock (requests) {
|
13 | mock = mockWs(requests);
|
14 | }
|
15 |
|
16 | function createWs (autoConnect) {
|
17 | ws = new Ws(TEST_WS_URL, autoConnect);
|
18 |
|
19 | return ws;
|
20 | }
|
21 |
|
22 | describe('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('removes subscriptions', () => {
|
39 | createMock([
|
40 | {
|
41 | id: 1,
|
42 | method: 'subscribe_test',
|
43 | reply: {
|
44 | result: 1
|
45 | }
|
46 | },
|
47 | {
|
48 | id: 2,
|
49 | method: 'unsubscribe_test',
|
50 | reply: {
|
51 | result: true
|
52 | }
|
53 | }
|
54 | ]);
|
55 |
|
56 | const ws = createWs();
|
57 |
|
58 | return ws
|
59 | .subscribe('test', 'subscribe_test', [], () => {})
|
60 | .then((id) => {
|
61 | return ws.unsubscribe('test', 'subscribe_test', id);
|
62 | });
|
63 | });
|
64 |
|
65 | it('fails when sub not found', () => {
|
66 | createMock([{
|
67 | id: 1,
|
68 | method: 'subscribe_test',
|
69 | reply: {
|
70 | result: 1
|
71 | }
|
72 | }]);
|
73 |
|
74 | const ws = createWs();
|
75 |
|
76 | return ws
|
77 | .subscribe('test', 'subscribe_test', [], () => {})
|
78 | .then((id) => {
|
79 | return ws.unsubscribe('test', 'subscribe_test', 111);
|
80 | })
|
81 | .catch((error) => {
|
82 | expect(error.message).toMatch(/find active subscription/);
|
83 | });
|
84 | });
|
85 | });
|