Skip to content

Commit

Permalink
Add test to ensure switching from non-null to null works as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 18, 2024
1 parent 0df7960 commit 286f4c3
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/react/hooks/__tests__/useSuspenseFragment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,67 @@ test("returns cached value when `from` changes from `null` to non-null value", a
await expect(takeSnapshot).not.toRerender();
});

test("returns null value when `from` changes from non-null value to `null`", async () => {
interface ItemFragment {
__typename: "Item";
id: number;
text: string;
}

const fragment: TypedDocumentNode<ItemFragment> = gql`
fragment ItemFragment on Item {
id
text
}
`;

const client = new ApolloClient({ cache: new InMemoryCache() });

client.writeFragment({
fragment,
data: {
__typename: "Item",
id: 1,
text: "Item #1",
},
});

using _disabledAct = disableActEnvironment();
const { takeSnapshot, rerender } = await renderHookToSnapshotStream(
({ id }) =>
useSuspenseFragment({
fragment,
from: id === null ? null : { __typename: "Item", id },
}),
{
initialProps: { id: 1 as null | number },
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
}
);

{
const { data } = await takeSnapshot();

expect(data).toEqual({
__typename: "Item",
id: 1,
text: "Item #1",
});
}

await rerender({ id: null });

{
const { data } = await takeSnapshot();

expect(data).toBeNull();
}

await expect(takeSnapshot).not.toRerender();
});

test("suspends until cached value is available when `from` changes from `null` to non-null value", async () => {
interface ItemFragment {
__typename: "Item";
Expand Down

0 comments on commit 286f4c3

Please sign in to comment.