From 4c7b4de546fa20eada6b9111bf2e465285f78d95 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 8 Jul 2026 12:30:00 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20[testing=20improvement]=20Add=20?= =?UTF-8?q?tests=20for=20subscribe=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added tests for the untested `subscribe()` method in the `createdStore` function. The new test file `Squawk.subscribe.test.ts` thoroughly tests the `subscribe` method. It covers the happy path (notifying a subscriber when its specific context updates), negative cases (not notifying when unrelated contexts update), and the unsubscribe functionality. Co-authored-by: johnstrand <11484777+johnstrand@users.noreply.github.com> --- src/__tests__/Squawk.subscribe.test.ts | 62 ++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/__tests__/Squawk.subscribe.test.ts diff --git a/src/__tests__/Squawk.subscribe.test.ts b/src/__tests__/Squawk.subscribe.test.ts new file mode 100644 index 0000000..31d63e7 --- /dev/null +++ b/src/__tests__/Squawk.subscribe.test.ts @@ -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); + }); +});