@@ -2,6 +2,8 @@ import { Test, TestingModule } from '@nestjs/testing';
2
2
import * as bcrypt from 'bcrypt' ;
3
3
import { UsersService } from '../models/users/users.service' ;
4
4
import { AuthService } from './auth.service' ;
5
+ import { AuthEmailsService } from '../auth-emails/auth-emails.service' ;
6
+ import { Types } from 'mongoose' ;
5
7
6
8
const MOCK_PASSWORD = 'test-password' ;
7
9
@@ -27,6 +29,7 @@ describe('AuthService', () => {
27
29
28
30
create : jest . fn ( ( data ) => {
29
31
return {
32
+ _id : new Types . ObjectId ( ) ,
30
33
profile : {
31
34
email : data . email ,
32
35
username : data . username ,
@@ -38,10 +41,11 @@ describe('AuthService', () => {
38
41
} ) ,
39
42
} ;
40
43
41
- beforeAll ( async ( ) => {
42
- mockPassword = MOCK_PASSWORD ;
43
- mockPasswordHashed = await bcrypt . hash ( mockPassword , 4 ) ;
44
- } ) ;
44
+ const mockAuthEmailService = {
45
+ sendEmailConfirmation : jest . fn ( ( ) => Promise . resolve ( ) ) ,
46
+ } ;
47
+
48
+ const sendEmailConfirmationSpy = jest . spyOn ( mockAuthEmailService , 'sendEmailConfirmation' ) ;
45
49
46
50
beforeEach ( async ( ) => {
47
51
const module : TestingModule = await Test . createTestingModule ( {
@@ -51,26 +55,36 @@ describe('AuthService', () => {
51
55
provide : UsersService ,
52
56
useValue : mockAuthService ,
53
57
} ,
58
+ {
59
+ provide : AuthEmailsService ,
60
+ useValue : mockAuthEmailService ,
61
+ } ,
54
62
] ,
55
63
} ) . compile ( ) ;
56
64
57
65
service = module . get < AuthService > ( AuthService ) ;
58
66
} ) ;
59
67
68
+ beforeAll ( async ( ) => {
69
+ mockPassword = MOCK_PASSWORD ;
70
+ mockPasswordHashed = await bcrypt . hash ( mockPassword , 4 ) ;
71
+ } ) ;
72
+
60
73
it ( 'should be defined' , ( ) => {
61
74
expect ( service ) . toBeDefined ( ) ;
62
75
} ) ;
63
76
64
- it ( 'should register user' , ( ) => {
65
- expect (
66
- service . registerUser ( {
67
-
68
- username : 'username' ,
69
- password : 'password' ,
70
- } ) ,
71
- ) . resolves . toEqual (
77
+ it ( 'should register user' , async ( ) => {
78
+ const user = await service . registerUser ( {
79
+
80
+ username : 'username' ,
81
+ password : 'password' ,
82
+ } ) ;
83
+ expect ( user ) . toEqual (
72
84
expect . objectContaining ( { profile : expect . objectContaining ( { username : 'username' } ) } ) ,
73
85
) ;
86
+ expect ( sendEmailConfirmationSpy ) . toBeCalledTimes ( 1 ) ;
87
+ expect ( sendEmailConfirmationSpy ) . toBeCalledWith ( user . _id ) ;
74
88
} ) ;
75
89
76
90
describe ( 'User validation' , ( ) => {
0 commit comments