import type { Emitter } from '.' import { createEmitter } from '.' describe('event emitter', () => { let events: Emitter beforeEach(() => { events = createEmitter() }) it('should allow subscribing to named events', () => { const callback = jest.fn() events.on('onSelectionChange', callback) events.emit('onSelectionChange', new Set(['1', '2'])) expect(callback).toHaveBeenCalledWith(new Set(['1', '2'])) }) it('should allow unsubscribing', () => { const callback = jest.fn() const unsubscribe = events.on('onSelectionChange', callback) unsubscribe() events.emit('onSelectionChange', new Set(['1', '2'])) expect(callback).not.toHaveBeenCalled() }) it('should only remove the unsubscribed listener, not other listeners', () => { const callbackA = jest.fn() const callbackB = jest.fn() events.on('onSelectionChange', callbackA) const unsubscribeB = events.on('onSelectionChange', callbackB) unsubscribeB() events.emit('onSelectionChange', new Set(['1'])) expect(callbackA).toHaveBeenCalledWith(new Set(['1'])) expect(callbackB).not.toHaveBeenCalled() }) })