-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.test.ts
79 lines (67 loc) · 2.22 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
import SafeEventEmitter from '.';
describe('SafeEventEmitter', () => {
it('can be constructed without error', () => {
expect(new SafeEventEmitter()).toBeDefined();
});
it('can emit a value with no listeners', () => {
const see = new SafeEventEmitter();
expect(see.emit('foo', 42)).toBe(false);
});
it('can emit a value with 1 listeners', () => {
expect.assertions(2);
const see = new SafeEventEmitter();
see.on('foo', (x) => expect(x).toBe(42));
expect(see.emit('foo', 42)).toBe(true);
});
it('can emit a value with 2 listeners', () => {
expect.assertions(3);
const see = new SafeEventEmitter();
see.on('foo', (x) => expect(x).toBe(42));
see.on('foo', (x) => expect(x).toBe(42));
expect(see.emit('foo', 42)).toBe(true);
});
it('returns false when _events is somehow undefined', () => {
const see = new SafeEventEmitter();
see.on('foo', () => { /* */ });
delete (see as any)._events;
expect(see.emit('foo', 42)).toBe(false);
});
it('throws error from handler after setTimeout', () => {
jest.useFakeTimers();
const see = new SafeEventEmitter();
see.on('boom', () => {
throw new Error('foo');
});
expect(() => {
see.emit('boom');
}).not.toThrow();
expect(() => {
jest.runAllTimers();
}).toThrow('foo');
});
it('throws error emitted when there is no error handler', () => {
const see = new SafeEventEmitter();
expect(() => {
see.emit('error', new Error('foo'));
}).toThrow('foo');
});
it('throws error emitted when there is no error handler AND _events is somehow undefined', () => {
const see = new SafeEventEmitter();
delete (see as any)._events;
expect(() => {
see.emit('error', new Error('foo'));
}).toThrow('foo');
});
it('throws default error when there is no error handler and error event emitted', () => {
const see = new SafeEventEmitter();
expect(() => {
see.emit('error');
}).toThrow('Unhandled error.');
});
it('throws error when there is no error handler and error event emitted', () => {
const see = new SafeEventEmitter();
expect(() => {
see.emit('error', { message: 'foo' });
}).toThrow('Unhandled error. (foo)');
});
});