@eleven-am/pondsocket 0.1.56 → 0.1.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/.eslintrc.json +387 -0
  2. package/dist/LICENSE +674 -0
  3. package/dist/README.md +139 -0
  4. package/dist/package.json +51 -0
  5. package/{types.d.ts → dist/types.d.ts} +0 -5
  6. package/jest.config.js +11 -0
  7. package/package.json +3 -3
  8. package/src/abstracts/abstractRequest.test.ts +49 -0
  9. package/src/abstracts/abstractRequest.ts +56 -0
  10. package/src/abstracts/abstractResponse.ts +26 -0
  11. package/src/abstracts/middleware.test.ts +75 -0
  12. package/src/abstracts/middleware.ts +50 -0
  13. package/src/channel/channel.test.ts +501 -0
  14. package/src/channel/channel.ts +305 -0
  15. package/src/channel/eventRequest.test.ts +37 -0
  16. package/src/channel/eventRequest.ts +27 -0
  17. package/src/channel/eventResponse.test.ts +249 -0
  18. package/src/channel/eventResponse.ts +172 -0
  19. package/src/client/channel.test.ts +799 -0
  20. package/src/client/channel.ts +342 -0
  21. package/src/client.ts +124 -0
  22. package/src/endpoint/endpoint.test.ts +825 -0
  23. package/src/endpoint/endpoint.ts +304 -0
  24. package/src/endpoint/response.ts +106 -0
  25. package/src/enums.ts +52 -0
  26. package/src/errors/pondError.ts +32 -0
  27. package/src/express.ts +58 -0
  28. package/src/index.ts +3 -0
  29. package/src/lobby/JoinRequest.test.ts +48 -0
  30. package/src/lobby/JoinResponse.test.ts +162 -0
  31. package/src/lobby/joinRequest.ts +32 -0
  32. package/src/lobby/joinResponse.ts +146 -0
  33. package/src/lobby/lobby.ts +182 -0
  34. package/src/matcher/matcher.test.ts +103 -0
  35. package/src/matcher/matcher.ts +105 -0
  36. package/src/node.ts +33 -0
  37. package/src/presence/presence.ts +127 -0
  38. package/src/presence/presenceEngine.test.ts +143 -0
  39. package/src/server/pondSocket.ts +153 -0
  40. package/src/subjects/subject.test.ts +163 -0
  41. package/src/subjects/subject.ts +137 -0
  42. package/src/typedefs.d.ts +451 -0
  43. package/src/types.d.ts +89 -0
  44. package/tsconfig.build.json +7 -0
  45. package/tsconfig.json +12 -0
  46. /package/{abstracts → dist/abstracts}/abstractRequest.js +0 -0
  47. /package/{abstracts → dist/abstracts}/abstractResponse.js +0 -0
  48. /package/{abstracts → dist/abstracts}/middleware.js +0 -0
  49. /package/{channel → dist/channel}/channel.js +0 -0
  50. /package/{channel → dist/channel}/eventRequest.js +0 -0
  51. /package/{channel → dist/channel}/eventResponse.js +0 -0
  52. /package/{client → dist/client}/channel.js +0 -0
  53. /package/{client.d.ts → dist/client.d.ts} +0 -0
  54. /package/{client.js → dist/client.js} +0 -0
  55. /package/{endpoint → dist/endpoint}/endpoint.js +0 -0
  56. /package/{endpoint → dist/endpoint}/response.js +0 -0
  57. /package/{enums.js → dist/enums.js} +0 -0
  58. /package/{errors → dist/errors}/pondError.js +0 -0
  59. /package/{express.d.ts → dist/express.d.ts} +0 -0
  60. /package/{express.js → dist/express.js} +0 -0
  61. /package/{index.d.ts → dist/index.d.ts} +0 -0
  62. /package/{index.js → dist/index.js} +0 -0
  63. /package/{lobby → dist/lobby}/joinRequest.js +0 -0
  64. /package/{lobby → dist/lobby}/joinResponse.js +0 -0
  65. /package/{lobby → dist/lobby}/lobby.js +0 -0
  66. /package/{matcher → dist/matcher}/matcher.js +0 -0
  67. /package/{node.d.ts → dist/node.d.ts} +0 -0
  68. /package/{node.js → dist/node.js} +0 -0
  69. /package/{presence → dist/presence}/presence.js +0 -0
  70. /package/{server → dist/server}/pondSocket.js +0 -0
  71. /package/{subjects → dist/subjects}/subject.js +0 -0
@@ -0,0 +1,799 @@
1
+ import { Channel } from './channel';
2
+ import {
3
+ ClientActions,
4
+ ChannelState,
5
+ PresenceEventTypes,
6
+ ServerActions,
7
+ Events,
8
+ ChannelReceiver,
9
+ } from '../enums';
10
+ import { SimpleBehaviorSubject, SimpleSubject } from '../subjects/subject';
11
+ // eslint-disable-next-line import/no-unresolved
12
+ import { ChannelEvent } from '../types';
13
+
14
+ const createChannel = (params?: Record<string, any>) => {
15
+ const publisher = jest.fn();
16
+ const state = new SimpleBehaviorSubject<boolean>(true);
17
+ const receiver = new SimpleSubject<ChannelEvent>();
18
+
19
+ const channel = new Channel(
20
+ publisher,
21
+ state,
22
+ 'test',
23
+ receiver,
24
+ params,
25
+ );
26
+
27
+ return {
28
+ channel,
29
+ publisher,
30
+ state,
31
+ receiver,
32
+ };
33
+ };
34
+
35
+ describe('Channel', () => {
36
+ it('should correctly send a join message', () => {
37
+ const { channel, publisher, state } = createChannel();
38
+
39
+ expect(publisher).not.toHaveBeenCalled();
40
+ expect(state.value).toBe(true);
41
+
42
+ channel.join();
43
+
44
+ expect(publisher).toHaveBeenCalledWith({
45
+ action: ClientActions.JOIN_CHANNEL,
46
+ channelName: 'test',
47
+ event: ClientActions.JOIN_CHANNEL,
48
+ payload: {},
49
+ });
50
+
51
+ const { channel: channel2, publisher: publisher2, state: state2 } = createChannel();
52
+
53
+ state2.publish(false);
54
+ expect(state2.value).toBe(false);
55
+ expect(publisher2).not.toHaveBeenCalled();
56
+
57
+ channel2.join();
58
+
59
+ expect(publisher2).not.toHaveBeenCalled();
60
+
61
+ state2.publish(true);
62
+
63
+ expect(publisher2).toHaveBeenCalledWith({
64
+ action: ClientActions.JOIN_CHANNEL,
65
+ channelName: 'test',
66
+ event: ClientActions.JOIN_CHANNEL,
67
+ payload: {},
68
+ });
69
+ const { channel: channel3 } = createChannel();
70
+
71
+ channel3.leave();
72
+
73
+ expect(() => channel3.join())
74
+ .toThrowError('This channel has been closed');
75
+ });
76
+
77
+ it('should correctly send a leave message', () => {
78
+ const { channel, publisher, receiver } = createChannel();
79
+
80
+ expect(channel.channelState).toBe(ChannelState.IDLE);
81
+
82
+ channel.join();
83
+
84
+ expect(channel.channelState).toBe(ChannelState.JOINING);
85
+
86
+ receiver.publish({
87
+ action: ServerActions.SYSTEM,
88
+ channelName: 'test',
89
+ event: 'SYSTEM',
90
+ payload: {
91
+ event: 'JOIN',
92
+ },
93
+ });
94
+
95
+ expect(channel.channelState).toBe(ChannelState.JOINING);
96
+ // once the server responds with an ack, the channel state should be joined
97
+ receiver.publish({
98
+ action: ServerActions.SYSTEM,
99
+ channelName: 'test',
100
+ event: Events.ACKNOWLEDGE,
101
+ payload: {
102
+ event: 'JOIN',
103
+ },
104
+ });
105
+
106
+ expect(channel.channelState).toBe(ChannelState.JOINED);
107
+
108
+ channel.leave();
109
+
110
+ expect(channel.channelState).toBe(ChannelState.CLOSED);
111
+
112
+ expect(publisher).toHaveBeenCalledWith({
113
+ action: ClientActions.LEAVE_CHANNEL,
114
+ channelName: 'test',
115
+ event: ClientActions.LEAVE_CHANNEL,
116
+ payload: {},
117
+ });
118
+ });
119
+
120
+ it('should notify subscribers when the channel state changes', () => {
121
+ const { channel, receiver } = createChannel();
122
+
123
+ const stateListener = jest.fn();
124
+
125
+ channel.onChannelStateChange(stateListener);
126
+
127
+ expect(stateListener).toHaveBeenCalledWith(ChannelState.IDLE);
128
+
129
+ channel.join();
130
+
131
+ receiver.publish({
132
+ action: 'SYSTEM',
133
+ channelName: 'test',
134
+ event: Events.ACKNOWLEDGE,
135
+ payload: {
136
+ event: 'JOIN',
137
+ },
138
+ });
139
+
140
+ expect(stateListener).toHaveBeenCalledWith(ChannelState.JOINED);
141
+
142
+ channel.leave();
143
+
144
+ expect(stateListener).toHaveBeenCalledWith(ChannelState.CLOSED);
145
+ });
146
+
147
+ it('should notify subscribers when state changes BOOLEAN', () => {
148
+ const { channel, receiver } = createChannel();
149
+
150
+ const stateListener = jest.fn();
151
+
152
+ channel.onConnectionChange(stateListener);
153
+
154
+ expect(stateListener).toHaveBeenCalledWith(false);
155
+
156
+ expect(channel.isConnected()).toBe(false);
157
+
158
+ channel.join();
159
+
160
+ expect(channel.isConnected()).toBe(false);
161
+
162
+ receiver.publish({
163
+ action: 'SYSTEM',
164
+ channelName: 'test',
165
+ event: Events.ACKNOWLEDGE,
166
+ payload: {
167
+ event: 'JOIN',
168
+ },
169
+ });
170
+
171
+ expect(stateListener).toHaveBeenCalledWith(true);
172
+ expect(channel.isConnected()).toBe(true);
173
+
174
+ channel.leave();
175
+
176
+ expect(stateListener).toHaveBeenCalledWith(false);
177
+ });
178
+
179
+ it('should correctly send a message', () => {
180
+ const { channel, publisher, state, receiver } = createChannel();
181
+
182
+ expect(channel.channelState).toBe(ChannelState.IDLE);
183
+ expect(publisher).not.toHaveBeenCalled();
184
+
185
+ // when the socket is connected but the channel is not joined, the message would not be queued
186
+ channel.broadcast('test', { test: false });
187
+ expect(publisher).not.toHaveBeenCalled();
188
+
189
+ channel.join();
190
+
191
+ publisher.mockClear();
192
+ receiver.publish({
193
+ action: 'SYSTEM',
194
+ channelName: 'test',
195
+ event: Events.ACKNOWLEDGE,
196
+ payload: {
197
+ event: 'JOIN',
198
+ },
199
+ });
200
+
201
+ // however once the channel is joined, the message should be sent
202
+ channel.broadcast('test', { test: true });
203
+ expect(publisher).toHaveBeenCalledWith({
204
+ action: ClientActions.BROADCAST,
205
+ addresses: ChannelReceiver.ALL_USERS,
206
+ channelName: 'test',
207
+ event: 'test',
208
+ payload: {
209
+ test: true,
210
+ },
211
+ });
212
+
213
+ // if for some reason the socket is disconnected, the message should be queued
214
+ publisher.mockClear();
215
+ state.publish(false);
216
+
217
+ expect(state.value).toBe(false);
218
+
219
+ channel.broadcast('test', { test: true });
220
+ expect(publisher).not.toHaveBeenCalled();
221
+
222
+ // once the socket is reconnected, a join message should be sent and the queued messages should be sent
223
+ publisher.mockClear();
224
+ state.publish(true);
225
+
226
+ expect(publisher).toHaveBeenCalledWith({
227
+ action: ClientActions.JOIN_CHANNEL,
228
+ channelName: 'test',
229
+ event: ClientActions.JOIN_CHANNEL,
230
+ payload: {},
231
+ });
232
+
233
+ // until it receives an ack message the queued messages should not be sent
234
+ expect(publisher).toHaveBeenCalledTimes(1);
235
+
236
+ receiver.publish({
237
+ action: 'SYSTEM',
238
+ channelName: 'test',
239
+ event: Events.ACKNOWLEDGE,
240
+ payload: {
241
+ event: 'JOIN',
242
+ },
243
+ });
244
+
245
+ expect(publisher).toHaveBeenCalledWith({
246
+ action: ClientActions.BROADCAST,
247
+ addresses: ChannelReceiver.ALL_USERS,
248
+ channelName: 'test',
249
+ event: 'test',
250
+ payload: {
251
+ test: true,
252
+ },
253
+ });
254
+
255
+ // if the channel is closed, the message should not be sent, and should not be queued
256
+ publisher.mockClear();
257
+ channel.leave();
258
+
259
+ // the leave message should be sent
260
+ expect(publisher).toHaveBeenCalledWith({
261
+ action: ClientActions.LEAVE_CHANNEL,
262
+ channelName: 'test',
263
+ event: ClientActions.LEAVE_CHANNEL,
264
+ payload: {},
265
+ });
266
+
267
+ expect(channel.channelState).toBe(ChannelState.CLOSED);
268
+ channel.broadcast('test', { test: true });
269
+ expect(publisher).toBeCalledTimes(1);
270
+ });
271
+
272
+ // The presence system tests
273
+ it('should notify subscribers when a user joins the channel', () => {
274
+ const { channel, receiver } = createChannel();
275
+
276
+ const presenceListener = jest.fn();
277
+
278
+ channel.onJoin(presenceListener);
279
+
280
+ expect(presenceListener).not.toHaveBeenCalled();
281
+
282
+ // usually the server wouldn't send a presence if the channel is not joined
283
+ // but for testing purposes, we'll just send it anyway
284
+
285
+ receiver.publish({
286
+ action: ServerActions.PRESENCE,
287
+ event: PresenceEventTypes.JOIN,
288
+ channelName: 'test',
289
+ payload: {
290
+ changed: {
291
+ id: 'test',
292
+ status: 'online',
293
+ },
294
+ presence: [
295
+ {
296
+ id: 'test',
297
+ status: 'online',
298
+ },
299
+ {
300
+ id: 'test2',
301
+ status: 'online',
302
+ },
303
+ ],
304
+ },
305
+ });
306
+
307
+ expect(presenceListener).toHaveBeenCalledWith({
308
+ id: 'test',
309
+ status: 'online',
310
+ });
311
+
312
+ presenceListener.mockClear();
313
+
314
+ receiver.publish({
315
+ action: ServerActions.PRESENCE,
316
+ event: PresenceEventTypes.UPDATE,
317
+ channelName: 'test',
318
+ payload: {
319
+ changed: {
320
+ id: 'test',
321
+ status: 'online',
322
+ },
323
+ presence: [
324
+ {
325
+ id: 'test',
326
+ status: 'online',
327
+ },
328
+ {
329
+ id: 'test2',
330
+ status: 'online',
331
+ },
332
+ ],
333
+ },
334
+ });
335
+
336
+ expect(presenceListener).not.toHaveBeenCalled();
337
+
338
+ // also, if a presence event is received for a different channel, it should not be sent to the listener
339
+ receiver.publish({
340
+ action: ServerActions.PRESENCE,
341
+ event: PresenceEventTypes.JOIN,
342
+ channelName: 'test2',
343
+ payload: {
344
+ changed: {
345
+ id: 'test',
346
+ status: 'online',
347
+ },
348
+ presence: [
349
+ {
350
+ id: 'test',
351
+ status: 'online',
352
+ },
353
+ {
354
+ id: 'test2',
355
+ status: 'online',
356
+ },
357
+ ],
358
+ },
359
+ });
360
+
361
+ expect(presenceListener).not.toHaveBeenCalled();
362
+ });
363
+
364
+ it('should notify subscribers when a user leaves the channel', () => {
365
+ const { channel, receiver } = createChannel();
366
+
367
+ const presenceListener = jest.fn();
368
+
369
+ channel.onLeave(presenceListener);
370
+
371
+ expect(presenceListener).not.toHaveBeenCalled();
372
+
373
+ // usually the server wouldn't send a presence if the channel is not joined
374
+ // but for testing purposes, we'll just send it anyway
375
+
376
+ receiver.publish({
377
+ action: ServerActions.PRESENCE,
378
+ event: PresenceEventTypes.LEAVE,
379
+ channelName: 'test',
380
+ payload: {
381
+ changed: {
382
+ id: 'test',
383
+ status: 'online',
384
+ },
385
+ presence: [
386
+ {
387
+ id: 'test',
388
+ status: 'online',
389
+ },
390
+ {
391
+ id: 'test2',
392
+ status: 'online',
393
+ },
394
+ ],
395
+ },
396
+ });
397
+
398
+ expect(presenceListener).toHaveBeenCalledWith({
399
+ id: 'test',
400
+ status: 'online',
401
+ });
402
+
403
+ presenceListener.mockClear();
404
+
405
+ receiver.publish({
406
+ action: ServerActions.PRESENCE,
407
+ event: PresenceEventTypes.UPDATE,
408
+ channelName: 'test',
409
+ payload: {
410
+ changed: {
411
+ id: 'test',
412
+ status: 'online',
413
+ },
414
+ presence: [
415
+ {
416
+ id: 'test',
417
+ status: 'online',
418
+ },
419
+ {
420
+ id: 'test2',
421
+ status: 'online',
422
+ },
423
+ ],
424
+ },
425
+ });
426
+
427
+ expect(presenceListener).not.toHaveBeenCalled();
428
+
429
+ // also, if a presence event is received for a different channel, it should not be sent to the listener
430
+ receiver.publish({
431
+ action: ServerActions.PRESENCE,
432
+ event: PresenceEventTypes.LEAVE,
433
+ channelName: 'test2',
434
+ payload: {
435
+ changed: {
436
+ id: 'test',
437
+ status: 'online',
438
+ },
439
+ presence: [
440
+ {
441
+ id: 'test',
442
+ status: 'online',
443
+ },
444
+ {
445
+ id: 'test2',
446
+ status: 'online',
447
+ },
448
+ ],
449
+ },
450
+ });
451
+
452
+ expect(presenceListener).not.toHaveBeenCalled();
453
+ });
454
+
455
+ it('should notify subscribers when a user updates their presence', () => {
456
+ const { channel, receiver } = createChannel();
457
+
458
+ const presenceListener = jest.fn();
459
+
460
+ channel.onUsersChange(presenceListener);
461
+
462
+ expect(presenceListener).not.toHaveBeenCalled();
463
+
464
+ // usually the server wouldn't send a presence if the channel is not joined
465
+ // but for testing purposes, we'll just send it anyway
466
+
467
+ receiver.publish({
468
+ action: ServerActions.PRESENCE,
469
+ event: PresenceEventTypes.UPDATE,
470
+ channelName: 'test',
471
+ payload: {
472
+ changed: {
473
+ id: 'test',
474
+ status: 'online',
475
+ },
476
+ presence: [
477
+ {
478
+ id: 'test',
479
+ status: 'online',
480
+ },
481
+ {
482
+ id: 'test2',
483
+ status: 'online',
484
+ },
485
+ ],
486
+ },
487
+ });
488
+
489
+ expect(presenceListener).toHaveBeenCalledWith([
490
+ {
491
+ id: 'test',
492
+ status: 'online',
493
+ },
494
+ {
495
+ id: 'test2',
496
+ status: 'online',
497
+ },
498
+ ]);
499
+
500
+ expect(channel.getPresence()).toBe(presenceListener.mock.calls[0][0]);
501
+
502
+ presenceListener.mockClear();
503
+
504
+ // if we receive a leave or join event, it should be sent to the listener
505
+ // this is because we are listening for all presence events
506
+ receiver.publish({
507
+ action: ServerActions.PRESENCE,
508
+ event: PresenceEventTypes.JOIN,
509
+ channelName: 'test',
510
+ payload: {
511
+ changed: {
512
+ id: 'test',
513
+ status: 'online',
514
+ },
515
+ presence: [
516
+ {
517
+ id: 'test',
518
+ status: 'online',
519
+ },
520
+ ],
521
+ },
522
+ });
523
+
524
+ expect(presenceListener).toHaveBeenCalledWith([
525
+ {
526
+ id: 'test',
527
+ status: 'online',
528
+ },
529
+ ]);
530
+
531
+ expect(channel.getPresence()).toBe(presenceListener.mock.calls[0][0]);
532
+
533
+ presenceListener.mockClear();
534
+
535
+ // also, if a presence event is received for a different channel, it should not be sent to the listener
536
+ receiver.publish({
537
+ action: ServerActions.PRESENCE,
538
+ event: PresenceEventTypes.UPDATE,
539
+ channelName: 'test2',
540
+ payload: {
541
+ changed: {
542
+ id: 'test',
543
+ status: 'online',
544
+ },
545
+ presence: [
546
+ {
547
+ id: 'test',
548
+ status: 'online',
549
+ },
550
+ {
551
+ id: 'test2',
552
+ status: 'online',
553
+ },
554
+ ],
555
+ },
556
+ });
557
+
558
+ expect(presenceListener).not.toHaveBeenCalled();
559
+
560
+ // we once again send a presence event for the same channel, but this time it should be sent to the listener
561
+ receiver.publish({
562
+ action: ServerActions.PRESENCE,
563
+ event: PresenceEventTypes.UPDATE,
564
+ channelName: 'test',
565
+ payload: {
566
+ changed: {
567
+ id: 'test',
568
+ status: 'away',
569
+ },
570
+ presence: [
571
+ {
572
+ id: 'test',
573
+ status: 'away',
574
+ },
575
+ {
576
+ id: 'test2',
577
+ status: 'online',
578
+ },
579
+ ],
580
+ },
581
+ });
582
+
583
+ expect(presenceListener).toHaveBeenCalledWith([
584
+ {
585
+ id: 'test',
586
+ status: 'away',
587
+ },
588
+ {
589
+ id: 'test2',
590
+ status: 'online',
591
+ },
592
+ ]);
593
+
594
+ expect(channel.getPresence()).toBe(presenceListener.mock.calls[0][0]);
595
+ });
596
+
597
+ it('should notify subscribers when any presence event occurs', () => {
598
+ const { channel, receiver } = createChannel();
599
+
600
+ const presenceListener = jest.fn();
601
+
602
+ channel.onPresenceChange(presenceListener);
603
+
604
+ expect(presenceListener).not.toHaveBeenCalled();
605
+
606
+ // usually the server wouldn't send a presence if the channel is not joined
607
+ // but for testing purposes, we'll just send it anyway
608
+
609
+ receiver.publish({
610
+ action: ServerActions.PRESENCE,
611
+ event: PresenceEventTypes.UPDATE,
612
+ channelName: 'test',
613
+ payload: {
614
+ changed: {
615
+ id: 'test',
616
+ status: 'online',
617
+ },
618
+ presence: [
619
+ {
620
+ id: 'test',
621
+ status: 'online',
622
+ },
623
+ {
624
+ id: 'test2',
625
+ status: 'online',
626
+ },
627
+ ],
628
+ },
629
+ });
630
+
631
+ expect(presenceListener).toHaveBeenCalledWith({
632
+ changed: {
633
+ id: 'test',
634
+ status: 'online',
635
+ },
636
+ presence: [
637
+ {
638
+ id: 'test',
639
+ status: 'online',
640
+ },
641
+ {
642
+ id: 'test2',
643
+ status: 'online',
644
+ },
645
+ ],
646
+ });
647
+ });
648
+
649
+ // message events
650
+
651
+ it('should notify subscribers when a message is received', () => {
652
+ const { channel, receiver } = createChannel();
653
+
654
+ const messageListener = jest.fn();
655
+
656
+ channel.onMessage(messageListener);
657
+
658
+ expect(messageListener).not.toHaveBeenCalled();
659
+
660
+ receiver.publish({
661
+ action: ServerActions.BROADCAST,
662
+ event: 'message',
663
+ channelName: 'test',
664
+ payload: {
665
+ id: 'test',
666
+ message: 'test',
667
+ },
668
+ });
669
+
670
+ expect(messageListener).toHaveBeenCalledWith('message', {
671
+ id: 'test',
672
+ message: 'test',
673
+ });
674
+
675
+ // if a message event is received for a different channel, it should not be sent to the listener
676
+ receiver.publish({
677
+ action: ServerActions.BROADCAST,
678
+ event: 'message',
679
+ channelName: 'test2',
680
+ payload: {
681
+ id: 'test',
682
+ message: 'test',
683
+ },
684
+ });
685
+
686
+ expect(messageListener).toHaveBeenCalledTimes(1);
687
+
688
+ // we can also subscribe to a specific message event
689
+ const specificMessageListener = jest.fn();
690
+
691
+ channel.onMessageEvent('test', specificMessageListener);
692
+
693
+ expect(specificMessageListener).not.toHaveBeenCalled();
694
+
695
+ receiver.publish({
696
+ action: ServerActions.BROADCAST,
697
+ event: 'test',
698
+ channelName: 'test',
699
+ payload: {
700
+ id: 'test',
701
+ message: 'test',
702
+ },
703
+ });
704
+
705
+ expect(specificMessageListener).toHaveBeenCalledWith({
706
+ id: 'test',
707
+ message: 'test',
708
+ });
709
+
710
+ specificMessageListener.mockClear();
711
+ // if a message event is received for the same channel, but a different event, it should not be sent to the listener
712
+ receiver.publish({
713
+ action: ServerActions.BROADCAST,
714
+ event: 'test2',
715
+ channelName: 'test',
716
+ payload: {
717
+ id: 'test',
718
+ message: 'test',
719
+ },
720
+ });
721
+
722
+ expect(specificMessageListener).not.toHaveBeenCalled();
723
+
724
+ // if a message event is received for a different channel, it should not be sent to the listener
725
+ receiver.publish({
726
+ action: ServerActions.BROADCAST,
727
+ event: 'test',
728
+ channelName: 'test2',
729
+ payload: {
730
+ id: 'test',
731
+ message: 'test',
732
+ },
733
+ });
734
+
735
+ expect(specificMessageListener).not.toHaveBeenCalled();
736
+ });
737
+
738
+ it('should be able to broadcast a message', () => {
739
+ const { channel, receiver, publisher } = createChannel();
740
+
741
+ channel.join();
742
+
743
+ publisher.mockClear();
744
+ receiver.publish({
745
+ action: 'SYSTEM',
746
+ channelName: 'test',
747
+ event: Events.ACKNOWLEDGE,
748
+ payload: {
749
+ event: 'JOIN',
750
+ },
751
+ });
752
+
753
+ channel.sendMessage('test', {
754
+ test: 'test',
755
+ }, ['test1']);
756
+
757
+ expect(publisher).toHaveBeenCalledWith({
758
+ action: ClientActions.BROADCAST,
759
+ channelName: 'test',
760
+ event: 'test',
761
+ payload: {
762
+ test: 'test',
763
+ },
764
+ addresses: ['test1'],
765
+ });
766
+
767
+ publisher.mockClear();
768
+
769
+ channel.broadcastFrom('test', {
770
+ test: 'test',
771
+ });
772
+
773
+ expect(publisher).toHaveBeenCalledWith({
774
+ action: ClientActions.BROADCAST,
775
+ channelName: 'test',
776
+ event: 'test',
777
+ payload: {
778
+ test: 'test',
779
+ },
780
+ addresses: ChannelReceiver.ALL_EXCEPT_SENDER,
781
+ });
782
+
783
+ publisher.mockClear();
784
+
785
+ channel.broadcast('test', {
786
+ test: 'test',
787
+ });
788
+
789
+ expect(publisher).toHaveBeenCalledWith({
790
+ action: ClientActions.BROADCAST,
791
+ channelName: 'test',
792
+ event: 'test',
793
+ payload: {
794
+ test: 'test',
795
+ },
796
+ addresses: ChannelReceiver.ALL_USERS,
797
+ });
798
+ });
799
+ });