Skip to content

Commit

Permalink
Add test for refetchQueries with a DocumentNode
Browse files Browse the repository at this point in the history
  • Loading branch information
charpeni committed Dec 18, 2024
1 parent 2fb7d7a commit d04679f
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5237,6 +5237,87 @@ describe("QueryManager", () => {
await expect(stream).not.toEmitAnything();
});

it("also works with a query document node", async () => {
const mutation = gql`
mutation changeAuthorName($id: ID!) {
changeAuthorName(newName: "Jack Smith", id: $id) {
firstName
lastName
}
}
`;
const mutationData = {
changeAuthorName: {
firstName: "Jack",
lastName: "Smith",
},
};
const query = gql`
query getAuthors($id: ID!) {
author(id: $id) {
firstName
lastName
}
}
`;
const data = {
author: {
firstName: "John",
lastName: "Smith",
},
};
const secondReqData = {
author: {
firstName: "Jane",
lastName: "Johnson",
},
};

const variables = { id: "1234" };
const mutationVariables = { id: "2345" };
const queryManager = mockQueryManager(
{
request: { query, variables },
result: { data },
delay: 10,
},
{
request: { query, variables },
result: { data: secondReqData },
delay: 100,
},
{
request: { query: mutation, variables: mutationVariables },
result: { data: mutationData },
delay: 10,
}
);
const observable = queryManager.watchQuery<any>({ query, variables });
const stream = new ObservableStream(observable);

await expect(stream).toEmitMatchedValue({ data });

await queryManager.mutate({
mutation,
variables: mutationVariables,
refetchQueries: [query],
});

await expect(stream).toEmitMatchedValue(
{ data: secondReqData },
{ timeout: 150 }
);
expect(observable.getCurrentResult().data).toEqual(secondReqData);

await wait(10);

queryManager["queries"].forEach((_, queryId) => {
expect(queryId).not.toContain("legacyOneTimeQuery");
});

await expect(stream).not.toEmitAnything();
});

itAsync(
"also works with a conditional function that returns false",
(resolve, reject) => {
Expand Down

0 comments on commit d04679f

Please sign in to comment.