Skip to content

Commit 08636b7

Browse files
committed
emitter test
1 parent 7940193 commit 08636b7

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lib/utils/event_emitter/event_emitter.spec.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { it, vi, expect } from 'vitest';
22

33
import { EventEmitter } from './event_emitter';
44
import exp from 'constants';
5+
import { emit, removeAllListeners } from 'process';
56

67
it('should call all registered listeners correctly on emit event', () => {
78
const emitter = new EventEmitter<{ foo: number, bar: string, baz: boolean}>();
@@ -60,12 +61,27 @@ it('should remove listeners correctly', () => {
6061
expect(barListener1).toHaveBeenCalledWith('hello');
6162
})
6263

63-
// it('should remove all listeners', () => {
64-
// const emitter = new EventEmitter();
65-
// const cb = jest.fn();
66-
// emitter.on('foo', cb);
67-
// emitter.on('foo', cb);
68-
// emitter.off('foo');
69-
// emitter.emit('foo');
70-
// expect(cb).not.toHaveBeenCalled();
71-
// }
64+
it('should remove all listeners when removeAllListeners() is called', () => {
65+
const emitter = new EventEmitter<{ foo: number, bar: string, baz: boolean}>();
66+
const fooListener1 = vi.fn();
67+
const fooListener2 = vi.fn();
68+
69+
emitter.on('foo', fooListener1);
70+
emitter.on('foo', fooListener2);
71+
72+
const barListener1 = vi.fn();
73+
const barListener2 = vi.fn();
74+
75+
emitter.on('bar', barListener1);
76+
emitter.on('bar', barListener2);
77+
78+
emitter.removeAllListeners();
79+
80+
emitter.emit('foo', 1);
81+
emitter.emit('bar', 'hello');
82+
83+
expect(fooListener1).not.toHaveBeenCalled();
84+
expect(fooListener2).not.toHaveBeenCalled();
85+
expect(barListener1).not.toHaveBeenCalled();
86+
expect(barListener2).not.toHaveBeenCalled();
87+
});

0 commit comments

Comments
 (0)