|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { EventEmitter } from 'events'; |
| 3 | + |
| 4 | +describe('EventEmitter compatibility', () => { |
| 5 | + let originalPrototype: any; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + originalPrototype = { |
| 9 | + addListener: EventEmitter.prototype.addListener, |
| 10 | + on: EventEmitter.prototype.on, |
| 11 | + removeListener: EventEmitter.prototype.removeListener, |
| 12 | + off: EventEmitter.prototype.off, |
| 13 | + removeAllListeners: EventEmitter.prototype.removeAllListeners, |
| 14 | + once: EventEmitter.prototype.once, |
| 15 | + emit: EventEmitter.prototype.emit, |
| 16 | + listeners: EventEmitter.prototype.listeners, |
| 17 | + setMaxListeners: EventEmitter.prototype.setMaxListeners, |
| 18 | + }; |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + Object.keys(originalPrototype).forEach(key => { |
| 23 | + EventEmitter.prototype[key] = originalPrototype[key]; |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('Express-like usage pattern', () => { |
| 28 | + it('should handle EventEmitter instances without _events property', async () => { |
| 29 | + await import('../../lib/events.js'); |
| 30 | + |
| 31 | + const emitter = new EventEmitter(); |
| 32 | + |
| 33 | + expect(() => { |
| 34 | + emitter.addListener('mount', () => { |
| 35 | + console.log('mounted'); |
| 36 | + }); |
| 37 | + }).not.toThrow(); |
| 38 | + |
| 39 | + expect(emitter.listenerCount('mount')).toBe(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle removeListener on uninitialized EventEmitter', async () => { |
| 43 | + await import('../../lib/events.js'); |
| 44 | + |
| 45 | + const emitter = new EventEmitter(); |
| 46 | + |
| 47 | + expect(() => { |
| 48 | + emitter.removeListener('nonexistent', () => {}); |
| 49 | + }).not.toThrow(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle removeAllListeners on uninitialized EventEmitter', async () => { |
| 53 | + await import('../../lib/events.js'); |
| 54 | + |
| 55 | + const emitter = new EventEmitter(); |
| 56 | + |
| 57 | + // Should not throw when removing all listeners |
| 58 | + expect(() => { |
| 59 | + emitter.removeAllListeners(); |
| 60 | + }).not.toThrow(); |
| 61 | + |
| 62 | + expect(() => { |
| 63 | + emitter.removeAllListeners('specific'); |
| 64 | + }).not.toThrow(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle emit on uninitialized EventEmitter', async () => { |
| 68 | + await import('../../lib/events.js'); |
| 69 | + |
| 70 | + const emitter = new EventEmitter(); |
| 71 | + |
| 72 | + // Should not throw when emitting events |
| 73 | + expect(() => { |
| 74 | + emitter.emit('test', 'data'); |
| 75 | + }).not.toThrow(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle once on uninitialized EventEmitter', async () => { |
| 79 | + await import('../../lib/events.js'); |
| 80 | + |
| 81 | + const emitter = new EventEmitter(); |
| 82 | + let called = false; |
| 83 | + |
| 84 | + // Should not throw when using once |
| 85 | + expect(() => { |
| 86 | + emitter.once('test', () => { |
| 87 | + called = true; |
| 88 | + }); |
| 89 | + }).not.toThrow(); |
| 90 | + |
| 91 | + emitter.emit('test'); |
| 92 | + expect(called).toBe(true); |
| 93 | + |
| 94 | + // Verify it only fires once |
| 95 | + called = false; |
| 96 | + emitter.emit('test'); |
| 97 | + expect(called).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should handle listeners on uninitialized EventEmitter', async () => { |
| 101 | + await import('../../lib/events.js'); |
| 102 | + |
| 103 | + const emitter = new EventEmitter(); |
| 104 | + |
| 105 | + // Should not throw and return empty array |
| 106 | + expect(() => { |
| 107 | + const listeners = emitter.listeners('test'); |
| 108 | + expect(listeners).toEqual([]); |
| 109 | + }).not.toThrow(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('Mixed usage with blessed widgets', () => { |
| 114 | + it('should work with both native EventEmitters and blessed objects', async () => { |
| 115 | + await import('../../lib/events.js'); |
| 116 | + |
| 117 | + // Native EventEmitter (like Express uses) |
| 118 | + const nativeEmitter = new EventEmitter(); |
| 119 | + |
| 120 | + // Simulated blessed object with _events already initialized |
| 121 | + const blessedEmitter: any = new EventEmitter(); |
| 122 | + blessedEmitter._events = {}; |
| 123 | + blessedEmitter.type = 'screen'; |
| 124 | + |
| 125 | + // Both should work without errors |
| 126 | + expect(() => { |
| 127 | + nativeEmitter.on('test', () => {}); |
| 128 | + blessedEmitter.on('test', () => {}); |
| 129 | + }).not.toThrow(); |
| 130 | + |
| 131 | + expect(() => { |
| 132 | + nativeEmitter.emit('test'); |
| 133 | + blessedEmitter.emit('test'); |
| 134 | + }).not.toThrow(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('Event bubbling compatibility', () => { |
| 139 | + it('should handle parent chain traversal safely', async () => { |
| 140 | + await import('../../lib/events.js'); |
| 141 | + |
| 142 | + const child: any = new EventEmitter(); |
| 143 | + const parent: any = new EventEmitter(); |
| 144 | + child.parent = parent; |
| 145 | + |
| 146 | + // Should not throw even with uninitialized _events |
| 147 | + expect(() => { |
| 148 | + child.emit('test'); |
| 149 | + }).not.toThrow(); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments