88 vi ,
99} from "vitest" ;
1010import createFetchMock from "vitest-fetch-mock" ;
11- import { initClientFetcher } from "./client" ;
11+ import { initClientFetcher , initStrictClientFetcher } from "./client" ;
1212import { TypedDocumentString } from "./testing" ;
1313import { createSha256 } from "helpers" ;
1414
@@ -26,8 +26,34 @@ const mutation = new TypedDocumentString(/* GraphQL */ `
2626 }
2727` ) ;
2828
29- const response = { foo : "foo" , bar : "bar" } ;
30- const responseString = JSON . stringify ( response ) ;
29+ const data = { foo : "foo" , bar : "bar" } ;
30+ const response = { data : data , errors : undefined } ;
31+ const successResponse = JSON . stringify ( response ) ;
32+
33+ const errorResponse = JSON . stringify ( {
34+ data : undefined ,
35+ errors : [ { message : "PersistedQueryNotFound" } ] ,
36+ } ) ;
37+
38+ const nestedErrorResponse = JSON . stringify ( {
39+ errors : [
40+ {
41+ message : "Starship not found" ,
42+ locations : [
43+ {
44+ line : 3 ,
45+ column : 3 ,
46+ } ,
47+ ] ,
48+ path : [ "secondShip" ] ,
49+ } ,
50+ ] ,
51+ data : {
52+ firstShip : "3001" ,
53+ secondShip : null ,
54+ } ,
55+ } ) ;
56+
3157const fetchMock = createFetchMock ( vi ) ;
3258
3359describe ( "gqlClientFetch" , ( ) => {
@@ -41,7 +67,7 @@ describe("gqlClientFetch", () => {
4167 } ) ;
4268
4369 it ( "should perform a query" , async ( ) => {
44- const mockedFetch = fetchMock . mockResponse ( responseString ) ;
70+ const mockedFetch = fetchMock . mockResponse ( successResponse ) ;
4571 const gqlResponse = await fetcher ( query , {
4672 myVar : "baz" ,
4773 } ) ;
@@ -76,7 +102,7 @@ describe("gqlClientFetch", () => {
76102 } ) ;
77103
78104 it ( "should perform a persisted query when enabled" , async ( ) => {
79- const mockedFetch = fetchMock . mockResponse ( responseString ) ;
105+ const mockedFetch = fetchMock . mockResponse ( successResponse ) ;
80106
81107 const gqlResponse = await persistedFetcher ( query , {
82108 myVar : "baz" ,
@@ -99,7 +125,7 @@ describe("gqlClientFetch", () => {
99125 ) ;
100126 } ) ;
101127 it ( "should perform a mutation" , async ( ) => {
102- const mockedFetch = fetchMock . mockResponse ( responseString ) ;
128+ const mockedFetch = fetchMock . mockResponse ( successResponse ) ;
103129 const gqlResponse = await fetcher ( mutation , {
104130 myVar : "baz" ,
105131 } ) ;
@@ -121,12 +147,7 @@ describe("gqlClientFetch", () => {
121147 } ) ;
122148
123149 it ( "should fallback to POST when persisted query is not found on the server" , async ( ) => {
124- const mockedFetch = fetchMock . mockResponses (
125- JSON . stringify ( {
126- errors : [ { message : "PersistedQueryNotFound" } ] ,
127- } ) ,
128- responseString ,
129- ) ;
150+ const mockedFetch = fetchMock . mockResponses ( errorResponse , successResponse ) ;
130151
131152 const gqlResponse = await persistedFetcher ( query , {
132153 myVar : "baz" ,
@@ -164,7 +185,7 @@ describe("gqlClientFetch", () => {
164185
165186 it ( "should use time out after 30 seconds by default" , async ( ) => {
166187 const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) ;
167- fetchMock . mockResponse ( responseString ) ;
188+ fetchMock . mockResponse ( successResponse ) ;
168189
169190 await fetcher ( query , {
170191 myVar : "baz" ,
@@ -182,7 +203,7 @@ describe("gqlClientFetch", () => {
182203 defaultTimeout : 1 ,
183204 } ) ;
184205 const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) ;
185- fetchMock . mockResponse ( responseString ) ;
206+ fetchMock . mockResponse ( successResponse ) ;
186207
187208 await fetcher ( query , {
188209 myVar : "baz" ,
@@ -198,7 +219,7 @@ describe("gqlClientFetch", () => {
198219
199220 it ( "should use the provided signal" , async ( ) => {
200221 const fetcher = initClientFetcher ( "https://localhost/graphql" ) ;
201- fetchMock . mockResponse ( responseString ) ;
222+ fetchMock . mockResponse ( successResponse ) ;
202223
203224 const controller = new AbortController ( ) ;
204225 await fetcher (
@@ -221,7 +242,7 @@ describe("gqlClientFetch", () => {
221242 } ) ;
222243
223244 it ( "should allow passing extra HTTP headers" , async ( ) => {
224- const mockedFetch = fetchMock . mockResponse ( responseString ) ;
245+ const mockedFetch = fetchMock . mockResponse ( successResponse ) ;
225246 const gqlResponse = await fetcher (
226247 query ,
227248 {
@@ -264,3 +285,33 @@ describe("gqlClientFetch", () => {
264285 ) ;
265286 } ) ;
266287} ) ;
288+
289+ describe ( "initStrictClientFetcher" , ( ) => {
290+ beforeAll ( ( ) => fetchMock . enableMocks ( ) ) ;
291+ afterAll ( ( ) => fetchMock . disableMocks ( ) ) ;
292+ beforeEach ( ( ) => fetchMock . resetMocks ( ) ) ;
293+
294+ it ( "should return the data directory if no error occurred" , async ( ) => {
295+ const gqlClientFetch = initStrictClientFetcher ( "https://localhost/graphql" ) ;
296+ fetchMock . mockResponse ( successResponse ) ;
297+ const gqlResponse = await gqlClientFetch ( query as any , { myVar : "baz" } ) ;
298+
299+ expect ( gqlResponse ) . toEqual ( data ) ;
300+ } ) ;
301+ it ( "should throw an aggregate error if a generic one occurred" , async ( ) => {
302+ const gqlClientFetch = initStrictClientFetcher ( "https://localhost/graphql" ) ;
303+ fetchMock . mockResponse ( errorResponse ) ;
304+ const promise = gqlClientFetch ( query as any , { myVar : "baz" } ) ;
305+
306+ await expect ( promise ) . rejects . toThrow ( ) ;
307+ } ) ;
308+ it ( "should return a response with a nested error thrown" , async ( ) => {
309+ const gqlClientFetch = initStrictClientFetcher ( "https://localhost/graphql" ) ;
310+ fetchMock . mockResponse ( nestedErrorResponse ) ;
311+ const result = await gqlClientFetch ( query as any , { myVar : "baz" } ) ;
312+
313+ expect ( result ) . toBeTruthy ( ) ;
314+ expect ( result . firstShip ) . toBe ( "3001" ) ;
315+ expect ( ( ) => result . secondShip ) . toThrowError ( "Starship not found" ) ;
316+ } ) ;
317+ } ) ;
0 commit comments