Skip to content

Commit

Permalink
Remove @nonreactive directives from queries passed to MockLink (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller authored May 14, 2024
1 parent 86984f2 commit 4c5c820
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 7 deletions.
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.
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39581,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32832
"dist/apollo-client.min.cjs": 39579,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32825
}
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

0 comments on commit 4c5c820

Please sign in to comment.