add failing test

This commit is contained in:
Víctor Oliva 2023-07-04 23:58:34 +02:00
parent 6a7894eede
commit 1efed6b2d2
3 changed files with 47 additions and 0 deletions

View File

@ -86,6 +86,28 @@ describe("combineStates", () => {
})
})
it("updates when one of the sources updates", () => {
const root = createRoot()
const source$ = new Subject<string>()
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<K extends Record<string, any>>(
root: StateNode<never, K>,

View File

@ -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<string>()
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$", () => {

View File

@ -41,6 +41,8 @@ export const substate = <T, K extends KeysBaseType>(
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