From 6a6186d5d79a4b4d2c2dd997ea63f1f5a412fc48 Mon Sep 17 00:00:00 2001 From: Logan Volkers Date: Thu, 30 May 2024 01:30:10 +0000 Subject: [PATCH] Reproduction of #64 in a test case --- src/react/ScopeProvider.test.tsx | 67 +++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/react/ScopeProvider.test.tsx b/src/react/ScopeProvider.test.tsx index e622fca..bf78f1e 100644 --- a/src/react/ScopeProvider.test.tsx +++ b/src/react/ScopeProvider.test.tsx @@ -1,4 +1,4 @@ -import { act, renderHook } from "@testing-library/react"; +import { act, render, renderHook } from "@testing-library/react"; import { Atom, PrimitiveAtom, @@ -475,4 +475,69 @@ strictModeSuite(({ wrapper: Outer, isStrict }) => { userLifecycle.expectToMatchCalls(["jeffrey@example.com"]); } }); + + describe("Issue #64 - Peer providers don't share a value", () => { + const Wrapper = ({ children }: { children?: React.ReactNode }) => ( + {children} + ); + + const Nested = molecule(() => use(UserMolecule)); + + const NestedComponent = () => ( +
{useMolecule(Nested).example}
+ ); + const NonNestedComponent = () => ( +
{useMolecule(UserMolecule).example}
+ ); + + test.each([{ tcase: "nested" }, { tcase: "direct" }])( + "Should render when $tcase is first", + async ({ tcase }) => { + userLifecycle.expectUncalled(); + + const result = render( + tcase === "nested" ? ( + <> + + + + + + + + ) : ( + <> + + + + + + + + ), + { + wrapper: Wrapper, + }, + ); + + // if (isStrict) { + // userLifecycle.expectCalledTimesEach(1, 2, 1); + // } else { + // userLifecycle.expectCalledTimesEach(1, 1, 0); + // } + + const a = await result.findByTestId("nested"); + const b = await result.findByTestId("non-nested"); + expect(a.innerText).toBe(b.innerText); + result.unmount(); + + // if (isStrict) { + // userLifecycle.expectCalledTimesEach(1, 2, 2); + // } else { + // userLifecycle.expectCalledTimesEach(1, 1, 1); + // userLifecycle.expectToMatchCalls(["bob"]); + // } + }, + ); + }); });