Skip to content

Commit

Permalink
Remove itAsync from remaining relevant tests (#12210)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller authored Jan 11, 2025
1 parent bb93405 commit cda321c
Show file tree
Hide file tree
Showing 7 changed files with 1,050 additions and 1,193 deletions.
210 changes: 76 additions & 134 deletions src/__tests__/graphqlSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { ApolloClient, FetchResult } from "../core";
import { InMemoryCache } from "../cache";
import { ApolloError, PROTOCOL_ERRORS_SYMBOL } from "../errors";
import { QueryManager } from "../core/QueryManager";
import { itAsync, mockObservableLink } from "../testing";
import { mockObservableLink } from "../testing";
import { GraphQLError } from "graphql";
import { spyOnConsole } from "../testing/internal";
import { ObservableStream, spyOnConsole } from "../testing/internal";
import { getDefaultOptionsForQueryManagerTests } from "../testing/core/mocking/mockQueryManager";

describe("GraphQL Subscriptions", () => {
Expand Down Expand Up @@ -47,62 +47,40 @@ describe("GraphQL Subscriptions", () => {
};
});

itAsync(
"should start a subscription on network interface and unsubscribe",
(resolve, reject) => {
const link = mockObservableLink();
// This test calls directly through Apollo Client
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
});
it("should start a subscription on network interface and unsubscribe", async () => {
const link = mockObservableLink();
// This test calls directly through Apollo Client
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
});

let count = 0;
const sub = client.subscribe(defaultOptions).subscribe({
next(result) {
count++;
expect(result).toEqual(results[0].result);
const stream = new ObservableStream(client.subscribe(defaultOptions));
link.simulateResult(results[0]);

// Test unsubscribing
if (count > 1) {
throw new Error("next fired after unsubscribing");
}
sub.unsubscribe();
resolve();
},
});
await expect(stream).toEmitValue(results[0].result);

link.simulateResult(results[0]);
}
);
stream.unsubscribe();
});

itAsync("should subscribe with default values", (resolve, reject) => {
it("should subscribe with default values", async () => {
const link = mockObservableLink();
// This test calls directly through Apollo Client
const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false }),
});

let count = 0;
const sub = client.subscribe(options).subscribe({
next(result) {
expect(result).toEqual(results[0].result);
const stream = new ObservableStream(client.subscribe(options));

// Test unsubscribing
if (count > 1) {
throw new Error("next fired after unsubscribing");
}
sub.unsubscribe();
link.simulateResult(results[0]);

resolve();
},
});
await expect(stream).toEmitValue(results[0].result);

link.simulateResult(results[0]);
stream.unsubscribe();
});

itAsync("should multiplex subscriptions", (resolve, reject) => {
it("should multiplex subscriptions", async () => {
const link = mockObservableLink();
const queryManager = new QueryManager(
getDefaultOptionsForQueryManagerTests({
Expand All @@ -112,88 +90,57 @@ describe("GraphQL Subscriptions", () => {
);

const obs = queryManager.startGraphQLSubscription(options);

let counter = 0;

// tslint:disable-next-line
obs.subscribe({
next(result) {
expect(result).toEqual(results[0].result);
counter++;
if (counter === 2) {
resolve();
}
},
}) as any;

// Subscribe again. Should also receive the same result.
// tslint:disable-next-line
obs.subscribe({
next(result) {
expect(result).toEqual(results[0].result);
counter++;
if (counter === 2) {
resolve();
}
},
}) as any;
const stream1 = new ObservableStream(obs);
const stream2 = new ObservableStream(obs);

link.simulateResult(results[0]);

await expect(stream1).toEmitValue(results[0].result);
await expect(stream2).toEmitValue(results[0].result);
});

itAsync(
"should receive multiple results for a subscription",
(resolve, reject) => {
const link = mockObservableLink();
let numResults = 0;
const queryManager = new QueryManager(
getDefaultOptionsForQueryManagerTests({
link,
cache: new InMemoryCache({ addTypename: false }),
})
);

// tslint:disable-next-line
queryManager.startGraphQLSubscription(options).subscribe({
next(result) {
expect(result).toEqual(results[numResults].result);
numResults++;
if (numResults === 4) {
resolve();
}
},
}) as any;
it("should receive multiple results for a subscription", async () => {
const link = mockObservableLink();
const queryManager = new QueryManager(
getDefaultOptionsForQueryManagerTests({
link,
cache: new InMemoryCache({ addTypename: false }),
})
);

for (let i = 0; i < 4; i++) {
link.simulateResult(results[i]);
}
const stream = new ObservableStream(
queryManager.startGraphQLSubscription(options)
);

for (let i = 0; i < 4; i++) {
link.simulateResult(results[i]);
}
);

itAsync(
"should not cache subscription data if a `no-cache` fetch policy is used",
(resolve, reject) => {
const link = mockObservableLink();
const cache = new InMemoryCache({ addTypename: false });
const client = new ApolloClient({
link,
cache,
});

expect(cache.extract()).toEqual({});
await expect(stream).toEmitValue(results[0].result);
await expect(stream).toEmitValue(results[1].result);
await expect(stream).toEmitValue(results[2].result);
await expect(stream).toEmitValue(results[3].result);
await expect(stream).not.toEmitAnything();
});

options.fetchPolicy = "no-cache";
const sub = client.subscribe(options).subscribe({
next() {
expect(cache.extract()).toEqual({});
sub.unsubscribe();
resolve();
},
});
it("should not cache subscription data if a `no-cache` fetch policy is used", async () => {
const link = mockObservableLink();
const cache = new InMemoryCache({ addTypename: false });
const client = new ApolloClient({
link,
cache,
});

link.simulateResult(results[0]);
}
);
expect(cache.extract()).toEqual({});

options.fetchPolicy = "no-cache";
const stream = new ObservableStream(client.subscribe(options));

link.simulateResult(results[0]);

await expect(stream).toEmitNext();
expect(cache.extract()).toEqual({});
});

it("should throw an error if the result has errors on it", () => {
const link = mockObservableLink();
Expand Down Expand Up @@ -492,27 +439,22 @@ describe("GraphQL Subscriptions", () => {
});
});

itAsync(
"should pass a context object through the link execution chain",
(resolve, reject) => {
const link = mockObservableLink();
const client = new ApolloClient({
cache: new InMemoryCache(),
link,
});
it("should pass a context object through the link execution chain", async () => {
const link = mockObservableLink();
const client = new ApolloClient({
cache: new InMemoryCache(),
link,
});

client.subscribe(options).subscribe({
next() {
expect(link.operation?.getContext().someVar).toEqual(
options.context.someVar
);
resolve();
},
});
const stream = new ObservableStream(client.subscribe(options));

link.simulateResult(results[0]);
}
);
link.simulateResult(results[0]);

await expect(stream).toEmitNext();
expect(link.operation?.getContext().someVar).toEqual(
options.context.someVar
);
});

it("should throw an error if the result has protocolErrors on it", async () => {
const link = mockObservableLink();
Expand Down
26 changes: 13 additions & 13 deletions src/__tests__/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe("optimistic mutation results", () => {
});

it("handles errors produced by one mutation in a series", async () => {
expect.assertions(12);
expect.assertions(11);
const client = await setup(
{
request: { query: mutation },
Expand Down Expand Up @@ -303,7 +303,7 @@ describe("optimistic mutation results", () => {
});

it("can run 2 mutations concurrently and handles all intermediate states well", async () => {
expect.assertions(36);
expect.assertions(35);
function checkBothMutationsAreApplied(
expectedText1: any,
expectedText2: any
Expand Down Expand Up @@ -466,7 +466,7 @@ describe("optimistic mutation results", () => {
});

it("handles errors produced by one mutation in a series", async () => {
expect.assertions(12);
expect.assertions(11);
const client = await setup(
{
request: { query: mutation },
Expand Down Expand Up @@ -528,7 +528,7 @@ describe("optimistic mutation results", () => {
});

it("can run 2 mutations concurrently and handles all intermediate states well", async () => {
expect.assertions(36);
expect.assertions(35);
function checkBothMutationsAreApplied(
expectedText1: any,
expectedText2: any
Expand Down Expand Up @@ -831,7 +831,7 @@ describe("optimistic mutation results", () => {
});

it("will use a passed variable in optimisticResponse", async () => {
expect.assertions(8);
expect.assertions(7);
const client = await setup({
request: { query: mutation, variables },
result: mutationResult,
Expand Down Expand Up @@ -893,7 +893,7 @@ describe("optimistic mutation results", () => {
});

it("will not update optimistically if optimisticResponse returns IGNORE sentinel object", async () => {
expect.assertions(7);
expect.assertions(6);

const client = await setup({
request: { query: mutation, variables },
Expand Down Expand Up @@ -1068,7 +1068,7 @@ describe("optimistic mutation results", () => {
};

it("will insert a single itemAsync to the beginning", async () => {
expect.assertions(9);
expect.assertions(8);
const client = await setup({
request: { query: mutation },
result: mutationResult,
Expand Down Expand Up @@ -1116,7 +1116,7 @@ describe("optimistic mutation results", () => {
});

it("two array insert like mutations", async () => {
expect.assertions(11);
expect.assertions(10);
const client = await setup(
{
request: { query: mutation },
Expand Down Expand Up @@ -1198,7 +1198,7 @@ describe("optimistic mutation results", () => {
});

it("two mutations, one fails", async () => {
expect.assertions(12);
expect.assertions(11);
const client = await setup(
{
request: { query: mutation },
Expand Down Expand Up @@ -1452,7 +1452,7 @@ describe("optimistic mutation results", () => {
};

it("will insert a single itemAsync to the beginning", async () => {
expect.assertions(8);
expect.assertions(7);
const client = await setup({
request: { query: mutation },
delay: 300,
Expand Down Expand Up @@ -1510,7 +1510,7 @@ describe("optimistic mutation results", () => {
});

it("two array insert like mutations", async () => {
expect.assertions(11);
expect.assertions(10);
const client = await setup(
{
request: { query: mutation },
Expand Down Expand Up @@ -1610,7 +1610,7 @@ describe("optimistic mutation results", () => {
});

it("two mutations, one fails", async () => {
expect.assertions(12);
expect.assertions(11);
const client = await setup(
{
request: { query: mutation },
Expand Down Expand Up @@ -2299,7 +2299,7 @@ describe("optimistic mutation - githunt comments", () => {
};

it("can post a new comment", async () => {
expect.assertions(3);
expect.assertions(2);
const mutationVariables = {
repoFullName: "org/repo",
commentContent: "New Comment",
Expand Down
Loading

0 comments on commit cda321c

Please sign in to comment.