|
| 1 | +import { it, vi, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { EventEmitter } from './event_emitter'; |
| 4 | + |
| 5 | +it('should call all registered listeners on emit event', () => { |
| 6 | + const emitter = new EventEmitter<{ foo: number, bar: string }>(); |
| 7 | + const fooListener1 = vi.fn(); |
| 8 | + const fooListener2 = vi.fn(); |
| 9 | + |
| 10 | + emitter.on('foo', fooListener1); |
| 11 | + emitter.on('foo', fooListener2); |
| 12 | + |
| 13 | + const barListener1 = vi.fn(); |
| 14 | + const barListener2 = vi.fn(); |
| 15 | + |
| 16 | + emitter.on('bar', barListener1); |
| 17 | + emitter.on('bar', barListener2); |
| 18 | + |
| 19 | + emitter.emit('foo', 1); |
| 20 | + expect(fooListener1).toHaveBeenCalledWith(1); |
| 21 | + expect(fooListener2).toHaveBeenCalledWith(1); |
| 22 | +}) |
| 23 | + |
| 24 | +// it('should remove listeners', () => { |
| 25 | +// const emitter = new EventEmitter(); |
| 26 | +// const cb = jest.fn(); |
| 27 | +// emitter.on('foo', cb); |
| 28 | +// emitter.off('foo', cb); |
| 29 | +// emitter.emit('foo'); |
| 30 | +// expect(cb).not.toHaveBeenCalled(); |
| 31 | +// }) |
| 32 | + |
| 33 | +// it('should remove all listeners', () => { |
| 34 | +// const emitter = new EventEmitter(); |
| 35 | +// const cb = jest.fn(); |
| 36 | +// emitter.on('foo', cb); |
| 37 | +// emitter.on('foo', cb); |
| 38 | +// emitter.off('foo'); |
| 39 | +// emitter.emit('foo'); |
| 40 | +// expect(cb).not.toHaveBeenCalled(); |
| 41 | +// } |
0 commit comments