Skip to content

Commit

Permalink
Test different references of a same query DocumentNode
Browse files Browse the repository at this point in the history
  • Loading branch information
charpeni committed Dec 18, 2024
1 parent d04679f commit 19d14a7
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5318,6 +5318,88 @@ describe("QueryManager", () => {
await expect(stream).not.toEmitAnything();
});

it("also works with different references of a same 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,
// spread the query into a new object to simulate multiple instances
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 19d14a7

Please sign in to comment.