Skip to content

Commit

Permalink
Remove itAsync from schemaLink tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 12, 2024
1 parent 18ff5c0 commit 1e7d108
Showing 1 changed file with 68 additions and 116 deletions.
184 changes: 68 additions & 116 deletions src/link/schema/__tests__/schemaLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import gql from "graphql-tag";

import { execute } from "../../core/execute";
import { SchemaLink } from "../";
import { itAsync } from "../../../testing";
import { ObservableStream } from "../../../testing/internal";

const sampleQuery = gql`
query SampleQuery {
Expand Down Expand Up @@ -51,25 +51,18 @@ describe("SchemaLink", () => {
expect(link.schema).toEqual(schema);
});

itAsync("calls next and then complete", (resolve, reject) => {
const next = jest.fn();
it("calls next and then complete", async () => {
const link = new SchemaLink({ schema });
const observable = execute(link, {
query: sampleQuery,
});
observable.subscribe({
next,
error: () => {
throw new Error("Received error");
},
complete: () => {
expect(next).toHaveBeenCalledTimes(1);
resolve();
},
});
const stream = new ObservableStream(observable);

await expect(stream).toEmitNext();
await expect(stream).toComplete();
});

itAsync("calls error when fetch fails", (resolve, reject) => {
it("calls error when fetch fails", async () => {
const link = new SchemaLink({
validate: true,
schema: makeExecutableSchema({
Expand All @@ -86,98 +79,67 @@ describe("SchemaLink", () => {
const observable = execute(link, {
query: sampleQuery,
});
observable.subscribe((result) => {
expect(result.errors).toBeTruthy();
expect(result.errors!.length).toBe(1);
expect(result.errors![0].message).toMatch(/Unauthorized/);
resolve();
const stream = new ObservableStream(observable);

await expect(stream).toEmitValue({
data: { sampleQuery: null },
errors: [{ message: "Unauthorized", path: ["sampleQuery"] }],
});
});

itAsync(
"supports query which is executed synchronously",
(resolve, reject) => {
const next = jest.fn();
const link = new SchemaLink({ schema });
const introspectionQuery = gql`
query IntrospectionQuery {
__schema {
types {
name
}
it("supports query which is executed synchronously", async () => {
const link = new SchemaLink({ schema });
const introspectionQuery = gql`
query IntrospectionQuery {
__schema {
types {
name
}
}
`;
const observable = execute(link, {
query: introspectionQuery,
});
observable.subscribe(
next,
() => {
throw new Error("Received error");
},
() => {
expect(next).toHaveBeenCalledTimes(1);
resolve();
}
);
}
);

itAsync(
"passes operation context into execute with context function",
(resolve, reject) => {
const next = jest.fn();
const contextValue = { some: "value" };
const contextProvider = jest.fn((operation) => operation.getContext());
const resolvers = {
Query: {
sampleQuery: (root: any, args: any, context: any) => {
try {
expect(context).toEqual(contextValue);
} catch (error) {
reject("Should pass context into resolver");
}
},
}
`;
const observable = execute(link, {
query: introspectionQuery,
});
const stream = new ObservableStream(observable);

await expect(stream).toEmitNext();
await expect(stream).toComplete();
});

it("passes operation context into execute with context function", async () => {
const contextValue = { some: "value" };
const contextProvider = jest.fn((operation) => operation.getContext());
const resolvers = {
Query: {
sampleQuery: (root: any, args: any, context: any) => {
expect(context).toEqual(contextValue);
},
};
const schemaWithResolvers = makeExecutableSchema({
typeDefs,
resolvers,
});
const link = new SchemaLink({
schema: schemaWithResolvers,
context: contextProvider,
});
const observable = execute(link, {
query: sampleQuery,
context: contextValue,
});
observable.subscribe(
next,
(error) => reject("Shouldn't call onError"),
() => {
try {
expect(next).toHaveBeenCalledTimes(1);
expect(contextProvider).toHaveBeenCalledTimes(1);
resolve();
} catch (e) {
reject(e);
}
}
);
}
);
},
};
const schemaWithResolvers = makeExecutableSchema({
typeDefs,
resolvers,
});
const link = new SchemaLink({
schema: schemaWithResolvers,
context: contextProvider,
});
const observable = execute(link, {
query: sampleQuery,
context: contextValue,
});
const stream = new ObservableStream(observable);

await expect(stream).toEmitNext();
await expect(stream).toComplete();
expect(contextProvider).toHaveBeenCalledTimes(1);
});

itAsync("passes static context into execute", (resolve, reject) => {
const next = jest.fn();
it("passes static context into execute", async () => {
const contextValue = { some: "value" };
const resolver = jest.fn((root, args, context) => {
try {
expect(context).toEqual(contextValue);
} catch (error) {
reject("Should pass context into resolver");
}
expect(context).toEqual(contextValue);
});

const resolvers = {
Expand All @@ -196,22 +158,14 @@ describe("SchemaLink", () => {
const observable = execute(link, {
query: sampleQuery,
});
observable.subscribe(
next,
(error) => reject("Shouldn't call onError"),
() => {
try {
expect(next).toHaveBeenCalledTimes(1);
expect(resolver).toHaveBeenCalledTimes(1);
resolve();
} catch (e) {
reject(e);
}
}
);
const stream = new ObservableStream(observable);

await expect(stream).toEmitNext();
await expect(stream).toComplete();
expect(resolver).toHaveBeenCalledTimes(1);
});

itAsync("reports errors for unknown queries", (resolve, reject) => {
it("reports errors for unknown queries", async () => {
const link = new SchemaLink({
validate: true,
schema: makeExecutableSchema({
Expand All @@ -225,11 +179,9 @@ describe("SchemaLink", () => {
}
`,
});
observable.subscribe((result) => {
expect(result.errors).toBeTruthy();
expect(result.errors!.length).toBe(1);
expect(result.errors![0].message).toMatch(/Cannot query field "unknown"/);
resolve();
const stream = new ObservableStream(observable);
await expect(stream).toEmitValue({
errors: [{ message: 'Cannot query field "unknown" on type "Query".' }],
});
});
});

0 comments on commit 1e7d108

Please sign in to comment.