Skip to content
Merged
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
62 changes: 62 additions & 0 deletions src/__tests__/Squawk.subscribe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import createStore from "../Squawk";

describe("Squawk subscribe", () => {
it("should notify subscriber when the subscribed context is updated", () => {
const store = createStore({
testProp: "initial",
otherProp: 1
});

const callback = jest.fn();
const unsubscribe = store.subscribe("testProp", callback);

// Update the subscribed property
store.update({ testProp: "updated" });

// The subscriber should be notified with the new value
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith("updated");

unsubscribe();
});

it("should not notify subscriber when an unrelated context is updated", () => {
const store = createStore({
testProp: "initial",
otherProp: 1
});

const callback = jest.fn();
store.subscribe("testProp", callback);

// Update an unrelated property
store.update({ otherProp: 2 });

// The subscriber should not be notified
expect(callback).not.toHaveBeenCalled();
});

it("should no longer notify subscriber after unsubscribe is called", () => {
const store = createStore({
testProp: "initial"
});

const callback = jest.fn();
const unsubscribe = store.subscribe("testProp", callback);

// Update the subscribed property
store.update({ testProp: "updated" });

expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith("updated");

// Unsubscribe
unsubscribe();

// Update the property again
store.update({ testProp: "updated again" });

// The subscriber should not be notified a second time
expect(callback).toHaveBeenCalledTimes(1);
});
});
Loading