|
| 1 | +import { createEventNotifier } from "./eventNotifier.js"; |
| 2 | + |
| 3 | +describe("EventNotifier", () => { |
| 4 | + let notifier: ReturnType<typeof createEventNotifier<string>>; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + notifier = createEventNotifier<string>(); |
| 8 | + }); |
| 9 | + |
| 10 | + test("should notify listeners in registration order", () => { |
| 11 | + const events: string[] = []; |
| 12 | + notifier.onEvent((event) => events.push(`first: ${event}`)); |
| 13 | + notifier.onEvent((event) => events.push(`second: ${event}`)); |
| 14 | + notifier.onEvent((event) => events.push(`third: ${event}`)); |
| 15 | + |
| 16 | + notifier.notify("test event"); |
| 17 | + |
| 18 | + expect(events).toEqual([ |
| 19 | + "first: test event", |
| 20 | + "second: test event", |
| 21 | + "third: test event", |
| 22 | + ]); |
| 23 | + }); |
| 24 | + |
| 25 | + test("should not notify unsubscribed listeners", () => { |
| 26 | + const events: string[] = []; |
| 27 | + const subscription = notifier.onEvent((event) => events.push(event)); |
| 28 | + |
| 29 | + notifier.notify("first event"); |
| 30 | + subscription.close(); |
| 31 | + notifier.notify("second event"); |
| 32 | + |
| 33 | + expect(events).toEqual(["first event"]); |
| 34 | + }); |
| 35 | + |
| 36 | + test("should handle function events", () => { |
| 37 | + const events: string[] = []; |
| 38 | + notifier.onEvent((event) => events.push(event)); |
| 39 | + |
| 40 | + notifier.notify(() => "dynamic event"); |
| 41 | + |
| 42 | + expect(events).toEqual(["dynamic event"]); |
| 43 | + }); |
| 44 | + |
| 45 | + test("should handle errors through error handler", () => { |
| 46 | + const errors: Error[] = []; |
| 47 | + notifier.onError((error) => errors.push(error)); |
| 48 | + |
| 49 | + notifier.onEvent(() => { |
| 50 | + throw new Error("test error"); |
| 51 | + }); |
| 52 | + |
| 53 | + notifier.notify("test event"); |
| 54 | + |
| 55 | + expect(errors).toHaveLength(1); |
| 56 | + expect(errors[0]).toBeInstanceOf(Error); |
| 57 | + expect(errors[0].message).toBe("test error"); |
| 58 | + }); |
| 59 | + |
| 60 | + test("should not notify after close", () => { |
| 61 | + const events: string[] = []; |
| 62 | + notifier.onEvent((event) => events.push(event)); |
| 63 | + |
| 64 | + notifier.notify("first event"); |
| 65 | + notifier.close(); |
| 66 | + notifier.notify("second event"); |
| 67 | + |
| 68 | + expect(events).toEqual(["first event"]); |
| 69 | + }); |
| 70 | + |
| 71 | + test("should handle multiple subscriptions and unsubscriptions", () => { |
| 72 | + const events: string[] = []; |
| 73 | + const subscription1 = notifier.onEvent((event) => |
| 74 | + events.push(`1: ${event}`) |
| 75 | + ); |
| 76 | + const subscription2 = notifier.onEvent((event) => |
| 77 | + events.push(`2: ${event}`) |
| 78 | + ); |
| 79 | + |
| 80 | + notifier.notify("first event"); |
| 81 | + subscription1.close(); |
| 82 | + notifier.notify("second event"); |
| 83 | + subscription2.close(); |
| 84 | + notifier.notify("third event"); |
| 85 | + |
| 86 | + expect(events).toEqual([ |
| 87 | + "1: first event", |
| 88 | + "2: first event", |
| 89 | + "2: second event", |
| 90 | + ]); |
| 91 | + }); |
| 92 | + |
| 93 | + test("should handle error handler after close", () => { |
| 94 | + const errors: Error[] = []; |
| 95 | + notifier.onError((error) => errors.push(error)); |
| 96 | + notifier.close(); |
| 97 | + |
| 98 | + notifier.onEvent(() => { |
| 99 | + throw new Error("test error"); |
| 100 | + }); |
| 101 | + |
| 102 | + notifier.notify("test event"); |
| 103 | + |
| 104 | + expect(errors).toHaveLength(0); |
| 105 | + }); |
| 106 | + |
| 107 | + test("should clear error handler on close", () => { |
| 108 | + const errors: Error[] = []; |
| 109 | + notifier.onError((error) => errors.push(error)); |
| 110 | + |
| 111 | + // Close should clear the error handler |
| 112 | + notifier.close(); |
| 113 | + |
| 114 | + // Setting up a new listener after close |
| 115 | + notifier.onEvent(() => { |
| 116 | + throw new Error("test error"); |
| 117 | + }); |
| 118 | + |
| 119 | + // This should not trigger the error handler since the notifier is closed |
| 120 | + notifier.notify("test event"); |
| 121 | + |
| 122 | + expect(errors).toHaveLength(0); |
| 123 | + }); |
| 124 | + |
| 125 | + test("should use the last set error handler", () => { |
| 126 | + const errors1: Error[] = []; |
| 127 | + const errors2: Error[] = []; |
| 128 | + |
| 129 | + // First error handler |
| 130 | + notifier.onError((error) => errors1.push(error)); |
| 131 | + |
| 132 | + // Second error handler should replace the first one |
| 133 | + notifier.onError((error) => errors2.push(error)); |
| 134 | + |
| 135 | + notifier.onEvent(() => { |
| 136 | + throw new Error("test error"); |
| 137 | + }); |
| 138 | + |
| 139 | + notifier.notify("test event"); |
| 140 | + |
| 141 | + expect(errors1).toHaveLength(0); |
| 142 | + expect(errors2).toHaveLength(1); |
| 143 | + expect(errors2[0].message).toBe("test error"); |
| 144 | + }); |
| 145 | +}); |
0 commit comments