Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tests and Initial Storage #6

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: publish
on:
push:
tags:
- v[0-9]+.[0-9]+.[0-9]+
jobs:
deploy:
uses: NucleoidJS/actions/.github/workflows/publish.yml@main
secrets: inherit
25 changes: 0 additions & 25 deletions .github/workflows/tag.yml

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@nucleoidjs/synapses",
"name": "react-event",
"version": "1.0.13",
"type": "module",
"description": "Event-driven alternative to React Context",
Expand Down
1 change: 1 addition & 0 deletions src/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const subscribe = (type, callback) => {
};

const publish = (type, payload) => {
map.set(type, payload);
if (!subscriptions[type]) return;

Object.keys(subscriptions[type]).forEach((key) => {
Expand Down
40 changes: 39 additions & 1 deletion src/test/synapses.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { subscribe, publish } from "../../index";
import { map, publish, subscribe } from "../Event";

describe("Synapses", () => {
it("subscribes and publishes events", (done) => {
Expand All @@ -13,4 +13,42 @@ describe("Synapses", () => {

publish("TEST_EVENT", { number: 10, string: "blue" });
});

it("notifies all subscribers of an event", (done) => {
let receivedByFirstSubscriber = false;
canmingir marked this conversation as resolved.
Show resolved Hide resolved
let receivedBySecondSubscriber = false;

const checkDone = () => {
if (receivedByFirstSubscriber && receivedBySecondSubscriber) {
done();
}
};

subscribe("MULTI_SUB_EVENT", (result) => {
expect(result).toMatchObject({ data: "shared" });
receivedByFirstSubscriber = true;
checkDone();
});

subscribe("MULTI_SUB_EVENT", (result) => {
expect(result).toMatchObject({ data: "shared" });
receivedBySecondSubscriber = true;
checkDone();
});

publish("MULTI_SUB_EVENT", { data: "shared" });
});

it("handles events with no subscribers without errors", () => {
expect(() =>
publish("NO_SUBSCRIBER_EVENT", { data: "none" })
).not.toThrow();
});

it("registers the last published event to map", () => {
publish("LAST_EVENT", { data: "test payload" });

const lastPublishedEvent = map.get("LAST_EVENT");
expect(lastPublishedEvent).toEqual({ data: "test payload" });
});
});
Loading