From 1efed6b2d2b455aa3cf2693a0d4fa641f0bbea85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Oliva?= Date: Tue, 4 Jul 2023 23:58:34 +0200 Subject: [PATCH] add failing test --- .../context-state2/src/combineStates.test.ts | 22 ++++++++++++++++++ packages/context-state2/src/substate.test.ts | 23 +++++++++++++++++++ packages/context-state2/src/substate.ts | 2 ++ 3 files changed, 47 insertions(+) diff --git a/packages/context-state2/src/combineStates.test.ts b/packages/context-state2/src/combineStates.test.ts index d9962f6..8a66ccd 100644 --- a/packages/context-state2/src/combineStates.test.ts +++ b/packages/context-state2/src/combineStates.test.ts @@ -86,6 +86,28 @@ describe("combineStates", () => { }) }) + it("updates when one of the sources updates", () => { + const root = createRoot() + const source$ = new Subject() + const nodeA = substate(root, () => source$) + const nodeB = substate(root, () => of("b")) + + const combined = combineStates({ nodeA, nodeB }) + root.run() + + source$.next("a") + expect(combined.getValue({ root: "" })).toEqual({ + nodeA: "a", + nodeB: "b", + }) + + source$.next("2") + expect(combined.getValue({ root: "" })).toEqual({ + nodeA: "2", + nodeB: "b", + }) + }) + it("doesn't emit a value until all branches have one", async () => { function createSettableNode>( root: StateNode, diff --git a/packages/context-state2/src/substate.test.ts b/packages/context-state2/src/substate.test.ts index 5f95ad0..da0f96b 100644 --- a/packages/context-state2/src/substate.test.ts +++ b/packages/context-state2/src/substate.test.ts @@ -300,6 +300,29 @@ describe("subState", () => { expect(nodeB.getValue()).toBe("b") expect(nodeA.getValue()).toBe("b-a") }) + + it.only("can reference its siblings after a change", () => { + const root = createRoot() + const source$ = new Subject() + const subNode = substate(root, () => source$) + const nodeA = substate(subNode, (ctx, getState$) => + getState$(nodeB, {}).pipe(map((v) => ctx(subNode) + "-" + v + "-a")), + ) + const nodeB = substate(subNode, (ctx) => of("b" + ctx(subNode))) + + root.run() + + expect(nodeA.getValue()).toBeInstanceOf(Promise) + source$.next("1") + + expect(nodeB.getValue()).toBe("b1") + expect(nodeA.getValue()).toBe("1-b1-a") + + source$.next("2") + + expect(nodeB.getValue()).toBe("b2") + expect(nodeA.getValue()).toBe("2-b2-a") + }) }) describe("state$", () => { diff --git a/packages/context-state2/src/substate.ts b/packages/context-state2/src/substate.ts index 295bda3..ee3853b 100644 --- a/packages/context-state2/src/substate.ts +++ b/packages/context-state2/src/substate.ts @@ -41,6 +41,8 @@ export const substate = ( next: () => { stateNode.resetInstance(instanceKey) // TODO shouldn't re-activation of instances happen after all subscribers have restarted? how to do it? + // Yes. I would need a special kind of observable so that I can first reset the instances without activating them + // and then synchronously activate them }, error: () => { // TODO