@@ -5,6 +5,8 @@ import { Signer, Credentials, DateUtils } from '@aws-amplify/core';
5
5
6
6
jest . mock ( 'axios' ) ;
7
7
8
+ const mockAxios = jest . spyOn ( axios as any , 'default' ) ;
9
+
8
10
axios . CancelToken = < CancelTokenStatic > {
9
11
source : ( ) => ( { token : null , cancel : null } ) ,
10
12
} ;
@@ -28,6 +30,7 @@ const config = {
28
30
29
31
afterEach ( ( ) => {
30
32
jest . restoreAllMocks ( ) ;
33
+ mockAxios . mockClear ( ) ;
31
34
} ) ;
32
35
33
36
describe ( 'Rest API test' , ( ) => {
@@ -166,19 +169,11 @@ describe('Rest API test', () => {
166
169
api . configure ( custom_config ) ;
167
170
const spyon = jest
168
171
. spyOn ( Credentials , 'get' )
169
- . mockImplementationOnce ( ( ) => {
170
- return new Promise ( ( res , rej ) => {
171
- res ( 'cred' ) ;
172
- } ) ;
173
- } ) ;
172
+ . mockResolvedValueOnce ( 'cred' ) ;
174
173
175
174
const spyonRequest = jest
176
175
. spyOn ( RestClient . prototype as any , '_request' )
177
- . mockImplementationOnce ( ( ) => {
178
- return new Promise ( ( res , rej ) => {
179
- res ( { } ) ;
180
- } ) ;
181
- } ) ;
176
+ . mockResolvedValueOnce ( { } ) ;
182
177
await api . get ( 'apiName' , 'path' , { } ) ;
183
178
184
179
expect ( spyonRequest ) . toBeCalledWith (
@@ -221,25 +216,15 @@ describe('Rest API test', () => {
221
216
session_token : 'token' ,
222
217
} ;
223
218
224
- const spyon = jest . spyOn ( Credentials , 'get' ) . mockImplementation ( ( ) => {
225
- return new Promise ( ( res , rej ) => {
226
- res ( creds ) ;
227
- } ) ;
228
- } ) ;
219
+ const spyon = jest . spyOn ( Credentials , 'get' ) . mockResolvedValue ( creds ) ;
229
220
230
221
const spyonSigner = jest
231
222
. spyOn ( Signer , 'sign' )
232
223
. mockImplementationOnce ( ( ) => {
233
224
return { headers : { } } ;
234
225
} ) ;
235
226
236
- const spyAxios = jest
237
- . spyOn ( axios as any , 'default' )
238
- . mockImplementationOnce ( ( ) => {
239
- return new Promise ( ( res , rej ) => {
240
- res ( resp ) ;
241
- } ) ;
242
- } ) ;
227
+ mockAxios . mockResolvedValue ( resp ) ;
243
228
244
229
const init = {
245
230
timeout : 2500 ,
@@ -297,13 +282,7 @@ describe('Rest API test', () => {
297
282
return { headers : { } } ;
298
283
} ) ;
299
284
300
- const spyAxios = jest
301
- . spyOn ( axios as any , 'default' )
302
- . mockImplementationOnce ( ( ) => {
303
- return new Promise ( ( res , rej ) => {
304
- res ( resp ) ;
305
- } ) ;
306
- } ) ;
285
+ mockAxios . mockResolvedValue ( resp ) ;
307
286
308
287
const init = {
309
288
queryStringParameters : {
@@ -363,13 +342,7 @@ describe('Rest API test', () => {
363
342
return { headers : { } } ;
364
343
} ) ;
365
344
366
- const spyAxios = jest
367
- . spyOn ( axios as any , 'default' )
368
- . mockImplementationOnce ( ( ) => {
369
- return new Promise ( ( res , rej ) => {
370
- res ( resp ) ;
371
- } ) ;
372
- } ) ;
345
+ mockAxios . mockResolvedValue ( resp ) ;
373
346
374
347
const init = {
375
348
queryStringParameters : {
@@ -429,13 +402,7 @@ describe('Rest API test', () => {
429
402
return { headers : { } } ;
430
403
} ) ;
431
404
432
- const spyAxios = jest
433
- . spyOn ( axios as any , 'default' )
434
- . mockImplementationOnce ( ( ) => {
435
- return new Promise ( ( res , rej ) => {
436
- res ( resp ) ;
437
- } ) ;
438
- } ) ;
405
+ mockAxios . mockResolvedValue ( resp ) ;
439
406
440
407
const init = {
441
408
queryStringParameters : {
@@ -557,19 +524,31 @@ describe('Rest API test', () => {
557
524
. mockImplementation ( ( ) => 'endpoint' ) ;
558
525
559
526
jest . spyOn ( Credentials , 'get' ) . mockResolvedValue ( 'creds' ) ;
560
-
561
- jest
562
- . spyOn ( RestClient . prototype as any , '_signed' )
563
- . mockRejectedValueOnce ( normalError ) ;
527
+ jest . spyOn ( RestClient . prototype as any , '_sign' ) . mockReturnValue ( {
528
+ ...init ,
529
+ headers : { ...init . headers , Authorization : 'signed' } ,
530
+ } ) ;
531
+ mockAxios . mockImplementationOnce ( ( ) => {
532
+ return new Promise ( ( _ , rej ) => {
533
+ rej ( normalError ) ;
534
+ } ) ;
535
+ } ) ;
564
536
565
537
await expect ( api . post ( 'url' , 'path' , init ) ) . rejects . toThrow ( normalError ) ;
566
538
567
539
// Clock should not be skewed from normal errors
568
540
expect ( DateUtils . getClockOffset ( ) ) . toBe ( 0 ) ;
569
541
570
- jest
571
- . spyOn ( RestClient . prototype as any , '_signed' )
572
- . mockRejectedValueOnce ( clockSkewError ) ;
542
+ // mock clock skew error response and successful response after retry
543
+ mockAxios
544
+ . mockImplementationOnce ( ( ) => {
545
+ return new Promise ( ( _ , rej ) => {
546
+ rej ( clockSkewError ) ;
547
+ } ) ;
548
+ } )
549
+ . mockResolvedValue ( {
550
+ data : [ { name : 'Bob' } ] ,
551
+ } ) ;
573
552
574
553
await expect ( api . post ( 'url' , 'path' , init ) ) . resolves . toEqual ( [
575
554
{ name : 'Bob' } ,
0 commit comments