8
8
vi ,
9
9
} from "vitest" ;
10
10
import createFetchMock from "vitest-fetch-mock" ;
11
- import { initClientFetcher } from "./client" ;
11
+ import { initClientFetcher , initStrictClientFetcher } from "./client" ;
12
12
import { TypedDocumentString } from "./testing" ;
13
13
import { createSha256 } from "helpers" ;
14
14
@@ -26,8 +26,34 @@ const mutation = new TypedDocumentString(/* GraphQL */ `
26
26
}
27
27
` ) ;
28
28
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
+
31
57
const fetchMock = createFetchMock ( vi ) ;
32
58
33
59
describe ( "gqlClientFetch" , ( ) => {
@@ -41,7 +67,7 @@ describe("gqlClientFetch", () => {
41
67
} ) ;
42
68
43
69
it ( "should perform a query" , async ( ) => {
44
- const mockedFetch = fetchMock . mockResponse ( responseString ) ;
70
+ const mockedFetch = fetchMock . mockResponse ( successResponse ) ;
45
71
const gqlResponse = await fetcher ( query , {
46
72
myVar : "baz" ,
47
73
} ) ;
@@ -76,7 +102,7 @@ describe("gqlClientFetch", () => {
76
102
} ) ;
77
103
78
104
it ( "should perform a persisted query when enabled" , async ( ) => {
79
- const mockedFetch = fetchMock . mockResponse ( responseString ) ;
105
+ const mockedFetch = fetchMock . mockResponse ( successResponse ) ;
80
106
81
107
const gqlResponse = await persistedFetcher ( query , {
82
108
myVar : "baz" ,
@@ -99,7 +125,7 @@ describe("gqlClientFetch", () => {
99
125
) ;
100
126
} ) ;
101
127
it ( "should perform a mutation" , async ( ) => {
102
- const mockedFetch = fetchMock . mockResponse ( responseString ) ;
128
+ const mockedFetch = fetchMock . mockResponse ( successResponse ) ;
103
129
const gqlResponse = await fetcher ( mutation , {
104
130
myVar : "baz" ,
105
131
} ) ;
@@ -121,12 +147,7 @@ describe("gqlClientFetch", () => {
121
147
} ) ;
122
148
123
149
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 ) ;
130
151
131
152
const gqlResponse = await persistedFetcher ( query , {
132
153
myVar : "baz" ,
@@ -164,7 +185,7 @@ describe("gqlClientFetch", () => {
164
185
165
186
it ( "should use time out after 30 seconds by default" , async ( ) => {
166
187
const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) ;
167
- fetchMock . mockResponse ( responseString ) ;
188
+ fetchMock . mockResponse ( successResponse ) ;
168
189
169
190
await fetcher ( query , {
170
191
myVar : "baz" ,
@@ -182,7 +203,7 @@ describe("gqlClientFetch", () => {
182
203
defaultTimeout : 1 ,
183
204
} ) ;
184
205
const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) ;
185
- fetchMock . mockResponse ( responseString ) ;
206
+ fetchMock . mockResponse ( successResponse ) ;
186
207
187
208
await fetcher ( query , {
188
209
myVar : "baz" ,
@@ -198,7 +219,7 @@ describe("gqlClientFetch", () => {
198
219
199
220
it ( "should use the provided signal" , async ( ) => {
200
221
const fetcher = initClientFetcher ( "https://localhost/graphql" ) ;
201
- fetchMock . mockResponse ( responseString ) ;
222
+ fetchMock . mockResponse ( successResponse ) ;
202
223
203
224
const controller = new AbortController ( ) ;
204
225
await fetcher (
@@ -221,7 +242,7 @@ describe("gqlClientFetch", () => {
221
242
} ) ;
222
243
223
244
it ( "should allow passing extra HTTP headers" , async ( ) => {
224
- const mockedFetch = fetchMock . mockResponse ( responseString ) ;
245
+ const mockedFetch = fetchMock . mockResponse ( successResponse ) ;
225
246
const gqlResponse = await fetcher (
226
247
query ,
227
248
{
@@ -264,3 +285,33 @@ describe("gqlClientFetch", () => {
264
285
) ;
265
286
} ) ;
266
287
} ) ;
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