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.
152 changes: 152 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, spyOnConsole } from "../../../internal";

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

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

const link = new MockLink([
{
request: { query },
result: { data: { a: 1, b: 2, c: 3 } },
maxUsageCount: Number.POSITIVE_INFINITY,
},
]);

{
const stream = new ObservableStream(
execute(link, {
query: gql`
query A {
a
b
c
}
`,
})
);

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

{
using spy = spyOnConsole("warn");
const stream = new ObservableStream(execute(link, { query }));

expect(spy.warn).toHaveBeenCalledTimes(1);
expect(spy.warn).toHaveBeenCalledWith(
expect.stringMatching(/^No more mocked responses for the query/)
);

await expect(stream.takeError()).resolves.toEqual(expect.any(Error));
}
});

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

const link = new MockLink([
{
request: { query },
result: { data: { a: 1, b: 2, c: 3 } },
maxUsageCount: Number.POSITIVE_INFINITY,
},
]);

{
const stream = new ObservableStream(
execute(link, {
query: gql`
query A {
a
b
c
}
`,
})
);

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

{
using spy = spyOnConsole("warn");
const stream = new ObservableStream(execute(link, { query }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit weird to me, because this would never be called like that in AC. Can you give me some insights into the intent here please?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these tests, I'm just trying to "prove" that the removal of the directives happen, so if we execute a query that matches the original (i.e. it still contains the directive), it won't match, hence this check.

What here is weird to you? Are you referring to the execute call here? Or something else?


expect(spy.warn).toHaveBeenCalledTimes(1);
expect(spy.warn).toHaveBeenCalledWith(
expect.stringMatching(/^No more mocked responses for the query/)
);

await expect(stream.takeError()).resolves.toEqual(expect.any(Error));
}
});

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

const link = new MockLink([
{
request: { query },
result: { data: { a: 1, b: 2 } },
maxUsageCount: Number.POSITIVE_INFINITY,
},
]);

{
const stream = new ObservableStream(
execute(link, {
query: gql`
query A {
a
b
}
`,
})
);

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

{
using spy = spyOnConsole("warn");
const stream = new ObservableStream(
execute(link, {
query: gql`
query A {
a
b
c @client
}
`,
})
);

expect(spy.warn).toHaveBeenCalledTimes(1);
expect(spy.warn).toHaveBeenCalledWith(
expect.stringMatching(/^No more mocked responses for the query/)
);

await expect(stream.takeError()).resolves.toEqual(expect.any(Error));
}
});
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