Skip to content

Commit 6d93e74

Browse files
committed
adding js tests
1 parent fd6d57a commit 6d93e74

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

js-tests/Connector.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import WS from "jest-websocket-mock";
22
import {Connector} from "../js-src/Connector";
3+
import {Channel} from "../js-src/Channel";
34

45
describe('Connector', () => {
5-
let server;
6+
let server: WS;
67

78
beforeEach(() => {
9+
if (server) {
10+
server.close()
11+
}
12+
813
server = new WS("ws://localhost:1234");
914
});
1015

@@ -16,9 +21,37 @@ describe('Connector', () => {
1621
await server.connected;
1722

1823
await expect(server).toReceiveMessage('{"event":"whoami"}');
19-
2024
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id"}}')
2125

2226
expect(connector.socketId()).toBe('test-socket-id')
2327
})
28+
29+
test('we can subscribe to a channel and listen to events', async () => {
30+
const connector = new Connector({
31+
host: "ws://localhost:1234",
32+
})
33+
34+
await server.connected;
35+
36+
await expect(server).toReceiveMessage('{"event":"whoami"}');
37+
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id"}}')
38+
39+
const channel = connector.channel('my-test-channel')
40+
41+
await expect(server).toReceiveMessage('{"event":"subscribe","data":{"channel":"my-test-channel"}}');
42+
43+
server.send('{"event":"subscription_succeeded","channel":"my-test-channel"}')
44+
45+
expect(channel).toBeInstanceOf(Channel)
46+
47+
const handler1 = jest.fn();
48+
const handler2 = jest.fn();
49+
50+
channel.on('my-test-event', handler1)
51+
52+
server.send('{"event":"my-test-event","channel":"my-test-channel","data":{}}')
53+
54+
expect(handler1).toBeCalled();
55+
expect(handler2).not.toBeCalled();
56+
})
2457
});

0 commit comments

Comments
 (0)