|
1 | 1 | import React, { Fragment, ReactNode, useEffect, useRef, useState } from "react"; |
2 | | -import { DocumentNode, GraphQLError } from "graphql"; |
| 2 | +import { DocumentNode, GraphQLError, GraphQLFormattedError } from "graphql"; |
3 | 3 | import gql from "graphql-tag"; |
4 | 4 | import { act } from "@testing-library/react"; |
5 | 5 | import userEvent from "@testing-library/user-event"; |
@@ -10081,6 +10081,85 @@ describe("useQuery Hook", () => { |
10081 | 10081 | } |
10082 | 10082 | ); |
10083 | 10083 | }); |
| 10084 | + |
| 10085 | + // https://github.com/apollographql/apollo-client/issues/11938 |
| 10086 | + it("does not emit `data` on previous fetch when a 2nd fetch is kicked off and the result returns an error when errorPolicy is none", async () => { |
| 10087 | + const query = gql` |
| 10088 | + query { |
| 10089 | + user { |
| 10090 | + id |
| 10091 | + name |
| 10092 | + } |
| 10093 | + } |
| 10094 | + `; |
| 10095 | + |
| 10096 | + const graphQLError: GraphQLFormattedError = { message: "Cannot get name" }; |
| 10097 | + |
| 10098 | + const mocks = [ |
| 10099 | + { |
| 10100 | + request: { query }, |
| 10101 | + result: { |
| 10102 | + data: { user: { __typename: "User", id: "1", name: null } }, |
| 10103 | + errors: [graphQLError], |
| 10104 | + }, |
| 10105 | + delay: 10, |
| 10106 | + maxUsageCount: Number.POSITIVE_INFINITY, |
| 10107 | + }, |
| 10108 | + ]; |
| 10109 | + |
| 10110 | + const ProfiledHook = profileHook(() => |
| 10111 | + useQuery(query, { notifyOnNetworkStatusChange: true }) |
| 10112 | + ); |
| 10113 | + |
| 10114 | + render(<ProfiledHook />, { |
| 10115 | + wrapper: ({ children }) => ( |
| 10116 | + <MockedProvider mocks={mocks}>{children}</MockedProvider> |
| 10117 | + ), |
| 10118 | + }); |
| 10119 | + |
| 10120 | + { |
| 10121 | + const { loading, data, error } = await ProfiledHook.takeSnapshot(); |
| 10122 | + |
| 10123 | + expect(loading).toBe(true); |
| 10124 | + expect(data).toBeUndefined(); |
| 10125 | + expect(error).toBeUndefined(); |
| 10126 | + } |
| 10127 | + |
| 10128 | + { |
| 10129 | + const { loading, data, error } = await ProfiledHook.takeSnapshot(); |
| 10130 | + |
| 10131 | + expect(loading).toBe(false); |
| 10132 | + expect(data).toBeUndefined(); |
| 10133 | + expect(error).toEqual(new ApolloError({ graphQLErrors: [graphQLError] })); |
| 10134 | + } |
| 10135 | + |
| 10136 | + const { refetch } = ProfiledHook.getCurrentSnapshot(); |
| 10137 | + |
| 10138 | + refetch().catch(() => {}); |
| 10139 | + refetch().catch(() => {}); |
| 10140 | + |
| 10141 | + { |
| 10142 | + const { loading, networkStatus, data, error } = |
| 10143 | + await ProfiledHook.takeSnapshot(); |
| 10144 | + |
| 10145 | + expect(loading).toBe(true); |
| 10146 | + expect(data).toBeUndefined(); |
| 10147 | + expect(networkStatus).toBe(NetworkStatus.refetch); |
| 10148 | + expect(error).toBeUndefined(); |
| 10149 | + } |
| 10150 | + |
| 10151 | + { |
| 10152 | + const { loading, networkStatus, data, error } = |
| 10153 | + await ProfiledHook.takeSnapshot(); |
| 10154 | + |
| 10155 | + expect(loading).toBe(false); |
| 10156 | + expect(data).toBeUndefined(); |
| 10157 | + expect(networkStatus).toBe(NetworkStatus.error); |
| 10158 | + expect(error).toEqual(new ApolloError({ graphQLErrors: [graphQLError] })); |
| 10159 | + } |
| 10160 | + |
| 10161 | + await expect(ProfiledHook).not.toRerender({ timeout: 200 }); |
| 10162 | + }); |
10084 | 10163 | }); |
10085 | 10164 |
|
10086 | 10165 | describe.skip("Type Tests", () => { |
|
0 commit comments