|
| 1 | +import { EventBus } from '../../src/events/event-bus'; |
| 2 | +import { Events } from '../../src/events/events'; |
| 3 | + |
| 4 | +describe('EventBus', () => { |
| 5 | + let eventBus: EventBus; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + // Reset the singleton instance before each test |
| 9 | + EventBus.destroy(); |
| 10 | + eventBus = EventBus.getInstance(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + EventBus.destroy(); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('getInstance', () => { |
| 18 | + it('should return a singleton instance', () => { |
| 19 | + const instance1 = EventBus.getInstance(); |
| 20 | + const instance2 = EventBus.getInstance(); |
| 21 | + |
| 22 | + expect(instance1).toBe(instance2); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('on and emit', () => { |
| 27 | + it('should allow subscribing to events and emit them', () => { |
| 28 | + const callback = jest.fn(); |
| 29 | + |
| 30 | + eventBus.on(Events.UPDATE, callback); |
| 31 | + eventBus.emit(Events.UPDATE, 123); |
| 32 | + |
| 33 | + expect(callback).toHaveBeenCalledTimes(1); |
| 34 | + expect(callback).toHaveBeenCalledWith(123); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should call multiple callbacks for the same event', () => { |
| 38 | + const callback1 = jest.fn(); |
| 39 | + const callback2 = jest.fn(); |
| 40 | + |
| 41 | + eventBus.on(Events.DRAW, callback1); |
| 42 | + eventBus.on(Events.DRAW, callback2); |
| 43 | + eventBus.emit(Events.DRAW, 456); |
| 44 | + |
| 45 | + expect(callback1).toHaveBeenCalledWith(456); |
| 46 | + expect(callback2).toHaveBeenCalledWith(456); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should pass multiple arguments to callbacks', () => { |
| 50 | + const callback = jest.fn(); |
| 51 | + |
| 52 | + eventBus.on(Events.SYNC, callback); |
| 53 | + eventBus.emit(Events.SYNC); |
| 54 | + |
| 55 | + expect(callback).toHaveBeenCalledTimes(1); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not throw error when emitting event with no subscribers', () => { |
| 59 | + expect(() => { |
| 60 | + eventBus.emit(Events.UPDATE, 789); |
| 61 | + }).not.toThrow(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('off', () => { |
| 66 | + it('should unsubscribe a callback from an event', () => { |
| 67 | + const callback = jest.fn(); |
| 68 | + |
| 69 | + eventBus.on(Events.UPDATE, callback); |
| 70 | + eventBus.off(Events.UPDATE, callback); |
| 71 | + eventBus.emit(Events.UPDATE, 100); |
| 72 | + |
| 73 | + expect(callback).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should only remove the specified callback', () => { |
| 77 | + const callback1 = jest.fn(); |
| 78 | + const callback2 = jest.fn(); |
| 79 | + |
| 80 | + eventBus.on(Events.DRAW, callback1); |
| 81 | + eventBus.on(Events.DRAW, callback2); |
| 82 | + eventBus.off(Events.DRAW, callback1); |
| 83 | + eventBus.emit(Events.DRAW, 200); |
| 84 | + |
| 85 | + expect(callback1).not.toHaveBeenCalled(); |
| 86 | + expect(callback2).toHaveBeenCalledWith(200); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should not throw error when removing non-existent callback', () => { |
| 90 | + const callback = jest.fn(); |
| 91 | + |
| 92 | + expect(() => { |
| 93 | + eventBus.off(Events.UPDATE, callback); |
| 94 | + }).not.toThrow(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('once', () => { |
| 99 | + it('should only call the callback once', () => { |
| 100 | + const callback = jest.fn(); |
| 101 | + |
| 102 | + eventBus.once(Events.UPDATE, callback); |
| 103 | + eventBus.emit(Events.UPDATE, 1); |
| 104 | + eventBus.emit(Events.UPDATE, 2); |
| 105 | + eventBus.emit(Events.UPDATE, 3); |
| 106 | + |
| 107 | + expect(callback).toHaveBeenCalledTimes(1); |
| 108 | + expect(callback).toHaveBeenCalledWith(1); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should automatically unsubscribe after first call', () => { |
| 112 | + const callback = jest.fn(); |
| 113 | + |
| 114 | + eventBus.once(Events.DRAW, callback); |
| 115 | + eventBus.emit(Events.DRAW, 100); |
| 116 | + |
| 117 | + // Manually check if callback is removed by trying to remove it again |
| 118 | + expect(() => { |
| 119 | + eventBus.off(Events.DRAW, callback); |
| 120 | + }).not.toThrow(); |
| 121 | + |
| 122 | + eventBus.emit(Events.DRAW, 200); |
| 123 | + expect(callback).toHaveBeenCalledTimes(1); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('clear', () => { |
| 128 | + it('should remove all callbacks for an event', () => { |
| 129 | + const callback1 = jest.fn(); |
| 130 | + const callback2 = jest.fn(); |
| 131 | + |
| 132 | + eventBus.on(Events.UPDATE, callback1); |
| 133 | + eventBus.on(Events.UPDATE, callback2); |
| 134 | + eventBus.clear(Events.UPDATE); |
| 135 | + eventBus.emit(Events.UPDATE, 300); |
| 136 | + |
| 137 | + expect(callback1).not.toHaveBeenCalled(); |
| 138 | + expect(callback2).not.toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should only clear callbacks for the specified event', () => { |
| 142 | + const updateCallback = jest.fn(); |
| 143 | + const drawCallback = jest.fn(); |
| 144 | + |
| 145 | + eventBus.on(Events.UPDATE, updateCallback); |
| 146 | + eventBus.on(Events.DRAW, drawCallback); |
| 147 | + eventBus.clear(Events.UPDATE); |
| 148 | + |
| 149 | + eventBus.emit(Events.UPDATE, 400); |
| 150 | + eventBus.emit(Events.DRAW, 500); |
| 151 | + |
| 152 | + expect(updateCallback).not.toHaveBeenCalled(); |
| 153 | + expect(drawCallback).toHaveBeenCalledWith(500); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('destroy', () => { |
| 158 | + it('should reset the singleton instance', () => { |
| 159 | + const instance1 = EventBus.getInstance(); |
| 160 | + EventBus.destroy(); |
| 161 | + const instance2 = EventBus.getInstance(); |
| 162 | + |
| 163 | + expect(instance1).not.toBe(instance2); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments