-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
325 lines (286 loc) · 11 KB
/
index.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
import { describe, expect, it, vi } from 'vitest';
import { createEmitter, defineHandler, defineHandlers } from './index'; // Adjust the import path as needed
describe('Event Emitter Middleware', () => {
describe('createEmitter', () => {
it('should create an emitter with initial handlers', () => {
type EventPayloadMap = {
test: { id: string; text: string };
};
const handlers = {
test: [vi.fn()],
};
const ee = createEmitter<EventPayloadMap>(handlers);
expect(ee).toBeDefined();
expect(ee.emit).toBeDefined();
expect(ee.on).toBeDefined();
expect(ee.off).toBeDefined();
expect(ee.emitAsync).toBeDefined();
});
it('should create an emitter without initial handlers', () => {
const ee = createEmitter();
expect(ee).toBeDefined();
});
it('should allow adding and removing handlers', () => {
type EventPayloadMap = {
test: string;
};
const ee = createEmitter<EventPayloadMap>();
const handler = vi.fn();
ee.on('test', handler);
ee.emit('test', 'payload');
expect(handler).toHaveBeenCalledWith('payload');
ee.off('test', handler);
ee.emit('test', 'payload');
expect(handler).toHaveBeenCalledTimes(1);
});
it('should remove all handlers for an event when no handler is specified', () => {
type EventPayloadMap = {
test: string;
};
const ee = createEmitter<EventPayloadMap>();
const handler1 = vi.fn();
const handler2 = vi.fn();
ee.on('test', handler1);
ee.on('test', handler2);
ee.off('test');
ee.emit('test', 'payload');
expect(handler1).not.toHaveBeenCalled();
expect(handler2).not.toHaveBeenCalled();
});
it('should emit events to all registered handlers', () => {
type EventPayloadMap = {
test: string;
};
const ee = createEmitter<EventPayloadMap>();
const handler1 = vi.fn();
const handler2 = vi.fn();
ee.on('test', handler1);
ee.on('test', handler2);
ee.emit('test', 'payload');
expect(handler1).toHaveBeenCalledWith('payload');
expect(handler2).toHaveBeenCalledWith('payload');
});
it('should not add the same named function handler multiple times', () => {
type EventPayloadMap = {
test: string;
};
const ee = createEmitter<EventPayloadMap>();
const handler = vi.fn();
ee.on('test', handler);
ee.on('test', handler);
ee.emit('test', 'payload');
expect(handler).toHaveBeenCalledTimes(1);
});
it('should emit async events concurrently', async () => {
type EventPayloadMap = {
test: { id: string };
};
const ee = createEmitter<EventPayloadMap>();
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const handler1 = vi.fn(
defineHandler<EventPayloadMap, 'test'>(async (_payload) => {
await delay(100);
}),
);
const handler2 = vi.fn(
defineHandler<EventPayloadMap, 'test'>(async (_payload) => {
await delay(100);
}),
);
ee.on('test', handler1);
ee.on('test', handler2);
const start = Date.now();
await ee.emitAsync('test', { id: '123' }, { mode: 'concurrent' });
const end = Date.now();
// The total time should be close to 100ms (since handlers run concurrently)
// We'll allow a small margin for execution time
expect(end - start).toBeLessThan(150);
expect(handler1).toHaveBeenCalledWith({ id: '123' });
expect(handler2).toHaveBeenCalledWith({ id: '123' });
expect(handler1).toHaveBeenCalledTimes(1);
expect(handler2).toHaveBeenCalledTimes(1);
});
it('should emit async events sequentially', async () => {
type EventPayloadMap = {
test: { id: string };
};
const ee = createEmitter<EventPayloadMap>();
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const handler1 = vi.fn(
defineHandler<EventPayloadMap, 'test'>(async (_payload) => {
await delay(100);
}),
);
const handler2 = vi.fn(
defineHandler<EventPayloadMap, 'test'>(async (_payload) => {
await delay(100);
}),
);
ee.on('test', handler1);
ee.on('test', handler2);
const start = Date.now();
await ee.emitAsync('test', { id: '123' }, { mode: 'sequencial' });
const end = Date.now();
// The total time should be close to 200ms (since handlers run sequentially)
// We'll allow a small margin for execution time
expect(end - start).toBeGreaterThanOrEqual(200);
expect(end - start).toBeLessThan(250);
expect(handler1).toHaveBeenCalledWith({ id: '123' });
expect(handler2).toHaveBeenCalledWith({ id: '123' });
expect(handler1).toHaveBeenCalledTimes(1);
expect(handler2).toHaveBeenCalledTimes(1);
});
it('should throw AggregateError when async handlers fail using emitAsync with concurent mode', async () => {
type EventPayloadMap = {
test: string;
};
const ee = createEmitter<EventPayloadMap>();
const handler1 = vi.fn().mockRejectedValue(new Error('Error 1'));
const handler2 = vi.fn().mockRejectedValue(new Error('Error 2'));
ee.on('test', handler1);
ee.on('test', handler2);
await expect(ee.emitAsync('test', 'payload')).rejects.toThrow(AggregateError);
try {
await ee.emitAsync('test', 'payload', { mode: 'concurrent' });
// Should not reach here
expect(true).toBe(false);
} catch (error) {
expect((error as AggregateError).errors).toHaveLength(2);
expect((error as AggregateError).errors[0].message).toBe('Error 1');
expect((error as AggregateError).errors[1].message).toBe('Error 2');
}
});
it('should stop execution on first error in async handlers fail using emitAsync with sequential mode', async () => {
type EventPayloadMap = {
test: { id: string };
};
const ee = createEmitter<EventPayloadMap>();
const handler1 = vi.fn(
defineHandler<EventPayloadMap, 'test'>(async () => {
throw new Error('Error 1');
}),
);
const handler2 = vi.fn(
defineHandler<EventPayloadMap, 'test'>(async () => {
// This should not be called
}),
);
ee.on('test', handler1);
ee.on('test', handler2);
try {
await ee.emitAsync('test', { id: '789' }, { mode: 'sequencial' });
// Should not reach here
expect(true).toBe(false);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toBe('Error 1');
}
expect(handler1).toHaveBeenCalledWith({ id: '789' });
expect(handler1).toHaveBeenCalledTimes(1);
expect(handler2).not.toHaveBeenCalled();
});
it('should throw TypeError when adding a non-function handler', () => {
type EventPayloadMap = {
test: string;
};
const ee = createEmitter<EventPayloadMap>();
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
expect(() => ee.on('test', 'not a function' as any)).toThrow(TypeError);
});
it('should throw RangeError when max handlers limit is reached', () => {
type EventPayloadMap = { test: string };
const emitter = createEmitter<EventPayloadMap>({}, { maxHandlers: 3 });
emitter.on('test', vi.fn());
emitter.on('test', vi.fn());
emitter.on('test', vi.fn());
expect(() => emitter.on('test', vi.fn())).toThrow(RangeError);
});
it('should use default max handlers limit of 10 when not specified', () => {
type EventPayloadMap = { testEvent: string };
const emitter = createEmitter<EventPayloadMap>();
for (let i = 0; i < 10; i++) {
emitter.on('testEvent', vi.fn());
}
expect(() => emitter.on('testEvent', vi.fn())).toThrow(RangeError);
});
it('should allow different events to have their own handler counts', () => {
type EventPayloadMap = { test1: string; test2: string };
const emitter = createEmitter<EventPayloadMap>({}, { maxHandlers: 2 });
emitter.on('test1', vi.fn());
emitter.on('test1', vi.fn());
emitter.on('test2', vi.fn());
expect(() => emitter.on('test1', vi.fn())).toThrow(RangeError);
expect(() => emitter.on('test2', vi.fn())).not.toThrow();
});
it('should include event key in error message when limit is reached', () => {
type EventPayloadMap = { specificEvent: string };
const emitter = createEmitter<EventPayloadMap>({}, { maxHandlers: 1 });
emitter.on('specificEvent', vi.fn());
expect(() => emitter.on('specificEvent', vi.fn())).toThrow(/specificEvent/);
});
it('should allow setting custom max handlers limit', () => {
type EventPayloadMap = { test: string };
const emitter = createEmitter<EventPayloadMap>({}, { maxHandlers: 5 });
for (let i = 0; i < 5; i++) {
emitter.on('test', vi.fn());
}
expect(() => emitter.on('test', vi.fn())).toThrow(RangeError);
});
it('should do nothing when emitting an event with no handlers', () => {
type EventPayloadMap = {
test: string;
};
const ee = createEmitter<EventPayloadMap>();
expect(() => ee.emit('test', 'payload')).not.toThrow();
});
it('should do nothing when emitting an async event with no handlers', async () => {
type EventPayloadMap = {
test: string;
};
const ee = createEmitter<EventPayloadMap>();
await expect(ee.emitAsync('test', 'payload')).resolves.toBeUndefined();
});
});
describe('defineHandler', () => {
it('should return the provided handler', () => {
type EventPayloadMap = {
test: number;
};
const handler = (_payload: number) => {};
const definedHandler = defineHandler<EventPayloadMap, 'test'>(handler);
expect(definedHandler).toBe(handler);
});
});
describe('defineHandlers', () => {
it('should return the provided handlers object', () => {
const handlers = {
test: [(_payload: number) => {}],
};
const definedHandlers = defineHandlers(handlers);
expect(definedHandlers).toBe(handlers);
});
});
describe('type safety', () => {
it('should enforce correct types for event payloads', () => {
type EventPayloadMap = {
numberEvent: number;
objectEvent: { id: string };
};
const ee = createEmitter<EventPayloadMap>();
// These should compile without errors
ee.on('numberEvent', (payload) => {
const _num: number = payload;
});
ee.on('objectEvent', (payload) => {
const _id: string = payload.id;
});
// @ts-expect-error - payload should be a number
ee.emit('numberEvent', 'not a number');
// @ts-expect-error - payload should be an object with an id property
ee.emit('objectEvent', { wrongKey: 'value' });
// These should compile without errors
ee.emit('numberEvent', 42);
ee.emit('objectEvent', { id: 'test' });
});
});
});