diff --git a/src/core/__tests__/QueryManager/links.ts b/src/core/__tests__/ApolloClient/links.test.ts similarity index 72% rename from src/core/__tests__/QueryManager/links.ts rename to src/core/__tests__/ApolloClient/links.test.ts index de43efdf8b9..6aef5ca37e3 100644 --- a/src/core/__tests__/QueryManager/links.ts +++ b/src/core/__tests__/ApolloClient/links.test.ts @@ -1,24 +1,17 @@ -// externals import gql from "graphql-tag"; import { print } from "graphql"; - import { Observable, ObservableSubscription, } from "../../../utilities/observables/Observable"; import { ApolloLink } from "../../../link/core"; import { InMemoryCache } from "../../../cache/inmemory/inMemoryCache"; - -// mocks import { MockSubscriptionLink } from "../../../testing/core"; - -// core -import { QueryManager } from "../../QueryManager"; -import { NextLink, Operation, Reference } from "../../../core"; -import { getDefaultOptionsForQueryManagerTests } from "../../../testing/core/mocking/mockQueryManager"; +import { ApolloClient, NextLink, Operation, Reference } from "../../../core"; describe("Link interactions", () => { it("includes the cache on the context for eviction links", (done) => { + expect.assertions(3); const query = gql` query CachedLuke { people_one(id: 1) { @@ -55,14 +48,12 @@ describe("Link interactions", () => { const mockLink = new MockSubscriptionLink(); const link = ApolloLink.from([evictionLink, mockLink]); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); - - const observable = queryManager.watchQuery({ + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); + + const observable = client.watchQuery({ query, variables: {}, }); @@ -101,14 +92,12 @@ describe("Link interactions", () => { }; const link = new MockSubscriptionLink(); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); - - const observable = queryManager.watchQuery({ + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); + + const observable = client.watchQuery({ query, variables: {}, }); @@ -175,14 +164,12 @@ describe("Link interactions", () => { }; const link = new MockSubscriptionLink(); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); - - const observable = queryManager.watchQuery({ + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); + + const observable = client.watchQuery({ query, variables: {}, }); @@ -248,14 +235,12 @@ describe("Link interactions", () => { const mockLink = new MockSubscriptionLink(); const link = ApolloLink.from([evictionLink, mockLink]); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); - - void queryManager.mutate({ mutation }); + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); + + void client.mutate({ mutation }); }); it("includes passed context in the context for mutations", (done) => { @@ -279,14 +264,12 @@ describe("Link interactions", () => { const mockLink = new MockSubscriptionLink(); const link = ApolloLink.from([evictionLink, mockLink]); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); - - void queryManager.mutate({ mutation, context: { planet: "Tatooine" } }); + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); + + void client.mutate({ mutation, context: { planet: "Tatooine" } }); }); it("includes getCacheKey function on the context for cache resolvers", async () => { @@ -321,46 +304,41 @@ describe("Link interactions", () => { return Observable.of({ data: bookData }); }); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - link, - cache: new InMemoryCache({ - typePolicies: { - Query: { - fields: { - book(_, { args, toReference, readField }) { - if (!args) { - throw new Error("arg must never be null"); - } - - const ref = toReference({ __typename: "Book", id: args.id }); - if (!ref) { - throw new Error("ref must never be null"); - } - - expect(ref).toEqual({ __ref: `Book:${args.id}` }); - const found = readField("books")!.find( - (book) => book.__ref === ref.__ref - ); - expect(found).toBeTruthy(); - return found; - }, + const client = new ApolloClient({ + link, + cache: new InMemoryCache({ + typePolicies: { + Query: { + fields: { + book(_, { args, toReference, readField }) { + if (!args) { + throw new Error("arg must never be null"); + } + + const ref = toReference({ __typename: "Book", id: args.id }); + if (!ref) { + throw new Error("ref must never be null"); + } + + expect(ref).toEqual({ __ref: `Book:${args.id}` }); + const found = readField("books")!.find( + (book) => book.__ref === ref.__ref + ); + expect(found).toBeTruthy(); + return found; }, }, }, - }), - }) - ); - - await queryManager.query({ query }); - - return queryManager - .query({ query: shouldHitCacheResolver }) - .then(({ data }) => { - expect(data).toMatchObject({ - book: { title: "Woo", __typename: "Book" }, - }); - }); + }, + }), + }); + + await client.query({ query }); + + const { data } = await client.query({ query: shouldHitCacheResolver }); + expect(data).toMatchObject({ + book: { title: "Woo", __typename: "Book" }, + }); }); it("removes @client fields from the query before it reaches the link", async () => { @@ -400,14 +378,12 @@ describe("Link interactions", () => { }); }); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - link, - cache: new InMemoryCache({ addTypename: false }), - }) - ); + const client = new ApolloClient({ + link, + cache: new InMemoryCache({ addTypename: false }), + }); - await queryManager.query({ query }); + await client.query({ query }); expect(print(result.current!.query)).toEqual(print(expectedQuery)); }); diff --git a/src/core/__tests__/QueryManager/multiple-results.ts b/src/core/__tests__/ApolloClient/multiple-results.test.ts similarity index 78% rename from src/core/__tests__/QueryManager/multiple-results.ts rename to src/core/__tests__/ApolloClient/multiple-results.test.ts index a8458d0ff13..6122807d97d 100644 --- a/src/core/__tests__/QueryManager/multiple-results.ts +++ b/src/core/__tests__/ApolloClient/multiple-results.test.ts @@ -1,16 +1,10 @@ -// externals import gql from "graphql-tag"; import { InMemoryCache } from "../../../cache/inmemory/inMemoryCache"; - -// mocks import { MockSubscriptionLink, wait } from "../../../testing/core"; - -// core -import { QueryManager } from "../../QueryManager"; import { GraphQLError } from "graphql"; -import { getDefaultOptionsForQueryManagerTests } from "../../../testing/core/mocking/mockQueryManager"; import { ObservableStream } from "../../../testing/internal"; import { ApolloError } from "../../../errors"; +import { ApolloClient } from "../../ApolloClient"; describe("mutiple results", () => { it("allows multiple query results from link", async () => { @@ -40,14 +34,12 @@ describe("mutiple results", () => { }, }; const link = new MockSubscriptionLink(); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); - const observable = queryManager.watchQuery({ + const observable = client.watchQuery({ query, variables: {}, }); @@ -56,7 +48,7 @@ describe("mutiple results", () => { // fire off first result link.simulateResult({ result: { data: initialData } }); - await expect(stream).toEmitValue({ + await expect(stream).toEmitApolloQueryResult({ data: initialData, loading: false, networkStatus: 7, @@ -64,7 +56,7 @@ describe("mutiple results", () => { link.simulateResult({ result: { data: laterData } }); - await expect(stream).toEmitValue({ + await expect(stream).toEmitApolloQueryResult({ data: laterData, loading: false, networkStatus: 7, @@ -98,14 +90,12 @@ describe("mutiple results", () => { }, }; const link = new MockSubscriptionLink(); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); - const observable = queryManager.watchQuery({ + const observable = client.watchQuery({ query, variables: {}, errorPolicy: "ignore", @@ -115,7 +105,7 @@ describe("mutiple results", () => { // fire off first result link.simulateResult({ result: { data: initialData } }); - await expect(stream).toEmitValue({ + await expect(stream).toEmitApolloQueryResult({ data: initialData, loading: false, networkStatus: 7, @@ -125,7 +115,7 @@ describe("mutiple results", () => { result: { errors: [new GraphQLError("defer failed")] }, }); - await expect(stream).toEmitValueStrict({ + await expect(stream).toEmitApolloQueryResult({ data: undefined, loading: false, networkStatus: 7, @@ -134,7 +124,7 @@ describe("mutiple results", () => { await wait(20); link.simulateResult({ result: { data: laterData } }); - await expect(stream).toEmitValue({ + await expect(stream).toEmitApolloQueryResult({ data: laterData, loading: false, networkStatus: 7, @@ -168,14 +158,12 @@ describe("mutiple results", () => { }, }; const link = new MockSubscriptionLink(); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); - const observable = queryManager.watchQuery({ + const observable = client.watchQuery({ query, variables: {}, errorPolicy: "ignore", @@ -185,7 +173,7 @@ describe("mutiple results", () => { // fire off first result link.simulateResult({ result: { data: initialData } }); - await expect(stream).toEmitValue({ + await expect(stream).toEmitApolloQueryResult({ data: initialData, loading: false, networkStatus: 7, @@ -199,7 +187,7 @@ describe("mutiple results", () => { }, }); - await expect(stream).toEmitValueStrict({ + await expect(stream).toEmitApolloQueryResult({ data: laterData, loading: false, networkStatus: 7, @@ -233,14 +221,12 @@ describe("mutiple results", () => { }, }; const link = new MockSubscriptionLink(); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); - const observable = queryManager.watchQuery({ + const observable = client.watchQuery({ query, variables: {}, errorPolicy: "all", @@ -250,7 +236,7 @@ describe("mutiple results", () => { // fire off first result link.simulateResult({ result: { data: initialData } }); - await expect(stream).toEmitValue({ + await expect(stream).toEmitApolloQueryResult({ data: initialData, loading: false, networkStatus: 7, @@ -261,7 +247,7 @@ describe("mutiple results", () => { error: new Error("defer failed"), }); - await expect(stream).toEmitValue({ + await expect(stream).toEmitApolloQueryResult({ data: initialData, loading: false, networkStatus: 7, @@ -270,7 +256,7 @@ describe("mutiple results", () => { link.simulateResult({ result: { data: laterData } }); - await expect(stream).toEmitValueStrict({ + await expect(stream).toEmitApolloQueryResult({ data: laterData, loading: false, networkStatus: 7, @@ -297,14 +283,12 @@ describe("mutiple results", () => { }; const link = new MockSubscriptionLink(); - const queryManager = new QueryManager( - getDefaultOptionsForQueryManagerTests({ - cache: new InMemoryCache({ addTypename: false }), - link, - }) - ); + const client = new ApolloClient({ + cache: new InMemoryCache({ addTypename: false }), + link, + }); - const observable = queryManager.watchQuery({ + const observable = client.watchQuery({ query, variables: {}, // errorPolicy: 'none', // this is the default @@ -332,7 +316,7 @@ describe("mutiple results", () => { // fire off first result link.simulateResult({ result: { data: initialData } }); - await expect(stream).toEmitValue({ + await expect(stream).toEmitApolloQueryResult({ data: initialData, loading: false, networkStatus: 7,