2
2
// ========================================================
3
3
import faker from 'faker' ;
4
4
import jwtDecode from 'jwt-decode' ;
5
- import { User } from '@prisma/client' ;
6
- import axios , { AxiosRequestConfig } from 'axios' ;
5
+ import axios from 'axios' ;
7
6
import formData from 'form-data' ;
7
+ import bcrypt from 'bcrypt' ;
8
+ import { User } from '@prisma/client' ;
8
9
import {
9
10
hashPassword ,
10
11
getGeneratedToken ,
@@ -25,11 +26,11 @@ import {
25
26
26
27
// Mocks
27
28
// ========================================================
28
- const axiosPost = jest . fn ( ) ;
29
- jest . mock ( 'axios' ) ;
30
- ( axios as jest . Mocked < typeof axios > ) . post . mockImplementation (
31
- jest . fn ( ) . mockImplementation ( axiosPost ) ,
32
- ) ;
29
+ jest . mock ( 'axios' , ( ) => {
30
+ return Object . assign ( jest . fn ( ) , {
31
+ post : jest . fn ( ) . mockReturnValue ( { success : true } ) ,
32
+ } ) ;
33
+ } ) ;
33
34
34
35
const formDataAppend = jest . fn ( ) ;
35
36
const formDataGetHeaders = jest . fn ( ) ;
361
362
const subject = 'my subject' ;
362
363
const body = 'hello there!' ;
363
364
const basicAuth = Buffer . from ( `api:secret` ) . toString ( 'base64' ) ;
365
+ const spyOnAxiosPost = jest . spyOn ( axios , 'post' ) ;
364
366
367
+ process . env . ENABLE_EMAIL = 'true' ;
365
368
process . env . MAILGUN_API_URL = 'url' ;
366
369
process . env . MAILGUN_DOMAIN = 'domain' ;
367
370
process . env . MAILGUN_SECRET_KEY = 'secret' ;
368
371
369
372
// Pre Expectations
370
373
expect ( formData ) . not . toHaveBeenCalled ( ) ;
371
- expect ( axiosPost ) . not . toHaveBeenCalled ( ) ;
374
+ expect ( spyOnAxiosPost ) . not . toBeCalled ( ) ;
372
375
373
376
// Init
374
377
const result = await sendEmail ( from , to , subject , body ) ;
388
391
expect ( formDataGetHeaders ) . toHaveBeenCalledTimes ( 1 ) ;
389
392
390
393
// Axios
391
- expect ( axiosPost ) . toHaveBeenCalledTimes ( 1 ) ;
394
+ expect ( spyOnAxiosPost ) . toBeCalledTimes ( 1 ) ;
392
395
expect ( axios . post ) . toHaveBeenCalledWith (
393
396
'url/domain/messages' ,
394
397
{ append : formDataAppend , getHeaders : formDataGetHeaders } ,
399
402
} ,
400
403
} ,
401
404
) ;
402
- expect ( result ) . toBe ( undefined ) ;
405
+ expect ( result ) . toStrictEqual ( { success : true } ) ;
403
406
} ) ;
404
407
405
408
/**
413
416
const body =
414
417
'<p>Here is the link to <a href="/asdf1234">reset your password</a>.</p><p><a href="/asdf1234">/asdf1234</a></p>.' ;
415
418
const basicAuth = Buffer . from ( `api:secret` ) . toString ( 'base64' ) ;
419
+ const spyOnAxiosPost = jest . spyOn ( axios , 'post' ) ;
416
420
421
+ process . env . ENABLE_EMAIL = 'true' ;
417
422
process . env . MAILGUN_DOMAIN = 'domain' ;
418
423
process . env . MAILGUN_SECRET_KEY = 'secret' ;
419
424
process . env . EMAIL_SUBJECT_RESET = subject ;
420
425
421
426
// Pre Expectations
422
427
expect ( formData ) . not . toHaveBeenCalled ( ) ;
423
- expect ( axiosPost ) . not . toHaveBeenCalled ( ) ;
428
+ expect ( spyOnAxiosPost ) . not . toBeCalled ( ) ;
424
429
425
430
// Init
426
431
const result = await sendResetPasswordEmail ( to , 'asdf1234' ) ;
440
445
expect ( formDataGetHeaders ) . toHaveBeenCalledTimes ( 1 ) ;
441
446
442
447
// Axios
443
- expect ( axiosPost ) . toHaveBeenCalledTimes ( 1 ) ;
448
+ expect ( spyOnAxiosPost ) . toBeCalledTimes ( 1 ) ;
444
449
expect ( axios . post ) . toHaveBeenCalledWith (
445
450
'url/domain/messages' ,
446
451
{ append : formDataAppend , getHeaders : formDataGetHeaders } ,
451
456
} ,
452
457
} ,
453
458
) ;
454
- expect ( result ) . toBe ( undefined ) ;
459
+ expect ( result ) . toStrictEqual ( { success : true } ) ;
455
460
} ) ;
456
461
457
462
/**
465
470
const body =
466
471
'<p>Here is the link to <a href="/asdf1234">confirm your account</a>.</p><p><a href="/asdf1234">/asdf1234</a></p>.' ;
467
472
const basicAuth = Buffer . from ( `api:secret` ) . toString ( 'base64' ) ;
473
+ const spyOnAxiosPost = jest . spyOn ( axios , 'post' ) ;
468
474
469
475
process . env . MAILGUN_DOMAIN = 'domain' ;
470
476
process . env . MAILGUN_SECRET_KEY = 'secret' ;
471
477
process . env . EMAIL_SUBJECT_CONFIRM = subject ;
472
478
473
479
// Pre Expectations
474
480
expect ( formData ) . not . toHaveBeenCalled ( ) ;
475
- expect ( axiosPost ) . not . toHaveBeenCalled ( ) ;
481
+ expect ( spyOnAxiosPost ) . not . toBeCalled ( ) ;
476
482
477
483
// Init
478
484
const result = await sendConfirmAccountEmail ( to , 'asdf1234' ) ;
492
498
expect ( formDataGetHeaders ) . toHaveBeenCalledTimes ( 1 ) ;
493
499
494
500
// Axios
495
- expect ( axiosPost ) . toHaveBeenCalledTimes ( 1 ) ;
501
+ expect ( spyOnAxiosPost ) . toBeCalledTimes ( 1 ) ;
496
502
expect ( axios . post ) . toHaveBeenCalledWith (
497
503
'url/domain/messages' ,
498
504
{ append : formDataAppend , getHeaders : formDataGetHeaders } ,
503
509
} ,
504
510
} ,
505
511
) ;
506
- expect ( result ) . toBe ( undefined ) ;
512
+ expect ( result ) . toStrictEqual ( { success : true } ) ;
507
513
} ) ;
0 commit comments