Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new pull request by comparing changes across two branches #193

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-vans-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Mocks with an infinite delay no longer require result or error
64 changes: 64 additions & 0 deletions src/testing/core/mocking/__tests__/mockLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import gql from "graphql-tag";
import { MockLink } from "../mockLink";
import { execute } from "../../../../link/core/execute";

/*
We've chosen this value as the MAXIMUM_DELAY since values that don't fit into a 32-bit signed int cause setTimeout to fire immediately
*/
const MAXIMUM_DELAY = 0x7f_ff_ff_ff;

describe("mockLink", () => {
beforeAll(() => jest.useFakeTimers());
afterAll(() => jest.useRealTimers());

const query = gql`
query A {
a
}
`;

it("should not require a result or error when delay equals Infinity", async () => {
const mockLink = new MockLink([
{
request: {
query,
},
delay: Infinity,
},
]);

const observable = execute(mockLink, { query });

const subscription = observable.subscribe(
() => fail("onNext was called"),
() => fail("onError was called"),
() => fail("onComplete was called")
);
jest.advanceTimersByTime(MAXIMUM_DELAY);
subscription.unsubscribe();
});

it("should require result or error when delay is just large", (done) => {
const mockLink = new MockLink([
{
request: {
query,
},
delay: MAXIMUM_DELAY,
},
]);

execute(mockLink, { query }).subscribe(
() => fail("onNext was called"),
(error) => {
expect(error).toBeInstanceOf(Error);
expect(error.message).toMatch(
/^Mocked response should contain either `result`, `error` or a `delay` of `Infinity`: /
);
done();
}
);

jest.advanceTimersByTime(MAXIMUM_DELAY);
});
});
4 changes: 2 additions & 2 deletions src/testing/core/mocking/mockLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ ${unmatchedVars.map((d) => ` ${stringifyForDisplay(d)}`).join("\n")}
mockedResponses.push(response);
}

if (!response.result && !response.error) {
if (!response.result && !response.error && response.delay !== Infinity) {
configError = new Error(
`Mocked response should contain either result or error: ${key}`
`Mocked response should contain either \`result\`, \`error\` or a \`delay\` of \`Infinity\`: ${key}`
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Object {

exports[`General use should pipe exceptions thrown in custom onError functions through the link chain 1`] = `[ApolloError: oh no!]`;

exports[`General use should return "Mocked response should contain" errors in response 1`] = `[ApolloError: Mocked response should contain either result or error: {"query":"query GetUser($username: String!) {\\n user(username: $username) {\\n id\\n __typename\\n }\\n}"}]`;
exports[`General use should return "Mocked response should contain" errors in response 1`] = `[ApolloError: Mocked response should contain either \`result\`, \`error\` or a \`delay\` of \`Infinity\`: {"query":"query GetUser($username: String!) {\\n user(username: $username) {\\n id\\n __typename\\n }\\n}"}]`;

exports[`General use should return "No more mocked responses" errors in response 1`] = `
[ApolloError: No more mocked responses for the query: query GetUser($username: String!) {
Expand Down
Loading