Skip to content

Commit 5fc26ee

Browse files
authored
Move remaining __tests__/QueryManager tests to ApolloClient and test with ApolloClient instance (apollographql#12326)
1 parent daa4f33 commit 5fc26ee

File tree

2 files changed

+106
-146
lines changed

2 files changed

+106
-146
lines changed

src/core/__tests__/QueryManager/links.ts renamed to src/core/__tests__/ApolloClient/links.test.ts

Lines changed: 69 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
// externals
21
import gql from "graphql-tag";
32
import { print } from "graphql";
4-
53
import {
64
Observable,
75
ObservableSubscription,
86
} from "../../../utilities/observables/Observable";
97
import { ApolloLink } from "../../../link/core";
108
import { InMemoryCache } from "../../../cache/inmemory/inMemoryCache";
11-
12-
// mocks
139
import { MockSubscriptionLink } from "../../../testing/core";
14-
15-
// core
16-
import { QueryManager } from "../../QueryManager";
17-
import { NextLink, Operation, Reference } from "../../../core";
18-
import { getDefaultOptionsForQueryManagerTests } from "../../../testing/core/mocking/mockQueryManager";
10+
import { ApolloClient, NextLink, Operation, Reference } from "../../../core";
1911

2012
describe("Link interactions", () => {
2113
it("includes the cache on the context for eviction links", (done) => {
14+
expect.assertions(3);
2215
const query = gql`
2316
query CachedLuke {
2417
people_one(id: 1) {
@@ -55,14 +48,12 @@ describe("Link interactions", () => {
5548

5649
const mockLink = new MockSubscriptionLink();
5750
const link = ApolloLink.from([evictionLink, mockLink]);
58-
const queryManager = new QueryManager(
59-
getDefaultOptionsForQueryManagerTests({
60-
cache: new InMemoryCache({ addTypename: false }),
61-
link,
62-
})
63-
);
64-
65-
const observable = queryManager.watchQuery<any>({
51+
const client = new ApolloClient({
52+
cache: new InMemoryCache({ addTypename: false }),
53+
link,
54+
});
55+
56+
const observable = client.watchQuery({
6657
query,
6758
variables: {},
6859
});
@@ -101,14 +92,12 @@ describe("Link interactions", () => {
10192
};
10293

10394
const link = new MockSubscriptionLink();
104-
const queryManager = new QueryManager(
105-
getDefaultOptionsForQueryManagerTests({
106-
cache: new InMemoryCache({ addTypename: false }),
107-
link,
108-
})
109-
);
110-
111-
const observable = queryManager.watchQuery<any>({
95+
const client = new ApolloClient({
96+
cache: new InMemoryCache({ addTypename: false }),
97+
link,
98+
});
99+
100+
const observable = client.watchQuery({
112101
query,
113102
variables: {},
114103
});
@@ -175,14 +164,12 @@ describe("Link interactions", () => {
175164
};
176165

177166
const link = new MockSubscriptionLink();
178-
const queryManager = new QueryManager(
179-
getDefaultOptionsForQueryManagerTests({
180-
cache: new InMemoryCache({ addTypename: false }),
181-
link,
182-
})
183-
);
184-
185-
const observable = queryManager.watchQuery<any>({
167+
const client = new ApolloClient({
168+
cache: new InMemoryCache({ addTypename: false }),
169+
link,
170+
});
171+
172+
const observable = client.watchQuery({
186173
query,
187174
variables: {},
188175
});
@@ -248,14 +235,12 @@ describe("Link interactions", () => {
248235

249236
const mockLink = new MockSubscriptionLink();
250237
const link = ApolloLink.from([evictionLink, mockLink]);
251-
const queryManager = new QueryManager(
252-
getDefaultOptionsForQueryManagerTests({
253-
cache: new InMemoryCache({ addTypename: false }),
254-
link,
255-
})
256-
);
257-
258-
void queryManager.mutate({ mutation });
238+
const client = new ApolloClient({
239+
cache: new InMemoryCache({ addTypename: false }),
240+
link,
241+
});
242+
243+
void client.mutate({ mutation });
259244
});
260245

261246
it("includes passed context in the context for mutations", (done) => {
@@ -279,14 +264,12 @@ describe("Link interactions", () => {
279264

280265
const mockLink = new MockSubscriptionLink();
281266
const link = ApolloLink.from([evictionLink, mockLink]);
282-
const queryManager = new QueryManager(
283-
getDefaultOptionsForQueryManagerTests({
284-
cache: new InMemoryCache({ addTypename: false }),
285-
link,
286-
})
287-
);
288-
289-
void queryManager.mutate({ mutation, context: { planet: "Tatooine" } });
267+
const client = new ApolloClient({
268+
cache: new InMemoryCache({ addTypename: false }),
269+
link,
270+
});
271+
272+
void client.mutate({ mutation, context: { planet: "Tatooine" } });
290273
});
291274

292275
it("includes getCacheKey function on the context for cache resolvers", async () => {
@@ -321,46 +304,41 @@ describe("Link interactions", () => {
321304
return Observable.of({ data: bookData });
322305
});
323306

324-
const queryManager = new QueryManager(
325-
getDefaultOptionsForQueryManagerTests({
326-
link,
327-
cache: new InMemoryCache({
328-
typePolicies: {
329-
Query: {
330-
fields: {
331-
book(_, { args, toReference, readField }) {
332-
if (!args) {
333-
throw new Error("arg must never be null");
334-
}
335-
336-
const ref = toReference({ __typename: "Book", id: args.id });
337-
if (!ref) {
338-
throw new Error("ref must never be null");
339-
}
340-
341-
expect(ref).toEqual({ __ref: `Book:${args.id}` });
342-
const found = readField<Reference[]>("books")!.find(
343-
(book) => book.__ref === ref.__ref
344-
);
345-
expect(found).toBeTruthy();
346-
return found;
347-
},
307+
const client = new ApolloClient({
308+
link,
309+
cache: new InMemoryCache({
310+
typePolicies: {
311+
Query: {
312+
fields: {
313+
book(_, { args, toReference, readField }) {
314+
if (!args) {
315+
throw new Error("arg must never be null");
316+
}
317+
318+
const ref = toReference({ __typename: "Book", id: args.id });
319+
if (!ref) {
320+
throw new Error("ref must never be null");
321+
}
322+
323+
expect(ref).toEqual({ __ref: `Book:${args.id}` });
324+
const found = readField<Reference[]>("books")!.find(
325+
(book) => book.__ref === ref.__ref
326+
);
327+
expect(found).toBeTruthy();
328+
return found;
348329
},
349330
},
350331
},
351-
}),
352-
})
353-
);
354-
355-
await queryManager.query({ query });
356-
357-
return queryManager
358-
.query({ query: shouldHitCacheResolver })
359-
.then(({ data }) => {
360-
expect(data).toMatchObject({
361-
book: { title: "Woo", __typename: "Book" },
362-
});
363-
});
332+
},
333+
}),
334+
});
335+
336+
await client.query({ query });
337+
338+
const { data } = await client.query({ query: shouldHitCacheResolver });
339+
expect(data).toMatchObject({
340+
book: { title: "Woo", __typename: "Book" },
341+
});
364342
});
365343

366344
it("removes @client fields from the query before it reaches the link", async () => {
@@ -400,14 +378,12 @@ describe("Link interactions", () => {
400378
});
401379
});
402380

403-
const queryManager = new QueryManager(
404-
getDefaultOptionsForQueryManagerTests({
405-
link,
406-
cache: new InMemoryCache({ addTypename: false }),
407-
})
408-
);
381+
const client = new ApolloClient({
382+
link,
383+
cache: new InMemoryCache({ addTypename: false }),
384+
});
409385

410-
await queryManager.query({ query });
386+
await client.query({ query });
411387

412388
expect(print(result.current!.query)).toEqual(print(expectedQuery));
413389
});

0 commit comments

Comments
 (0)