Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failing test for #12229 #12230

Draft
wants to merge 1 commit into
base: release-3.13
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,114 @@ describe("useQuery Hook", () => {
});
});

it('sets data to undefined when changing variables with a "network-only" fetch policy and notifyOnNetworkStatusChange: true', async () => {
const query = gql`
query ($for: String!) {
greeting
}
`;

const mocks = [
{
request: { query, variables: { for: "Bob" } },
result: { data: { greeting: "Hello, Bob" } },
delay: 20,
},
{
request: { query, variables: { for: "Sally" } },
result: { data: { greeting: "Hello, Sally" } },
delay: 20,
},
{
request: { query, variables: { for: "Bob" } },
result: { data: { greeting: "Hello again, Bob" } },
delay: 20,
},
];

using _disabledAct = disableActEnvironment();
const { takeSnapshot, rerender } = await renderHookToSnapshotStream(
(props) =>
useQuery(query, {
variables: { for: props.for },
fetchPolicy: "network-only",
notifyOnNetworkStatusChange: true,
}),
{
initialProps: { for: "Bob" },
wrapper: ({ children }) => (
<MockedProvider mocks={mocks}>{children}</MockedProvider>
),
}
);

{
const snapshot = await takeSnapshot();

expect(snapshot).toMatchObject({
data: undefined,
loading: true,
networkStatus: NetworkStatus.loading,
});
}

{
const snapshot = await takeSnapshot();

expect(snapshot).toMatchObject({
data: { greeting: "Hello, Bob" },
loading: false,
networkStatus: NetworkStatus.ready,
});
}

await rerender({ for: "Sally" });

{
const snapshot = await takeSnapshot();

expect(snapshot).toMatchObject({
data: undefined,
loading: true,
networkStatus: NetworkStatus.setVariables,
});
}

{
const snapshot = await takeSnapshot();

expect(snapshot).toMatchObject({
data: { greeting: "Hello, Sally" },
loading: false,
networkStatus: NetworkStatus.ready,
});
}

await rerender({ for: "Bob" });

{
const snapshot = await takeSnapshot();

expect(snapshot).toMatchObject({
data: undefined,
loading: true,
networkStatus: NetworkStatus.setVariables,
});
}

{
const snapshot = await takeSnapshot();

expect(snapshot).toMatchObject({
data: { greeting: "Hello again, Bob" },
loading: false,
networkStatus: NetworkStatus.ready,
});
}

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

describe("options.defaultOptions", () => {
it("can provide a default fetchPolicy", async () => {
const query = gql`
Expand Down
Loading