Skip to content

Commit 6d19392

Browse files
committed
Verify args in subscribe and publish
1 parent 32cfb14 commit 6d19392

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/Event.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ function typeColor(type) {
3131
}
3232

3333
const subscribe = (...args) => {
34+
if (args.length < 2) {
35+
throw new Error("subscribe requires at least 2 arguments");
36+
}
37+
3438
const callback = args.pop();
3539
const type = args.join(".");
3640
const id = uuid();
@@ -69,6 +73,10 @@ const subscribe = (...args) => {
6973
};
7074

7175
const publish = (...args) => {
76+
if (args.length < 2) {
77+
throw new Error("publish requires at least 2 arguments");
78+
}
79+
7280
const payload = args.pop();
7381
const type = args.join(".");
7482

src/test/event.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { subscribe, publish, messages } from "../Event.js";
22

33
describe("react-event", () => {
4+
it("accepts type and payload", () => {
5+
expect(() => publish("TEST_EVENT")).toThrow();
6+
});
7+
48
it("subscribes and publishes events", (done) => {
59
subscribe("TEST_EVENT", (result) => {
610
expect(result).toMatchObject({ number: 10, string: "blue" });

0 commit comments

Comments
 (0)