Skip to content

Commit

Permalink
Reproduction of #64 in a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
loganvolkers committed May 30, 2024
1 parent aa64348 commit 6a6186d
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/react/ScopeProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { act, renderHook } from "@testing-library/react";
import { act, render, renderHook } from "@testing-library/react";
import {
Atom,
PrimitiveAtom,
Expand Down Expand Up @@ -475,4 +475,69 @@ strictModeSuite(({ wrapper: Outer, isStrict }) => {
userLifecycle.expectToMatchCalls(["[email protected]"]);
}
});

describe("Issue #64 - Peer providers don't share a value", () => {
const Wrapper = ({ children }: { children?: React.ReactNode }) => (
<Outer>{children}</Outer>
);

const Nested = molecule(() => use(UserMolecule));

const NestedComponent = () => (
<div data-testid="nested">{useMolecule(Nested).example}</div>
);
const NonNestedComponent = () => (
<div data-testid="non-nested">{useMolecule(UserMolecule).example}</div>
);

test.each([{ tcase: "nested" }, { tcase: "direct" }])(
"Should render when $tcase is first",
async ({ tcase }) => {
userLifecycle.expectUncalled();

const result = render(
tcase === "nested" ? (
<>
<ScopeProvider scope={UserScope} value="bob">
<NestedComponent />
</ScopeProvider>
<ScopeProvider scope={UserScope} value="bob">
<NonNestedComponent />
</ScopeProvider>
</>
) : (
<>
<ScopeProvider scope={UserScope} value="bob">
<NonNestedComponent />
</ScopeProvider>
<ScopeProvider scope={UserScope} value="bob">
<NestedComponent />
</ScopeProvider>
</>
),
{
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"]);
// }
},
);
});
});

0 comments on commit 6a6186d

Please sign in to comment.