|
| 1 | +/* eslint-disable @typescript-eslint/ban-ts-comment */ |
1 | 2 | import PubSub, { PubSubInterface } from "../lib"; |
2 | 3 |
|
3 | 4 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
@@ -102,6 +103,54 @@ describe("PubSub -> Unsubscribe Method", () => { |
102 | 103 | expect(eventsAfterUnsubscribe).toHaveLength(0); |
103 | 104 | }); |
104 | 105 |
|
| 106 | + it("Should return false when a hashKey has not been passed", () => { |
| 107 | + // given |
| 108 | + const event = "unsubscribe.event"; |
| 109 | + |
| 110 | + // when |
| 111 | + pubSub.subscribe(event, noop); |
| 112 | + const eventsAfterSubscribe = pubSub.getAllSubscribers(); |
| 113 | + expect(eventsAfterSubscribe).toHaveLength(1); |
| 114 | + |
| 115 | + // then |
| 116 | + // @ts-ignore just for the tests |
| 117 | + const output = pubSub.unsubscribe(); |
| 118 | + const eventsAfterUnsubscribe = pubSub.getAllSubscribers(); |
| 119 | + |
| 120 | + expect(output).toBe(false); |
| 121 | + expect(eventsAfterUnsubscribe).toHaveLength(1); |
| 122 | + }); |
| 123 | + |
| 124 | + it("Should return true when successfully unsubscribed", () => { |
| 125 | + // given |
| 126 | + const event = "unsubscribe.event"; |
| 127 | + |
| 128 | + // when |
| 129 | + const hashKey = pubSub.subscribe(event, noop); |
| 130 | + |
| 131 | + // then |
| 132 | + const output = pubSub.unsubscribe(hashKey); |
| 133 | + const eventsAfterUnsubscribe = pubSub.getAllSubscribers(); |
| 134 | + |
| 135 | + expect(output).toEqual(true); |
| 136 | + expect(eventsAfterUnsubscribe).toHaveLength(0); |
| 137 | + }); |
| 138 | + |
| 139 | + it("Should return false when someone pass a hashKey that does not exist in the subscription list", () => { |
| 140 | + // given |
| 141 | + const event = "unsubscribe.event"; |
| 142 | + |
| 143 | + // when |
| 144 | + pubSub.subscribe(event, noop); |
| 145 | + |
| 146 | + // then |
| 147 | + const output = pubSub.unsubscribe("random-hash-key-that-not-exists"); |
| 148 | + const eventsAfterUnsubscribe = pubSub.getAllSubscribers(); |
| 149 | + |
| 150 | + expect(output).toEqual(false); |
| 151 | + expect(eventsAfterUnsubscribe).toHaveLength(1); |
| 152 | + }); |
| 153 | + |
105 | 154 | it("Should delete a specific event if there are two events with the same name in the subscribers list", () => { |
106 | 155 | // given |
107 | 156 | const event = "random.event"; |
@@ -162,6 +211,31 @@ describe("PubSub -> Publish method", () => { |
162 | 211 | expect(callback).toHaveBeenCalledWith(data); |
163 | 212 | }); |
164 | 213 |
|
| 214 | + it("Should return false when no one is subscribed to the published event", () => { |
| 215 | + // given |
| 216 | + const event = "nice.event!"; |
| 217 | + |
| 218 | + // when |
| 219 | + const output = pubSub.publish(event); |
| 220 | + |
| 221 | + // then |
| 222 | + expect(output).toEqual(false); |
| 223 | + }); |
| 224 | + |
| 225 | + it("Should return true when the event was published successfully", () => { |
| 226 | + // given |
| 227 | + const event = "nice.event!"; |
| 228 | + const callback = jest.fn(); |
| 229 | + pubSub.subscribe(event, callback); |
| 230 | + |
| 231 | + // when |
| 232 | + const output = pubSub.publish(event); |
| 233 | + |
| 234 | + // then |
| 235 | + expect(output).toEqual(true); |
| 236 | + expect(callback).toHaveBeenCalledTimes(1); |
| 237 | + }); |
| 238 | + |
165 | 239 | it("Should call callback from subscribe method (twice) after publishing the event twice.", () => { |
166 | 240 | // given |
167 | 241 | const event = "nice.event!"; |
|
0 commit comments