Skip to content

Commit

Permalink
migrate more usages
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Sep 8, 2023
1 parent fc7f293 commit 51cc9da
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 197 deletions.
19 changes: 4 additions & 15 deletions src/__tests__/graphqlSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ApolloError, PROTOCOL_ERRORS_SYMBOL } from "../errors";
import { QueryManager } from "../core/QueryManager";
import { itAsync, mockObservableLink } from "../testing";
import { GraphQLError } from "graphql";
import { spyOnConsole } from "../testing/internal";

describe("GraphQL Subscriptions", () => {
const results = [
Expand Down Expand Up @@ -328,9 +329,7 @@ describe("GraphQL Subscriptions", () => {
};

// Silence expected warning about missing field for cache write
const consoleSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using _consoleSpy = spyOnConsole("warn");

link.simulateResult(errorResult, true);

Expand All @@ -348,8 +347,6 @@ describe("GraphQL Subscriptions", () => {
],
})
);

consoleSpy.mockRestore();
});

it('strips errors in next result when `errorPolicy` is "ignore"', async () => {
Expand Down Expand Up @@ -443,9 +440,7 @@ describe("GraphQL Subscriptions", () => {
};

// Silence expected warning about missing field for cache write
const consoleSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using _consoleSpy = spyOnConsole("warn");

link.simulateResult(errorResult, true);

Expand All @@ -463,8 +458,6 @@ describe("GraphQL Subscriptions", () => {
],
})
);

consoleSpy.mockRestore();
});

it("should call complete handler when the subscription completes", () => {
Expand Down Expand Up @@ -546,14 +539,10 @@ describe("GraphQL Subscriptions", () => {
};

// Silence expected warning about missing field for cache write
const consoleSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using _consoleSpy = spyOnConsole("warn");

link.simulateResult(errorResult);

await promise;

consoleSpy.mockRestore();
});
});
21 changes: 7 additions & 14 deletions src/react/components/__tests__/client/Subscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ApolloProvider } from "../../../context";
import { ApolloLink, Operation } from "../../../../link/core";
import { itAsync, MockSubscriptionLink } from "../../../../testing";
import { Subscription } from "../../Subscription";
import { spyOnConsole } from "../../../../testing/internal";

const results = [
"Luke Skywalker",
Expand Down Expand Up @@ -118,9 +119,7 @@ it("calls onData if given", async () => {
});

it("calls onSubscriptionData with deprecation warning if given", async () => {
const consoleWarnSpy = jest
.spyOn(console, "warn")
.mockImplementation(() => {});
using consoleSpy = spyOnConsole("warn");
let count = 0;

const Component = () => (
Expand All @@ -141,8 +140,8 @@ it("calls onSubscriptionData with deprecation warning if given", async () => {
</ApolloProvider>
);

expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect(consoleSpy.warn).toHaveBeenCalledTimes(1);
expect(consoleSpy.warn).toHaveBeenCalledWith(
expect.stringContaining("'onSubscriptionData' is deprecated")
);

Expand All @@ -152,8 +151,6 @@ it("calls onSubscriptionData with deprecation warning if given", async () => {
}, 10);

await waitFor(() => expect(count).toBe(4));

consoleWarnSpy.mockRestore();
});

it("should call onComplete if specified", async () => {
Expand Down Expand Up @@ -187,9 +184,7 @@ it("should call onComplete if specified", async () => {
});

it("should call onSubscriptionComplete with deprecation warning if specified", async () => {
const consoleWarnSpy = jest
.spyOn(console, "warn")
.mockImplementation(() => {});
using consoleSpy = spyOnConsole("warn");
let count = 0;

let done = false;
Expand All @@ -211,8 +206,8 @@ it("should call onSubscriptionComplete with deprecation warning if specified", a
</ApolloProvider>
);

expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect(consoleSpy.warn).toHaveBeenCalledTimes(1);
expect(consoleSpy.warn).toHaveBeenCalledWith(
expect.stringContaining("'onSubscriptionComplete' is deprecated")
);

Expand All @@ -222,8 +217,6 @@ it("should call onSubscriptionComplete with deprecation warning if specified", a
}, 10);

await waitFor(() => expect(done).toBeTruthy());

consoleWarnSpy.mockRestore();
});

itAsync(
Expand Down
6 changes: 1 addition & 5 deletions src/react/hooks/__tests__/useBackgroundQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3767,9 +3767,7 @@ describe("useBackgroundQuery", () => {
// Disable error message shown in the console due to an uncaught error.
// TODO: need to determine why the error message is logged to the console
// as an uncaught error since other tests do not require this.
const consoleSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using _consoleSpy = spyOnConsole("error");

expect(screen.getByText("Loading")).toBeInTheDocument();

Expand All @@ -3790,8 +3788,6 @@ describe("useBackgroundQuery", () => {
});

expect(screen.queryByText("Loading")).not.toBeInTheDocument();

consoleSpy.mockRestore();
});

it("`refetch` works with startTransition to allow React to show stale UI until finished suspending", async () => {
Expand Down
9 changes: 3 additions & 6 deletions src/react/hooks/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,7 @@ describe("useMutation Hook", () => {
});

it(`should ignore errors when errorPolicy is 'ignore'`, async () => {
const errorMock = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using consoleSpy = spyOnConsole("error");
const variables = {
description: "Get milk!",
};
Expand Down Expand Up @@ -531,9 +529,8 @@ describe("useMutation Hook", () => {
});

expect(fetchResult).toEqual({});
expect(errorMock).toHaveBeenCalledTimes(1);
expect(errorMock.mock.calls[0][0]).toMatch("Missing field");
errorMock.mockRestore();
expect(consoleSpy.error).toHaveBeenCalledTimes(1);
expect(consoleSpy.error.mock.calls[0][0]).toMatch("Missing field");
});

it(`should not call onError when errorPolicy is 'ignore'`, async () => {
Expand Down
62 changes: 25 additions & 37 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4652,7 +4652,7 @@ describe("useQuery Hook", () => {

// This test was added for issue https://github.com/apollographql/apollo-client/issues/9794
it("onCompleted can set state without causing react errors", async () => {
const errorSpy = jest.spyOn(console, "error");
using consoleSpy = spyOnConsole("error");
const query = gql`
{
hello
Expand Down Expand Up @@ -4692,8 +4692,7 @@ describe("useQuery Hook", () => {

render(<ParentComponent />);
await screen.findByText("onCompletedCalled: true");
expect(errorSpy).not.toHaveBeenCalled();
errorSpy.mockRestore();
expect(consoleSpy.error).not.toHaveBeenCalled();
});

it("onCompleted should not execute on cache writes after initial query execution", async () => {
Expand Down Expand Up @@ -4978,9 +4977,7 @@ describe("useQuery Hook", () => {

describe("Partial refetch", () => {
it("should attempt a refetch when data is missing and partialRefetch is true", async () => {
const errorSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using consoleSpy = spyOnConsole("error");
const query = gql`
{
hello
Expand Down Expand Up @@ -5033,9 +5030,8 @@ describe("useQuery Hook", () => {
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);

expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy.mock.calls[0][0]).toMatch("Missing field");
errorSpy.mockRestore();
expect(consoleSpy.error).toHaveBeenCalledTimes(1);
expect(consoleSpy.error.mock.calls[0][0]).toMatch("Missing field");

await waitFor(
() => {
Expand Down Expand Up @@ -5064,9 +5060,7 @@ describe("useQuery Hook", () => {
allPeople: { people: [{ name: "Luke Skywalker" }] },
};

const errorSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using consoleSpy = spyOnConsole("error");
const link = mockSingleLink(
{ request: { query }, result: { data: {} }, delay: 20 },
{ request: { query }, result: { data }, delay: 20 }
Expand Down Expand Up @@ -5105,9 +5099,8 @@ describe("useQuery Hook", () => {
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);

expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy.mock.calls[0][0]).toMatch("Missing field");
errorSpy.mockRestore();
expect(consoleSpy.error).toHaveBeenCalledTimes(1);
expect(consoleSpy.error.mock.calls[0][0]).toMatch("Missing field");

await waitFor(
() => {
Expand All @@ -5121,9 +5114,7 @@ describe("useQuery Hook", () => {
});

it("should attempt a refetch when data is missing, partialRefetch is true and addTypename is false for the cache", async () => {
const errorSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using consoleSpy = spyOnConsole("error");
const query = gql`
{
hello
Expand Down Expand Up @@ -5177,9 +5168,8 @@ describe("useQuery Hook", () => {
expect(result.current.error).toBe(undefined);
expect(result.current.data).toBe(undefined);

expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy.mock.calls[0][0]).toMatch("Missing field");
errorSpy.mockRestore();
expect(consoleSpy.error).toHaveBeenCalledTimes(1);
expect(consoleSpy.error.mock.calls[0][0]).toMatch("Missing field");

await waitFor(
() => {
Expand Down Expand Up @@ -5646,9 +5636,7 @@ describe("useQuery Hook", () => {

describe("Missing Fields", () => {
it("should log debug messages about MissingFieldErrors from the cache", async () => {
const errorSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
using consoleSpy = spyOnConsole("error");

const carQuery: DocumentNode = gql`
query cars($id: Int) {
Expand Down Expand Up @@ -5705,8 +5693,8 @@ describe("useQuery Hook", () => {
expect(result.current.data).toEqual(carData);
expect(result.current.error).toBeUndefined();

expect(errorSpy).toHaveBeenCalled();
expect(errorSpy).toHaveBeenLastCalledWith(
expect(consoleSpy.error).toHaveBeenCalled();
expect(consoleSpy.error).toHaveBeenLastCalledWith(
`Missing field '%s' while writing result %o`,
"vin",
{
Expand All @@ -5717,7 +5705,6 @@ describe("useQuery Hook", () => {
__typename: "Car",
}
);
errorSpy.mockRestore();
});

it("should return partial cache data when `returnPartialData` is true", async () => {
Expand Down Expand Up @@ -8039,17 +8026,18 @@ describe("useQuery Hook", () => {

// We know we are writing partial data to the cache so suppress the console
// warning.
const consoleSpy = jest.spyOn(console, "error").mockImplementation();
cache.writeQuery({
query,
data: {
greeting: {
__typename: "Greeting",
recipient: { __typename: "Person", name: "Cached Alice" },
{
using _consoleSpy = spyOnConsole("error");
cache.writeQuery({
query,
data: {
greeting: {
__typename: "Greeting",
recipient: { __typename: "Person", name: "Cached Alice" },
},
},
},
});
consoleSpy.mockRestore();
});
}

const { result } = renderHook(
() =>
Expand Down
Loading

0 comments on commit 51cc9da

Please sign in to comment.