|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { GttEventBus } from './bus'; |
| 4 | +import { GttEvent } from './types'; |
| 5 | + |
| 6 | +// Lifecycle payloads carry live client/map objects in production; tests only |
| 7 | +// check delivery, so a marker object cast to the payload type is enough. |
| 8 | +const payload = (tag: string) => ({ client: {}, map: {}, tag }) as any; |
| 9 | + |
| 10 | +describe('GttEventBus', () => { |
| 11 | + describe('in-process subscribers', () => { |
| 12 | + it('delivers the payload to a subscriber', () => { |
| 13 | + const bus = new GttEventBus(); |
| 14 | + const handler = vi.fn(); |
| 15 | + bus.on(GttEvent.MapReady, handler); |
| 16 | + const p = payload('ready'); |
| 17 | + bus.emit(GttEvent.MapReady, p); |
| 18 | + expect(handler).toHaveBeenCalledOnce(); |
| 19 | + expect(handler).toHaveBeenCalledWith(p); |
| 20 | + }); |
| 21 | + |
| 22 | + it('only delivers to subscribers of the emitted event', () => { |
| 23 | + const bus = new GttEventBus(); |
| 24 | + const other = vi.fn(); |
| 25 | + bus.on(GttEvent.FeatureSelect, other); |
| 26 | + bus.emit(GttEvent.MapReady, payload('ready')); |
| 27 | + expect(other).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns an unsubscribe function from on()', () => { |
| 31 | + const bus = new GttEventBus(); |
| 32 | + const handler = vi.fn(); |
| 33 | + const off = bus.on(GttEvent.GeometryChange, handler); |
| 34 | + off(); |
| 35 | + bus.emit(GttEvent.GeometryChange, payload('geom')); |
| 36 | + expect(handler).not.toHaveBeenCalled(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('removes a handler with off()', () => { |
| 40 | + const bus = new GttEventBus(); |
| 41 | + const handler = vi.fn(); |
| 42 | + bus.on(GttEvent.GeometryChange, handler); |
| 43 | + bus.off(GttEvent.GeometryChange, handler); |
| 44 | + bus.emit(GttEvent.GeometryChange, payload('geom')); |
| 45 | + expect(handler).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('fires a once() handler only on the first emit', () => { |
| 49 | + const bus = new GttEventBus(); |
| 50 | + const handler = vi.fn(); |
| 51 | + bus.once(GttEvent.LayersReady, handler); |
| 52 | + bus.emit(GttEvent.LayersReady, payload('1')); |
| 53 | + bus.emit(GttEvent.LayersReady, payload('2')); |
| 54 | + expect(handler).toHaveBeenCalledOnce(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('delivers to every subscriber of an event', () => { |
| 58 | + const bus = new GttEventBus(); |
| 59 | + const a = vi.fn(); |
| 60 | + const b = vi.fn(); |
| 61 | + bus.on(GttEvent.MapReady, a); |
| 62 | + bus.on(GttEvent.MapReady, b); |
| 63 | + bus.emit(GttEvent.MapReady, payload('ready')); |
| 64 | + expect(a).toHaveBeenCalledOnce(); |
| 65 | + expect(b).toHaveBeenCalledOnce(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('isolates a throwing handler: it is logged and the others still run', () => { |
| 69 | + const error = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 70 | + const bus = new GttEventBus(); |
| 71 | + const bad = vi.fn(() => { throw new Error('boom'); }); |
| 72 | + const good = vi.fn(); |
| 73 | + bus.on(GttEvent.MapReady, bad); |
| 74 | + bus.on(GttEvent.MapReady, good); |
| 75 | + expect(() => bus.emit(GttEvent.MapReady, payload('ready'))).not.toThrow(); |
| 76 | + expect(good).toHaveBeenCalledOnce(); |
| 77 | + expect(error).toHaveBeenCalledOnce(); |
| 78 | + error.mockRestore(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('does not throw when emitting with no subscribers', () => { |
| 82 | + const bus = new GttEventBus(); |
| 83 | + expect(() => bus.emit(GttEvent.MapReady, payload('ready'))).not.toThrow(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('DOM CustomEvent dispatch', () => { |
| 88 | + it('dispatches a bubbling CustomEvent carrying the payload on the DOM target', () => { |
| 89 | + const target = new EventTarget(); |
| 90 | + const bus = new GttEventBus(target as any); |
| 91 | + const received: CustomEvent[] = []; |
| 92 | + target.addEventListener(GttEvent.GeometryChange, (e) => received.push(e as CustomEvent)); |
| 93 | + const p = payload('geom'); |
| 94 | + bus.emit(GttEvent.GeometryChange, p); |
| 95 | + expect(received).toHaveLength(1); |
| 96 | + expect(received[0].detail).toBe(p); |
| 97 | + expect(received[0].bubbles).toBe(true); |
| 98 | + }); |
| 99 | + |
| 100 | + it('does not dispatch a DOM event when no target is set', () => { |
| 101 | + // A null target must simply skip DOM dispatch (no throw); the in-process |
| 102 | + // handler still fires. |
| 103 | + const bus = new GttEventBus(null); |
| 104 | + const handler = vi.fn(); |
| 105 | + bus.on(GttEvent.MapReady, handler); |
| 106 | + expect(() => bus.emit(GttEvent.MapReady, payload('ready'))).not.toThrow(); |
| 107 | + expect(handler).toHaveBeenCalledOnce(); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments