-
Notifications
You must be signed in to change notification settings - Fork 0
/
PubSub.test.ts
373 lines (294 loc) · 12.6 KB
/
PubSub.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import { PubSub } from './index';
describe('PubSub', () => {
it(`should properly subscribe to given event`, () => {
const pubSub = new PubSub({events: {testEvent: ''}});
const eventCallback1 = jest.fn();
const eventCallback2 = jest.fn();
const eventCallback3 = jest.fn();
pubSub.subscribe('testEvent', eventCallback1);
pubSub.subscribe('testEvent', eventCallback2);
pubSub.subscribe('testEvent', eventCallback3);
pubSub.publish('testEvent');
pubSub.publish('testEvent');
pubSub.publish('testEvent');
expect(eventCallback1).toBeCalledTimes(3);
expect(eventCallback2).toBeCalledTimes(3);
expect(eventCallback3).toBeCalledTimes(3);
pubSub.publish('testEvent');
pubSub.publish('testEvent');
pubSub.publish('testEvent');
expect(eventCallback1).toBeCalledTimes(6);
expect(eventCallback2).toBeCalledTimes(6);
expect(eventCallback3).toBeCalledTimes(6);
});
it(`should check if data that is being published
is the same for all subscribers`, () => {
const pubSub = new PubSub({events: {testEvent: 2}});
const eventCallback1 = jest.fn();
const eventCallback2 = jest.fn();
const eventCallback3 = jest.fn();
pubSub.subscribe('testEvent', eventCallback1);
pubSub.subscribe('testEvent', eventCallback2);
pubSub.subscribe('testEvent', eventCallback3);
pubSub.publish('testEvent', 2);
expect(eventCallback1).toBeCalledWith(2);
expect(eventCallback2).toBeCalledWith(2);
expect(eventCallback3).toBeCalledWith(2);
pubSub.publish('testEvent', 3);
});
it('should allow to unsubscribe given subscribe call', () => {
const pubSub = new PubSub({events: {testEvent: ''}});
const eventCallback1 = jest.fn();
const eventCallback2 = jest.fn();
const eventCallback3 = jest.fn();
const unsubscribeEvent1 = pubSub.subscribe('testEvent', eventCallback1);
pubSub.subscribe('testEvent', eventCallback2);
pubSub.subscribe('testEvent', eventCallback3);
pubSub.publish('testEvent');
pubSub.publish('testEvent');
pubSub.publish('testEvent');
expect(eventCallback1).toBeCalledTimes(3);
expect(eventCallback2).toBeCalledTimes(3);
expect(eventCallback3).toBeCalledTimes(3);
unsubscribeEvent1();
pubSub.publish('testEvent');
pubSub.publish('testEvent');
pubSub.publish('testEvent');
expect(eventCallback1).toBeCalledTimes(3);
expect(eventCallback2).toBeCalledTimes(6);
expect(eventCallback3).toBeCalledTimes(6);
});
it('should subscribe for one event only and remove listener afterwards', () => {
const pubSub = new PubSub({events: {testEvent: ''}});
const eventCallback1 = jest.fn();
const eventCallback2 = jest.fn();
const eventCallback3 = jest.fn();
pubSub.subscribeForOnePublishOnly('testEvent', eventCallback1);
pubSub.subscribe('testEvent', eventCallback2);
pubSub.subscribe('testEvent', eventCallback3);
pubSub.publish('testEvent');
pubSub.publish('testEvent');
pubSub.publish('testEvent');
expect(eventCallback1).toBeCalledTimes(1);
expect(eventCallback2).toBeCalledTimes(3);
expect(eventCallback3).toBeCalledTimes(3);
pubSub.publish('testEvent');
pubSub.publish('testEvent');
pubSub.publish('testEvent');
expect(eventCallback1).toBeCalledTimes(1);
expect(eventCallback2).toBeCalledTimes(6);
expect(eventCallback3).toBeCalledTimes(6);
// @ts-ignore
expect(pubSub['subscribers']['testEvent']['1']).not.toBeDefined();
// @ts-ignore
expect(pubSub['subscribers']['testEvent']['2']).toBeDefined();
// @ts-ignore
expect(pubSub['subscribers']['testEvent']['3']).toBeDefined();
});
it('should allow user to clear all currently subscribed listeners', () => {
const NO_LISTENERS_FOUND_MSG = 'No listeners found for eventName: testEvent';
const pubSub = new PubSub({events: {testEvent: ''}, enableLogs: true });
console.log = jest.fn();
const eventCallback1 = jest.fn();
const eventCallback2 = jest.fn();
const eventCallback3 = jest.fn();
pubSub.subscribe('testEvent', eventCallback1);
pubSub.subscribe('testEvent', eventCallback2);
pubSub.subscribe('testEvent', eventCallback3);
pubSub.publish('testEvent');
pubSub.publish('testEvent');
pubSub.publish('testEvent');
expect(eventCallback1).toBeCalledTimes(3);
expect(eventCallback2).toBeCalledTimes(3);
expect(eventCallback3).toBeCalledTimes(3);
pubSub.clearAllSubscribers();
pubSub.publish('testEvent');
pubSub.publish('testEvent');
pubSub.publish('testEvent');
expect(eventCallback1).toBeCalledTimes(3);
expect(eventCallback2).toBeCalledTimes(3);
expect(eventCallback3).toBeCalledTimes(3);
expect(pubSub['subscribers']).toStrictEqual({});
expect(console.log).toBeCalledTimes(3);
expect(console.log).toHaveBeenCalledWith(NO_LISTENERS_FOUND_MSG);
});
it('should allow user to clear specific subscribers', () => {
const NO_LISTENERS_FOUND_MSG = 'No listeners found for eventName: testEvent2';
const pubSub = new PubSub({
events: {
testEvent: "",
testEvent2: "",
},
enableLogs: true,
});
console.log = jest.fn();
const eventCallback1 = jest.fn();
const eventCallback2 = jest.fn();
const eventCallback3 = jest.fn();
pubSub.subscribe('testEvent', eventCallback1);
pubSub.subscribe('testEvent2', eventCallback2);
pubSub.subscribe('testEvent2', eventCallback3);
pubSub.publish('testEvent');
pubSub.publish('testEvent2');
pubSub.publish('testEvent2');
expect(eventCallback1).toBeCalledTimes(1);
expect(eventCallback2).toBeCalledTimes(2);
expect(eventCallback3).toBeCalledTimes(2);
pubSub.clearAllSubscribersFromEvent('testEvent2');
pubSub.publish('testEvent');
pubSub.publish('testEvent2');
pubSub.publish('testEvent2');
expect(eventCallback1).toBeCalledTimes(2);
expect(eventCallback2).toBeCalledTimes(2);
expect(eventCallback3).toBeCalledTimes(2);
// @ts-ignore
expect(pubSub['subscribers']['testEvent2']).toStrictEqual(undefined);
expect(console.log).toBeCalledTimes(2);
expect(console.log).toHaveBeenCalledWith(NO_LISTENERS_FOUND_MSG);
});
it('should allow user to check if event has any active listeners', () => {
const pubSub = new PubSub({
events: {
testEvent: () => {},
testEvent2: () => {},
},
});
const unsubscribeTestEvent1 = pubSub.subscribe('testEvent', () => {});
pubSub.subscribe('testEvent2', () => {});
unsubscribeTestEvent1();
expect(pubSub.hasSubscribers('testEvent')).toBe(false);
expect(pubSub.hasSubscribers('testEvent2')).toBe(true);
});
it('should allow user to publish events asynchronously and NOT await for all subscribers to finish before returning the status and resolving Promise', async () => {
console.log = jest.fn();
const pubSub = new PubSub({
events: {
testEvent1: '',
testEvent2: '',
},
});
const promiseToResolve = (callback: () => void) => new Promise((resolve) => {
setTimeout(() => {
callback();
resolve(true);
}, Infinity)
});
pubSub.subscribe('testEvent1', () => promiseToResolve(() => console.log('testEvent1')));
pubSub.subscribe('testEvent2', () => promiseToResolve(() => console.log('testEvent2')));
const hasPublishedTestEvent1 = await pubSub.publishAsync('testEvent1', 'some data', {
awaitAllSubscribersFinish: false
});
expect(hasPublishedTestEvent1).toBe(true);
// @ts-ignore - intentional check if it works in runtime as well
const hasPublishedNonValidEvent = await pubSub.publishAsync('nonValidEvent', 'some data', {
awaitAllSubscribersFinish: false
});
expect(hasPublishedNonValidEvent).toBe(false);
expect(console.log).not.toBeCalled();
const hasPublishedTestEvent2 = await pubSub.publishAsync('testEvent2', 'some data', {
awaitAllSubscribersFinish: false
});
expect(hasPublishedTestEvent2).toBe(true);
expect(console.log).not.toBeCalledWith('testEvent2');
expect(console.log).not.toBeCalledTimes(2);
});
it('should allow user to publish events asynchronously and await for all subscribers to finish before returning the status and resolving Promise', async () => {
console.log = jest.fn();
const pubSub = new PubSub({
events: {
testEvent1: '',
testEvent2: '',
},
});
const promiseToResolve = (callback: () => void) => new Promise((resolve) => {
setTimeout(() => {
callback();
resolve(true);
}, 1000)
});
pubSub.subscribe('testEvent1', () => promiseToResolve(() => console.log('testEvent1')));
pubSub.subscribe('testEvent2', () => promiseToResolve(() => console.log('testEvent2')));
const hasPublishedTestEvent1 = await pubSub.publishAsync('testEvent1', 'some data');
expect(hasPublishedTestEvent1).toBe(true);
// @ts-ignore - intentional check if it works in runtime as well
const hasPublishedNonValidEvent = await pubSub.publishAsync('nonValidEvent', 'some data');
expect(hasPublishedNonValidEvent).toBe(false);
expect(console.log).toBeCalled();
expect(console.log).toBeCalledWith('testEvent1');
const hasPublishedTestEvent2 = await pubSub.publishAsync('testEvent2', 'some data', {
awaitAllSubscribersFinish: false
});
expect(hasPublishedTestEvent2).toBe(true);
expect(console.log).toBeCalledWith('testEvent2');
expect(console.log).toBeCalledTimes(3);
});
it('should allow user to count how many subscribers specific event has', () => {
const pubSub = new PubSub({
events: {
testEvent1: '',
testEvent2: '',
},
});
const unsubscribeTestEvent1 = pubSub.subscribe('testEvent1', () => {});
pubSub.subscribe('testEvent1', () => {});
pubSub.subscribe('testEvent1', () => {});
pubSub.subscribe('testEvent1', () => {});
pubSub.subscribe('testEvent1', () => {});
pubSub.subscribe('testEvent1', () => {});
const unsubscribeTestEvent2 = pubSub.subscribe('testEvent2', () => {});
expect(pubSub.countSubscribers('testEvent1')).toBe(6);
expect(pubSub.countSubscribers('testEvent2')).toBe(1);
unsubscribeTestEvent2();
unsubscribeTestEvent1();
expect(pubSub.countSubscribers('testEvent1')).toBe(5);
expect(pubSub.countSubscribers('testEvent2')).toBe(0);
});
it('should throw error while trying to log history without enabling keepHistory first', () => {
console.log = jest.fn();
console.error = jest.fn();
const pubSub = new PubSub({
events: {
testEvent1: '',
},
});
pubSub.subscribe('testEvent1', () => console.log('testEvent1'));
pubSub.subscribe('testEvent1', () => console.log('testEvent1 #2'));
pubSub.publish('testEvent1', '');
pubSub.publish('testEvent1', '');
pubSub.logHistory();
expect(pubSub['history']).toStrictEqual([]);
expect(console.error).toBeCalled();
expect(console.error).toBeCalledWith(`logHistory() will log empty array, because keepHistory param was not enabled while instantiating PubSub. Enable keepHistory first`);
});
it('should allow user to keep history of all publishes and subscriptions and let user be able to logHistory()', async () => {
console.log = jest.fn();
console.error = jest.fn();
const pubSub = new PubSub({
events: {
testEvent1: {something: ''},
testEvent2: {something: ''},
},
keepHistory: true
});
const unsubscribeTestEvent1 = pubSub.subscribe('testEvent1', () => console.log('testEvent1'));
pubSub.subscribe('testEvent2', () => console.log('testEvent2'));
pubSub.publish('testEvent1', {something: 'testEvent1'});
pubSub.publish('testEvent2', {something: 'testEvent2'});
unsubscribeTestEvent1();
await pubSub.publishAsync('testEvent2', {something: 'testEvent2'});
expect(pubSub['history']).toStrictEqual([
{ message: 'subscribed for event: testEvent1' },
{ message: 'subscribed for event: testEvent2' },
{ message: 'testEvent1 event published with data:', data: {something: 'testEvent1'}},
{ message: 'testEvent2 event published with data:', data: {something: 'testEvent2'}},
{ message: 'Unsubscribed from event: testEvent1'},
{ message: 'testEvent2 event published asynchronously with data:', data: {something: 'testEvent2'}},
]);
expect(console.log).toBeCalled();
expect(console.error).not.toBeCalled();
expect(console.error).not.toBeCalledWith(`logHistory() will log empty array, because keepHistory param was not enabled while instantiating PubSub. Enable keepHistory first`);
console.clear();
pubSub.logHistory();
expect(console.log).toBeCalledWith(pubSub['history']);
});
});