forked from Cylo-Traders/Agrocylo-PIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.e2e-spec.ts
More file actions
85 lines (72 loc) · 2.41 KB
/
Copy pathwebsocket.e2e-spec.ts
File metadata and controls
85 lines (72 loc) · 2.41 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
80
81
82
83
84
85
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { io, Socket } from 'socket.io-client';
import { AppModule } from './../src/app.module';
import { configureApp } from './../src/setup-app';
import { RealtimeEventsService } from '../src/websocket/realtime-events.service';
import {
CAMPAIGN_EVENT,
SUBSCRIBE_CAMPAIGN,
} from '../src/websocket/events.types';
describe('CampaignEventsGateway (e2e)', () => {
let app: INestApplication;
let client: Socket;
let url: string;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
configureApp(app);
await app.listen(0);
const address = app.getHttpServer().address();
url = `http://localhost:${address.port}`;
});
afterAll(async () => {
await app.close();
});
afterEach(() => {
client?.close();
});
it('connects, joins a campaign room, and receives a broadcast event for a simulated persisted event', (done) => {
client = io(url, { transports: ['websocket'], forceNew: true });
client.on('connect', () => {
client.emit(SUBSCRIBE_CAMPAIGN, '123');
// Give the join a tick to land before the "persist" fires.
setTimeout(() => {
const realtimeEvents = app.get(RealtimeEventsService);
realtimeEvents.emitCampaignEvent({
type: 'campaign.invested',
campaignId: '123',
data: { amount: '250' },
});
}, 50);
});
client.on(CAMPAIGN_EVENT, (payload) => {
expect(payload).toEqual({
type: 'campaign.invested',
campaignId: '123',
data: { amount: '250' },
});
done();
});
}, 10000);
it('does not deliver events for rooms the client never joined', (done) => {
client = io(url, { transports: ['websocket'], forceNew: true });
const received: unknown[] = [];
client.on('connect', () => {
// Deliberately not subscribing to any room.
const realtimeEvents = app.get(RealtimeEventsService);
realtimeEvents.emitCampaignEvent({
type: 'campaign.funded',
campaignId: '999',
data: {},
});
setTimeout(() => {
expect(received).toHaveLength(0);
done();
}, 200);
});
client.on(CAMPAIGN_EVENT, (payload) => received.push(payload));
}, 10000);
});