-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrooms.test.ts
More file actions
79 lines (61 loc) 路 2.59 KB
/
Copy pathrooms.test.ts
File metadata and controls
79 lines (61 loc) 路 2.59 KB
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
import { expect, it } from 'vitest';
import { setupServer } from './setup-server';
import { receive, track } from './test-events';
const ctx = setupServer();
it('joining a room receives emits for that room', async () => {
const { client: client1, serverSocket: socket1 } = await ctx.connectClient();
await socket1.join('room');
const got = receive(client1, 'msg');
ctx.io.to('room').emit('msg', 'hello');
await expect(got).resolves.toBe('hello');
});
it('a client that has not joined does not receive emits for that room', async () => {
const { client: client1, serverSocket: socket1 } = await ctx.connectClient();
const { client: client2, serverSocket: socket2 } = await ctx.connectClient();
await socket1.join('room');
const got1 = receive(client1, 'msg');
const msg2 = track(client2, 'msg');
const marker2 = receive(client2, 'marker');
ctx.io.to('room').emit('msg', 'hello');
socket2.emit('marker');
await expect(got1).resolves.toBe('hello');
await marker2;
expect(msg2.received).toBe(false);
});
it('after leaving, a client no longer receives emits for that room', async () => {
const { client: client1, serverSocket: socket1 } = await ctx.connectClient();
const { client: client2, serverSocket: socket2 } = await ctx.connectClient();
await socket1.join('room');
await socket2.join('room');
await socket2.leave('room');
const got1 = receive(client1, 'msg');
const msg2 = track(client2, 'msg');
const marker2 = receive(client2, 'marker');
ctx.io.to('room').emit('msg', 'hello');
socket2.emit('marker');
await expect(got1).resolves.toBe('hello');
await marker2;
expect(msg2.received).toBe(false);
});
it('a socket in several rooms receives the emits of each room', async () => {
const { client: client1, serverSocket: socket1 } = await ctx.connectClient();
await socket1.join('roomA');
await socket1.join('roomB');
const gotA = receive(client1, 'toA');
ctx.io.to('roomA').emit('toA', '1');
await expect(gotA).resolves.toBe('1');
const gotB = receive(client1, 'toB');
ctx.io.to('roomB').emit('toB', '2');
await expect(gotB).resolves.toBe('2');
});
it('every client in the same room receives (fan-out)', async () => {
const { client: client1, serverSocket: socket1 } = await ctx.connectClient();
const { client: client2, serverSocket: socket2 } = await ctx.connectClient();
await socket1.join('room');
await socket2.join('room');
const got1 = receive(client1, 'msg');
const got2 = receive(client2, 'msg');
ctx.io.to('room').emit('msg', 'hello');
await expect(got1).resolves.toBe('hello');
await expect(got2).resolves.toBe('hello');
});