From face11290e7d538c9273547e8ea0d4124c607f61 Mon Sep 17 00:00:00 2001 From: mspiess Date: Thu, 1 Feb 2024 23:38:41 +0100 Subject: [PATCH] feat(mocking): make response error and result not required when delay is infinite --- .changeset/tiny-vans-draw.md | 5 ++ .../core/mocking/__tests__/mockLink.ts | 55 +++++++++++++++++++ src/testing/core/mocking/mockLink.ts | 4 +- .../MockedProvider.test.tsx.snap | 2 +- 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 .changeset/tiny-vans-draw.md create mode 100644 src/testing/core/mocking/__tests__/mockLink.ts diff --git a/.changeset/tiny-vans-draw.md b/.changeset/tiny-vans-draw.md new file mode 100644 index 00000000000..ca0e2f55ac5 --- /dev/null +++ b/.changeset/tiny-vans-draw.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Mocks with an infinite delay no longer require result or error diff --git a/src/testing/core/mocking/__tests__/mockLink.ts b/src/testing/core/mocking/__tests__/mockLink.ts new file mode 100644 index 00000000000..80f6c2b63df --- /dev/null +++ b/src/testing/core/mocking/__tests__/mockLink.ts @@ -0,0 +1,55 @@ +import gql from "graphql-tag"; +import { MockLink } from "../mockLink"; +import { execute } from "../../../../link/core/execute"; +import { wait } from "../../../../testing/core/wait"; + +describe("mockLink", () => { + 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") + ); + await wait(10); + subscription.unsubscribe(); + }); + + it("should require result or error when delay is just large", (done) => { + const mockLink = new MockLink([ + { + request: { + query, + }, + delay: Number.MAX_VALUE, + }, + ]); + + 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 set the `delay` to `Infinity`: / + ); + done(); + } + ); + }); +}); diff --git a/src/testing/core/mocking/mockLink.ts b/src/testing/core/mocking/mockLink.ts index 6d4753746e5..487860fae61 100644 --- a/src/testing/core/mocking/mockLink.ts +++ b/src/testing/core/mocking/mockLink.ts @@ -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 set the \`delay\` to \`Infinity\`: ${key}` ); } } diff --git a/src/testing/react/__tests__/__snapshots__/MockedProvider.test.tsx.snap b/src/testing/react/__tests__/__snapshots__/MockedProvider.test.tsx.snap index 727f5edbb85..2390452b857 100644 --- a/src/testing/react/__tests__/__snapshots__/MockedProvider.test.tsx.snap +++ b/src/testing/react/__tests__/__snapshots__/MockedProvider.test.tsx.snap @@ -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 set the \`delay\` to \`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!) {