Skip to content

Commit

Permalink
Make mock response error and result optional when delay is infinite (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mspiess committed Feb 9, 2024
1 parent 0933946 commit 65ab695
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-vans-draw.md
@@ -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
@@ -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
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
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

0 comments on commit 65ab695

Please sign in to comment.