Skip to content

Commit 39ec7af

Browse files
authored
Merge pull request #7 from a-bugaj/feat/return-values
feat: Return values from methods
2 parents b32272f + 124c0d2 commit 39ec7af

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
package-lock.json
33
dist
44
coverage
5-
*.tgz
5+
*.tgz
6+
.idea

__tests__/pubSub.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
12
import PubSub, { PubSubInterface } from "../lib";
23

34
// eslint-disable-next-line @typescript-eslint/no-empty-function
@@ -102,6 +103,54 @@ describe("PubSub -> Unsubscribe Method", () => {
102103
expect(eventsAfterUnsubscribe).toHaveLength(0);
103104
});
104105

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+
105154
it("Should delete a specific event if there are two events with the same name in the subscribers list", () => {
106155
// given
107156
const event = "random.event";
@@ -162,6 +211,31 @@ describe("PubSub -> Publish method", () => {
162211
expect(callback).toHaveBeenCalledWith(data);
163212
});
164213

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+
165239
it("Should call callback from subscribe method (twice) after publishing the event twice.", () => {
166240
// given
167241
const event = "nice.event!";

lib/pubSub.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,31 @@ export class PubSub {
3131
return hashKey;
3232
};
3333

34-
unsubscribe = (hashKey: HashKey): void => {
35-
if (!hashKey) return;
34+
unsubscribe = (hashKey: HashKey): boolean => {
35+
if (!hashKey) return false;
3636

37-
const events = this.subscribers.filter((subscriber) => subscriber.hashKey !== hashKey);
37+
const isExists = this.subscribers.find(
38+
({ hashKey: subHashKey }) => subHashKey === hashKey
39+
);
40+
41+
if (!isExists) return false;
3842

43+
const events = this.subscribers.filter((subscriber) => subscriber.hashKey !== hashKey);
3944
this.subscribers = events;
45+
46+
return true;
4047
};
4148

42-
publish = <E extends string, D = unknown>(event: E, data?: D): void => {
49+
publish = <E extends string, D = unknown>(event: E, data?: D): boolean => {
4350
const events = this.subscribers.filter(({ eventName }) => eventName === event);
4451

52+
if (events.length === 0) return false;
53+
4554
events.forEach((event) => {
4655
event.callback(data);
4756
});
57+
58+
return true;
4859
};
4960

5061
getAllSubscribers = (): Subscribers[] => this.subscribers;

0 commit comments

Comments
 (0)