-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathConnector.test.ts
109 lines (74 loc) · 3.29 KB
/
Connector.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import WS from "jest-websocket-mock";
import { Connector } from "../js-src/Connector";
import { Channel } from "../js-src/Channel";
const mockedHost = 'ws://localhost:1234';
describe('Connector', () => {
let server: WS;
beforeEach(() => {
jest.useRealTimers();
server = new WS(mockedHost);
});
afterEach(() => {
server.close()
});
test('socket id is correctly set', async () => {
const connector = new Connector({
host: mockedHost,
})
await server.connected;
await expect(server).toReceiveMessage('{"event":"whoami"}');
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id"}}')
expect(connector.socketId()).toBe('test-socket-id')
})
test('we reconnect to the server on error', async () => {
const connector = new Connector({
host: mockedHost,
})
await server.connected;
await expect(server).toReceiveMessage('{"event":"whoami"}');
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id"}}')
server.close();
await server.closed;
server.server.stop(() => (server = new WS(mockedHost)));
await server.connected;
await expect(server).toReceiveMessage('{"event":"whoami"}');
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id2"}}')
expect(connector.socketId()).toBe('test-socket-id2')
})
test('we can subscribe to a channel and listen to events', async () => {
const connector = new Connector({
host: mockedHost,
})
await server.connected;
await expect(server).toReceiveMessage('{"event":"whoami"}');
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id"}}')
const channel = connector.channel('my-test-channel')
await expect(server).toReceiveMessage('{"event":"subscribe","data":{"channel":"my-test-channel"}}');
server.send('{"event":"subscription_succeeded","channel":"my-test-channel"}')
expect(channel).toBeInstanceOf(Channel)
const handler1 = jest.fn();
const handler2 = jest.fn();
channel.on('my-test-event', handler1)
server.send('{"event":"my-test-event","channel":"my-test-channel","data":{}}')
expect(handler1).toBeCalled();
expect(handler2).not.toBeCalled();
})
test('we can send a whisper event', async () => {
const connector = new Connector({
host: mockedHost,
})
await server.connected;
await expect(server).toReceiveMessage('{"event":"whoami"}');
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id"}}')
const channel = connector.channel('my-test-channel')
await expect(server).toReceiveMessage('{"event":"subscribe","data":{"channel":"my-test-channel"}}');
server.send('{"event":"subscription_succeeded","channel":"my-test-channel"}')
expect(channel).toBeInstanceOf(Channel)
const handler1 = jest.fn();
const handler2 = jest.fn();
channel.on('client-whisper', handler1)
server.send('{"event":"client-whisper","data":"whisper","channel":"my-test-channel","data":{}}')
expect(handler1).toBeCalled();
expect(handler2).not.toBeCalled();
})
});