@@ -78,13 +78,14 @@ describe('utils', () => {
78
78
79
79
describe ( 'when passing valid args' , ( ) => {
80
80
test ( 'send a JSON-RPC payload to score-api' , async ( ) => {
81
+ const result = { result : 'OK' } ;
81
82
fetch . mockReturnValue ( {
82
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
83
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
83
84
} ) ;
84
85
85
86
expect ( _validate ( { } ) ) . resolves ;
86
87
expect ( fetch ) . toHaveBeenCalledWith (
87
- 'https://score.snapshot.org' ,
88
+ 'https://score.snapshot.org/ ' ,
88
89
expect . objectContaining ( {
89
90
body : JSON . stringify ( {
90
91
jsonrpc : '2.0' ,
@@ -96,13 +97,14 @@ describe('utils', () => {
96
97
} ) ;
97
98
98
99
test ( 'send a POST request with JSON content-type' , async ( ) => {
100
+ const result = { result : 'OK' } ;
99
101
fetch . mockReturnValue ( {
100
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
102
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
101
103
} ) ;
102
104
103
105
expect ( _validate ( { } ) ) . resolves ;
104
106
expect ( fetch ) . toHaveBeenCalledWith (
105
- 'https://score.snapshot.org' ,
107
+ 'https://score.snapshot.org/ ' ,
106
108
expect . objectContaining ( {
107
109
method : 'POST' ,
108
110
headers : {
@@ -114,23 +116,28 @@ describe('utils', () => {
114
116
} ) ;
115
117
116
118
test ( 'can customize the score-api url' , ( ) => {
119
+ const result = { result : 'OK' } ;
117
120
fetch . mockReturnValue ( {
118
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
121
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
119
122
} ) ;
120
123
121
124
expect (
122
125
_validate ( { options : { url : 'https://snapshot.org/?apiKey=xxx' } } )
123
126
) . resolves ;
124
127
expect ( fetch ) . toHaveBeenCalledWith (
125
- 'https://snapshot.org/?apiKey=xxx' ,
126
- expect . anything ( )
128
+ 'https://snapshot.org/' ,
129
+ expect . objectContaining ( {
130
+ headers : expect . objectContaining ( {
131
+ 'X-API-KEY' : 'xxx'
132
+ } )
133
+ } )
127
134
) ;
128
135
} ) ;
129
136
130
137
test ( 'returns the JSON-RPC result property' , ( ) => {
131
138
const result = { result : 'OK' } ;
132
139
fetch . mockReturnValue ( {
133
- json : ( ) => new Promise ( ( resolve ) => resolve ( result ) )
140
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
134
141
} ) ;
135
142
136
143
expect ( _validate ( { } ) ) . resolves . toEqual ( 'OK' ) ;
@@ -141,7 +148,7 @@ describe('utils', () => {
141
148
test ( 'rejects with the JSON-RPC error object' , ( ) => {
142
149
const result = { error : { message : 'Oh no' } } ;
143
150
fetch . mockReturnValue ( {
144
- json : ( ) => new Promise ( ( resolve ) => resolve ( result ) )
151
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
145
152
} ) ;
146
153
147
154
expect ( _validate ( { } ) ) . rejects . toEqual ( result . error ) ;
@@ -152,7 +159,7 @@ describe('utils', () => {
152
159
test ( 'rejects with the error' , ( ) => {
153
160
const result = new Error ( 'Oh no' ) ;
154
161
fetch . mockReturnValue ( {
155
- json : ( ) => {
162
+ text : ( ) => {
156
163
throw result ;
157
164
}
158
165
} ) ;
@@ -196,7 +203,7 @@ describe('utils', () => {
196
203
network ?? payload . network ,
197
204
addresses ?? payload . addresses ,
198
205
snapshot ?? payload . snapshot ,
199
- scoreApiUrl ?? 'https://score.snapshot.org' ,
206
+ scoreApiUrl ?? 'https://score.snapshot.org/ ' ,
200
207
options ?? { }
201
208
) ;
202
209
}
@@ -239,8 +246,9 @@ describe('utils', () => {
239
246
240
247
describe ( 'when passing valid args' , ( ) => {
241
248
test ( 'send a JSON-RPC payload to score-api' , async ( ) => {
249
+ const result = { result : 'OK' } ;
242
250
fetch . mockReturnValue ( {
243
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
251
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
244
252
} ) ;
245
253
246
254
expect ( _getScores ( { } ) ) . resolves ;
@@ -253,8 +261,9 @@ describe('utils', () => {
253
261
} ) ;
254
262
255
263
test ( 'send a POST request with JSON content-type' , async ( ) => {
264
+ const result = { result : 'OK' } ;
256
265
fetch . mockReturnValue ( {
257
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
266
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
258
267
} ) ;
259
268
260
269
expect ( _getScores ( { } ) ) . resolves ;
@@ -270,23 +279,29 @@ describe('utils', () => {
270
279
) ;
271
280
} ) ;
272
281
273
- test ( 'can customize the score-api url' , ( ) => {
282
+ test ( 'can customize the score-api url and if apiKey should be passed in headers' , ( ) => {
283
+ const result = { result : 'OK' } ;
274
284
fetch . mockReturnValue ( {
275
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
285
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
276
286
} ) ;
277
287
278
288
expect ( _getScores ( { scoreApiUrl : 'https://snapshot.org/?apiKey=xxx' } ) )
279
289
. resolves ;
280
290
expect ( fetch ) . toHaveBeenCalledWith (
281
- 'https://snapshot.org/api/scores?apiKey=xxx' ,
282
- expect . anything ( )
291
+ 'https://snapshot.org/api/scores' ,
292
+ expect . objectContaining ( {
293
+ headers : expect . objectContaining ( {
294
+ 'X-API-KEY' : 'xxx'
295
+ } )
296
+ } )
283
297
) ;
284
298
} ) ;
285
299
286
300
test ( 'returns the JSON-RPC result scores property' , ( ) => {
287
301
const result = { scores : 'SCORES' , other : 'Other' } ;
288
302
fetch . mockReturnValue ( {
289
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result } ) )
303
+ text : ( ) =>
304
+ new Promise ( ( resolve ) => resolve ( JSON . stringify ( { result } ) ) )
290
305
} ) ;
291
306
292
307
expect ( _getScores ( { } ) ) . resolves . toEqual ( 'SCORES' ) ;
@@ -295,7 +310,8 @@ describe('utils', () => {
295
310
test ( 'returns the JSON-RPC all properties' , ( ) => {
296
311
const result = { scores : 'SCORES' , other : 'Other' } ;
297
312
fetch . mockReturnValue ( {
298
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result } ) )
313
+ text : ( ) =>
314
+ new Promise ( ( resolve ) => resolve ( JSON . stringify ( { result } ) ) )
299
315
} ) ;
300
316
301
317
expect (
@@ -308,7 +324,7 @@ describe('utils', () => {
308
324
test ( 'rejects with the JSON-RPC error object' , ( ) => {
309
325
const result = { error : { message : 'Oh no' } } ;
310
326
fetch . mockReturnValue ( {
311
- json : ( ) => new Promise ( ( resolve ) => resolve ( result ) )
327
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
312
328
} ) ;
313
329
314
330
expect ( _getScores ( { } ) ) . rejects . toEqual ( result . error ) ;
@@ -319,7 +335,7 @@ describe('utils', () => {
319
335
test ( 'rejects with the error' , ( ) => {
320
336
const result = new Error ( 'Oh no' ) ;
321
337
fetch . mockReturnValue ( {
322
- json : ( ) => {
338
+ text : ( ) => {
323
339
throw result ;
324
340
}
325
341
} ) ;
@@ -391,13 +407,14 @@ describe('utils', () => {
391
407
392
408
describe ( 'when passing valid args' , ( ) => {
393
409
test ( 'send a JSON-RPC payload to score-api' , async ( ) => {
410
+ const result = { result : 'OK' } ;
394
411
fetch . mockReturnValue ( {
395
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
412
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
396
413
} ) ;
397
414
398
415
expect ( _getVp ( { } ) ) . resolves ;
399
416
expect ( fetch ) . toHaveBeenCalledWith (
400
- 'https://score.snapshot.org' ,
417
+ 'https://score.snapshot.org/ ' ,
401
418
expect . objectContaining ( {
402
419
body : JSON . stringify ( {
403
420
jsonrpc : '2.0' ,
@@ -409,13 +426,14 @@ describe('utils', () => {
409
426
} ) ;
410
427
411
428
test ( 'send a POST request with JSON content-type' , async ( ) => {
429
+ const result = { result : 'OK' } ;
412
430
fetch . mockReturnValue ( {
413
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
431
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
414
432
} ) ;
415
433
416
434
expect ( _getVp ( { } ) ) . resolves ;
417
435
expect ( fetch ) . toHaveBeenCalledWith (
418
- 'https://score.snapshot.org' ,
436
+ 'https://score.snapshot.org/ ' ,
419
437
expect . objectContaining ( {
420
438
method : 'POST' ,
421
439
headers : {
@@ -427,21 +445,23 @@ describe('utils', () => {
427
445
} ) ;
428
446
429
447
test ( 'can customize the score-api url' , ( ) => {
448
+ const result = { result : 'OK' } ;
430
449
fetch . mockReturnValue ( {
431
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result : 'OK' } ) )
450
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
432
451
} ) ;
433
452
434
453
expect ( _getVp ( { options : { url : 'https://snapshot.org' } } ) ) . resolves ;
435
454
expect ( fetch ) . toHaveBeenCalledWith (
436
- 'https://snapshot.org' ,
455
+ 'https://snapshot.org/ ' ,
437
456
expect . anything ( )
438
457
) ;
439
458
} ) ;
440
459
441
460
test ( 'returns the JSON-RPC result property' , ( ) => {
442
461
const result = { data : 'OK' } ;
443
462
fetch . mockReturnValue ( {
444
- json : ( ) => new Promise ( ( resolve ) => resolve ( { result } ) )
463
+ text : ( ) =>
464
+ new Promise ( ( resolve ) => resolve ( JSON . stringify ( { result } ) ) )
445
465
} ) ;
446
466
447
467
expect ( _getVp ( { } ) ) . resolves . toEqual ( result ) ;
@@ -452,7 +472,7 @@ describe('utils', () => {
452
472
test ( 'rejects with the JSON-RPC error object' , ( ) => {
453
473
const result = { error : { message : 'Oh no' } } ;
454
474
fetch . mockReturnValue ( {
455
- json : ( ) => new Promise ( ( resolve ) => resolve ( result ) )
475
+ text : ( ) => new Promise ( ( resolve ) => resolve ( JSON . stringify ( result ) ) )
456
476
} ) ;
457
477
458
478
expect ( _getVp ( { } ) ) . rejects . toEqual ( result . error ) ;
@@ -463,7 +483,7 @@ describe('utils', () => {
463
483
test ( 'rejects with the error' , ( ) => {
464
484
const result = new Error ( 'Oh no' ) ;
465
485
fetch . mockReturnValue ( {
466
- json : ( ) => {
486
+ text : ( ) => {
467
487
throw result ;
468
488
}
469
489
} ) ;
0 commit comments