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

Remove @nonreactive directives from queries passed to MockLink #11845

Merged
merged 13 commits into from
May 14, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/serious-candles-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Remove `@nonreactive` directives from queries passed to `MockLink` to ensure they are properly matched.
154 changes: 154 additions & 0 deletions src/testing/core/mocking/__tests__/mockLink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gql from "graphql-tag";
import { MockLink, MockedResponse } from "../mockLink";
import { execute } from "../../../../link/core/execute";
import { ObservableStream } from "../../../internal";

describe("MockedResponse.newData", () => {
const setup = () => {
Expand Down Expand Up @@ -126,3 +127,156 @@ describe("mockLink", () => {
jest.advanceTimersByTime(MAXIMUM_DELAY);
});
});

test("removes @nonreactive directives from fields", async () => {
const serverQuery = gql`
query A {
a
b
c
}
`;

const link = new MockLink([
{
request: {
query: gql`
query A {
a
b
c @nonreactive
}
`,
},
result: { data: { a: 1, b: 2, c: 3 } },
},
{
request: {
query: gql`
query A {
a
b
c
}
`,
},
result: { data: { a: 4, b: 5, c: 6 } },
},
]);

{
const stream = new ObservableStream(execute(link, { query: serverQuery }));

await expect(stream.takeNext()).resolves.toEqual({
data: { a: 1, b: 2, c: 3 },
});
}

{
const stream = new ObservableStream(execute(link, { query: serverQuery }));

await expect(stream.takeNext()).resolves.toEqual({
data: { a: 4, b: 5, c: 6 },
});
}
});

test("removes @connection directives", async () => {
const serverQuery = gql`
query A {
a
b
c
}
`;

const link = new MockLink([
{
request: {
query: gql`
query A {
a
b
c @connection(key: "test")
}
`,
},
result: { data: { a: 1, b: 2, c: 3 } },
},
{
request: {
query: gql`
query A {
a
b
c
}
`,
},
result: { data: { a: 4, b: 5, c: 6 } },
},
]);

{
const stream = new ObservableStream(execute(link, { query: serverQuery }));

await expect(stream.takeNext()).resolves.toEqual({
data: { a: 1, b: 2, c: 3 },
});
}

{
const stream = new ObservableStream(execute(link, { query: serverQuery }));

await expect(stream.takeNext()).resolves.toEqual({
data: { a: 4, b: 5, c: 6 },
});
}
});

test("removes fields with @client directives", async () => {
const serverQuery = gql`
query A {
a
b
}
`;

const link = new MockLink([
{
request: {
query: gql`
query A {
a
b
c @client
}
`,
},
result: { data: { a: 1, b: 2 } },
},
{
request: {
query: gql`
query A {
a
b
}
`,
},
result: { data: { a: 3, b: 4 } },
},
]);

{
const stream = new ObservableStream(execute(link, { query: serverQuery }));

await expect(stream.takeNext()).resolves.toEqual({ data: { a: 1, b: 2 } });
}

{
const stream = new ObservableStream(execute(link, { query: serverQuery }));

await expect(stream.takeNext()).resolves.toEqual({ data: { a: 3, b: 4 } });
}
});
12 changes: 7 additions & 5 deletions src/testing/core/mocking/mockLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import {
Observable,
addTypenameToDocument,
removeClientSetsFromDocument,
removeConnectionDirectiveFromDocument,
cloneDeep,
stringifyForDisplay,
print,
removeDirectivesFromDocument,
checkDocument,
} from "../../../utilities/index.js";

export type ResultFunction<T, V = Record<string, any>> = (variables: V) => T;
Expand Down Expand Up @@ -202,11 +203,12 @@ ${unmatchedVars.map((d) => ` ${stringifyForDisplay(d)}`).join("\n")}
mockedResponse: MockedResponse
): MockedResponse {
const newMockedResponse = cloneDeep(mockedResponse);
const queryWithoutConnection = removeConnectionDirectiveFromDocument(
newMockedResponse.request.query
const queryWithoutClientOnlyDirectives = removeDirectivesFromDocument(
[{ name: "connection" }, { name: "nonreactive" }],
checkDocument(newMockedResponse.request.query)
);
invariant(queryWithoutConnection, "query is required");
newMockedResponse.request.query = queryWithoutConnection!;
invariant(queryWithoutClientOnlyDirectives, "query is required");
newMockedResponse.request.query = queryWithoutClientOnlyDirectives!;
const query = removeClientSetsFromDocument(newMockedResponse.request.query);
if (query) {
newMockedResponse.request.query = query;
Expand Down
Loading