diff options
| author | Asko Nõmm <asko@nmm.ee> | 2025-04-21 14:55:16 +0300 |
|---|---|---|
| committer | Asko Nõmm <asko@nmm.ee> | 2025-04-21 14:55:16 +0300 |
| commit | 57aafed7892df5e5e2dfed88c54f21891583fb98 (patch) | |
| tree | 6bc9c3534421505f33d6c39a9b03f08f70a060af /src/shapex.test.ts | |
| parent | 1a9481066ad2fc02ebe1fb6a2e37ab30918bc065 (diff) | |
Fixes #2
Diffstat (limited to 'src/shapex.test.ts')
| -rw-r--r-- | src/shapex.test.ts | 54 |
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", () => { |
