summaryrefslogtreecommitdiff
path: root/src/shapex.test.ts
diff options
context:
space:
mode:
authorAsko Nõmm <asko@nmm.ee>2025-04-21 14:56:10 +0300
committerGitHub <noreply@github.com>2025-04-21 14:56:10 +0300
commitd9f9c7de8d6aec0db98c183ff76c8dafc3950cf1 (patch)
tree6bc9c3534421505f33d6c39a9b03f08f70a060af /src/shapex.test.ts
parent1a9481066ad2fc02ebe1fb6a2e37ab30918bc065 (diff)
parent57aafed7892df5e5e2dfed88c54f21891583fb98 (diff)
Merge pull request #3 from tryshapex/2-fix-issue-where-subscription-data-cannot-be-different-shape-than-responses-with
Fix issue where subscription `data` cannot be different shape than responses `with`
Diffstat (limited to 'src/shapex.test.ts')
-rw-r--r--src/shapex.test.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/shapex.test.ts b/src/shapex.test.ts
index 55e38ae..2acaa29 100644
--- a/src/shapex.test.ts
+++ b/src/shapex.test.ts
@@ -170,6 +170,60 @@ describe("dispatch", () => {
args: [{ counter: 1 }, "arg-value"],
});
});
+
+ it("supports different data types for event callback and dispatch", () => {
+ type AppState = {
+ counter: number;
+ };
+
+ type ParentEventData = {
+ id: number;
+ };
+
+ type ChildEventData = {
+ message: string;
+ };
+
+ const $ = ShapeX<AppState>({ counter: 1 });
+
+ // This callback receives ChildEventData
+ const childEventCb: EventCallback<AppState, ChildEventData> = (
+ state,
+ data
+ ) => ({
+ state: data ? { ...state, counter: data.message.length } : state,
+ });
+
+ const spyChildCb = spy(childEventCb);
+
+ $.subscribe("child-event", spyChildCb);
+
+ // This callback receives ParentEventData but dispatches ChildEventData
+ const parentEventCb: EventCallback<
+ AppState,
+ ParentEventData,
+ ChildEventData
+ > = (state, data) => ({
+ state,
+ dispatch: {
+ to: "child-event",
+ with: { message: `ID ${data?.id ?? 0} processed` },
+ },
+ });
+
+ $.subscribe("parent-event", parentEventCb);
+
+ // Dispatch with parent event data
+ $.dispatch("parent-event", { id: 123 });
+
+ // Child event should be called with the child event data
+ assertSpyCall(spyChildCb, 0, {
+ args: [{ counter: 1 }, { message: "ID 123 processed" }],
+ });
+
+ // State should be updated based on the message length
+ assertEquals($.state().counter, 16);
+ });
});
describe("state change detection", () => {