1
- import axios from 'axios ' ;
1
+ import fetchMock from 'jest-fetch-mock ' ;
2
2
3
3
import QuickChart from '../src/index' ;
4
4
5
- jest . mock ( 'axios' ) ;
6
-
7
5
test ( 'basic chart, no auth' , ( ) => {
8
6
const qc = new QuickChart ( ) ;
9
7
qc . setConfig ( {
@@ -249,82 +247,100 @@ test('postdata for js chart', () => {
249
247
250
248
test ( 'getShortUrl for chart, no auth' , async ( ) => {
251
249
const mockResp = {
252
- status : 200 ,
253
- data : {
254
- success : true ,
255
- url : 'https://quickchart.io/chart/render/9a560ba4-ab71-4d1e-89ea-ce4741e9d232' ,
256
- } ,
250
+ success : true ,
251
+ url : 'https://quickchart.io/chart/render/9a560ba4-ab71-4d1e-89ea-ce4741e9d232' ,
257
252
} ;
258
- ( axios . post as jest . Mock ) . mockImplementationOnce ( ( ) => Promise . resolve ( mockResp ) ) ;
253
+ fetchMock . mockResponseOnce ( JSON . stringify ( mockResp ) ) ;
254
+
255
+ const qc = new QuickChart ( ) ;
256
+ qc . setConfig ( {
257
+ type : 'bar' ,
258
+ data : { labels : [ 'Hello world' , 'Foo bar' ] , datasets : [ { label : 'Foo' , data : [ 1 , 2 ] } ] } ,
259
+ } ) ;
260
+
261
+ await expect ( qc . getShortUrl ( ) ) . resolves . toEqual ( mockResp . url ) ;
262
+ } ) ;
263
+
264
+ test ( 'getShortUrl for chart js error' , async ( ) => {
265
+ fetchMock . mockResponseOnce ( ( ) => {
266
+ throw new Error ( 'Request timed out' ) ;
267
+ } ) ;
259
268
260
269
const qc = new QuickChart ( ) ;
261
270
qc . setConfig ( {
262
271
type : 'bar' ,
263
272
data : { labels : [ 'Hello world' , 'Foo bar' ] , datasets : [ { label : 'Foo' , data : [ 1 , 2 ] } ] } ,
264
273
} ) ;
265
274
266
- await expect ( qc . getShortUrl ( ) ) . resolves . toEqual ( mockResp . data . url ) ;
267
- expect ( axios . post ) . toHaveBeenCalled ( ) ;
275
+ await expect ( qc . getShortUrl ( ) ) . rejects . toThrow ( 'Request timed out' ) ;
268
276
} ) ;
269
277
270
278
test ( 'getShortUrl for chart bad status code' , async ( ) => {
271
- const mockResp = {
279
+ fetchMock . mockResponseOnce ( '' , {
272
280
status : 502 ,
273
- } ;
274
- ( axios . post as jest . Mock ) . mockImplementationOnce ( ( ) => Promise . resolve ( mockResp ) ) ;
281
+ } ) ;
282
+
283
+ const qc = new QuickChart ( ) ;
284
+ qc . setConfig ( {
285
+ type : 'bar' ,
286
+ data : { labels : [ 'Hello world' , 'Foo bar' ] , datasets : [ { label : 'Foo' , data : [ 1 , 2 ] } ] } ,
287
+ } ) ;
288
+
289
+ await expect ( qc . getShortUrl ( ) ) . rejects . toThrow ( 'failed with status code' ) ;
290
+ } ) ;
291
+
292
+ test ( 'getShortUrl for chart bad status code with error detail' , async ( ) => {
293
+ fetchMock . mockResponseOnce ( '' , {
294
+ status : 400 ,
295
+ headers : {
296
+ 'x-quickchart-error' : 'foo bar' ,
297
+ } ,
298
+ } ) ;
275
299
276
300
const qc = new QuickChart ( ) ;
277
301
qc . setConfig ( {
278
302
type : 'bar' ,
279
303
data : { labels : [ 'Hello world' , 'Foo bar' ] , datasets : [ { label : 'Foo' , data : [ 1 , 2 ] } ] } ,
280
304
} ) ;
281
305
282
- await expect ( qc . getShortUrl ( ) ) . rejects . toContain ( 'Bad response code' ) ;
283
- expect ( axios . post ) . toHaveBeenCalled ( ) ;
306
+ await expect ( qc . getShortUrl ( ) ) . rejects . toThrow ( 'foo bar' ) ;
284
307
} ) ;
285
308
286
309
test ( 'getShortUrl api failure' , async ( ) => {
287
- const mockResp = {
288
- status : 200 ,
289
- data : {
310
+ fetchMock . mockResponseOnce (
311
+ JSON . stringify ( {
290
312
success : false ,
291
- } ,
292
- } ;
293
- ( axios . post as jest . Mock ) . mockImplementationOnce ( ( ) => Promise . resolve ( mockResp ) ) ;
313
+ } ) ,
314
+ ) ;
294
315
295
316
const qc = new QuickChart ( ) ;
296
317
qc . setConfig ( {
297
318
type : 'bar' ,
298
319
data : { labels : [ 'Hello world' , 'Foo bar' ] , datasets : [ { label : 'Foo' , data : [ 1 , 2 ] } ] } ,
299
320
} ) ;
300
321
301
- await expect ( qc . getShortUrl ( ) ) . rejects . toContain ( 'failure response' ) ;
302
- expect ( axios . post ) . toHaveBeenCalled ( ) ;
322
+ await expect ( qc . getShortUrl ( ) ) . rejects . toThrow ( 'failure response' ) ;
323
+ expect ( fetch ) . toHaveBeenCalled ( ) ;
303
324
} ) ;
304
325
305
326
test ( 'toBinary, no auth' , async ( ) => {
306
- const mockResp = {
307
- status : 200 ,
308
- data : Buffer . from ( 'bWVvdw==' , 'base64' ) ,
309
- } ;
310
- ( axios . post as jest . Mock ) . mockImplementationOnce ( ( ) => Promise . resolve ( mockResp ) ) ;
327
+ const mockData = Buffer . from ( 'bWVvdw==' , 'base64' ) ;
328
+ // https://github.com/jefflau/jest-fetch-mock/issues/218
329
+ fetchMock . mockResponseOnce ( ( ) => Promise . resolve ( { body : mockData as unknown as string } ) ) ;
311
330
312
331
const qc = new QuickChart ( ) ;
313
332
qc . setConfig ( {
314
333
type : 'bar' ,
315
334
data : { labels : [ 'Hello world' , 'Foo bar' ] , datasets : [ { label : 'Foo' , data : [ 1 , 2 ] } ] } ,
316
335
} ) ;
317
336
318
- await expect ( qc . toBinary ( ) ) . resolves . toEqual ( mockResp . data ) ;
319
- expect ( axios . post ) . toHaveBeenCalled ( ) ;
337
+ await expect ( qc . toBinary ( ) ) . resolves . toEqual ( mockData ) ;
320
338
} ) ;
321
339
322
- test ( 'toBinary, no auth' , async ( ) => {
323
- const mockResp = {
324
- status : 200 ,
325
- data : Buffer . from ( 'bWVvdw==' , 'base64' ) ,
326
- } ;
327
- ( axios . post as jest . Mock ) . mockImplementationOnce ( ( ) => Promise . resolve ( mockResp ) ) ;
340
+ test ( 'toDataUrl, no auth' , async ( ) => {
341
+ fetchMock . mockResponseOnce ( ( ) =>
342
+ Promise . resolve ( { body : Buffer . from ( 'bWVvdw==' , 'base64' ) as unknown as string } ) ,
343
+ ) ;
328
344
329
345
const qc = new QuickChart ( ) ;
330
346
qc . setConfig ( {
@@ -333,7 +349,6 @@ test('toBinary, no auth', async () => {
333
349
} ) ;
334
350
335
351
await expect ( qc . toDataUrl ( ) ) . resolves . toEqual ( 'data:image/png;base64,bWVvdw==' ) ;
336
- expect ( axios . post ) . toHaveBeenCalled ( ) ;
337
352
} ) ;
338
353
339
354
test ( 'no chart specified throws error' , async ( ) => {
0 commit comments