1
+ // importing any mock file let the jest load all the mocks defined in that file
2
+ import { addHeaders } from '../../../auth/authentication/mock' ;
3
+ import {
4
+ mockUserFindByEmail , createTokensSpy , USER_EMAIL , USER_PASSWORD
5
+ } from '../login/mock' ;
6
+
7
+ // import the mock for this file below all mock imports
8
+ import { mockUserCreate , bcryptHashSpy , USER_NAME , USER_PROFILE_PIC } from './mock' ;
9
+
10
+ import supertest from 'supertest' ;
11
+ import app from '../../../../src/app' ;
12
+
13
+ describe ( 'Signup basic route' , ( ) => {
14
+
15
+ const endpoint = '/v1/signup/basic' ;
16
+ const request = supertest ( app ) ;
17
+
18
+ const email = '[email protected] ' ;
19
+
20
+ beforeEach ( ( ) => {
21
+ mockUserFindByEmail . mockClear ( ) ;
22
+ mockUserCreate . mockClear ( ) ;
23
+ bcryptHashSpy . mockClear ( ) ;
24
+ createTokensSpy . mockClear ( ) ;
25
+ } ) ;
26
+
27
+ it ( 'Should throw error when empty body is sent' , async ( ) => {
28
+ const response = await addHeaders ( request . post ( endpoint ) ) ;
29
+ expect ( response . status ) . toBe ( 400 ) ;
30
+ expect ( mockUserFindByEmail ) . not . toBeCalled ( ) ;
31
+ expect ( bcryptHashSpy ) . not . toBeCalled ( ) ;
32
+ expect ( mockUserCreate ) . not . toBeCalled ( ) ;
33
+ expect ( createTokensSpy ) . not . toBeCalled ( ) ;
34
+ } ) ;
35
+
36
+ it ( 'Should throw error when email is not sent' , async ( ) => {
37
+ const response = await addHeaders ( request . post ( endpoint )
38
+ . send ( {
39
+ name : USER_NAME ,
40
+ password : USER_PASSWORD ,
41
+ profilePicUrl : USER_PROFILE_PIC
42
+ } )
43
+ ) ;
44
+ expect ( response . status ) . toBe ( 400 ) ;
45
+ expect ( response . body . message ) . toMatch ( / e m a i l / ) ;
46
+ expect ( response . body . message ) . toMatch ( / r e q u i r e d / ) ;
47
+ expect ( mockUserFindByEmail ) . not . toBeCalled ( ) ;
48
+ expect ( bcryptHashSpy ) . not . toBeCalled ( ) ;
49
+ expect ( mockUserCreate ) . not . toBeCalled ( ) ;
50
+ expect ( createTokensSpy ) . not . toBeCalled ( ) ;
51
+ } ) ;
52
+
53
+ it ( 'Should throw error when password is not sent' , async ( ) => {
54
+ const response = await addHeaders ( request . post ( endpoint )
55
+ . send ( {
56
+ email : email ,
57
+ name : USER_NAME ,
58
+ profilePicUrl : USER_PROFILE_PIC
59
+ } )
60
+ ) ;
61
+ expect ( response . status ) . toBe ( 400 ) ;
62
+ expect ( response . body . message ) . toMatch ( / p a s s w o r d / ) ;
63
+ expect ( response . body . message ) . toMatch ( / r e q u i r e d / ) ;
64
+ expect ( mockUserFindByEmail ) . not . toBeCalled ( ) ;
65
+ expect ( bcryptHashSpy ) . not . toBeCalled ( ) ;
66
+ expect ( mockUserCreate ) . not . toBeCalled ( ) ;
67
+ expect ( createTokensSpy ) . not . toBeCalled ( ) ;
68
+ } ) ;
69
+
70
+ it ( 'Should throw error when name is not sent' , async ( ) => {
71
+ const response = await addHeaders ( request . post ( endpoint )
72
+ . send ( {
73
+ email : email ,
74
+ password : USER_PASSWORD ,
75
+ profilePicUrl : USER_PROFILE_PIC
76
+ } )
77
+ ) ;
78
+ expect ( response . status ) . toBe ( 400 ) ;
79
+ expect ( response . body . message ) . toMatch ( / n a m e / ) ;
80
+ expect ( response . body . message ) . toMatch ( / r e q u i r e d / ) ;
81
+ expect ( mockUserFindByEmail ) . not . toBeCalled ( ) ;
82
+ expect ( bcryptHashSpy ) . not . toBeCalled ( ) ;
83
+ expect ( mockUserCreate ) . not . toBeCalled ( ) ;
84
+ expect ( createTokensSpy ) . not . toBeCalled ( ) ;
85
+ } ) ;
86
+
87
+ it ( 'Should throw error when email is not valid format' , async ( ) => {
88
+ const response = await addHeaders ( request . post ( endpoint )
89
+ . send ( {
90
+ email : 'abc' ,
91
+ name : USER_NAME ,
92
+ password : USER_PASSWORD ,
93
+ profilePicUrl : USER_PROFILE_PIC
94
+ } )
95
+ ) ;
96
+ expect ( response . status ) . toBe ( 400 ) ;
97
+ expect ( response . body . message ) . toMatch ( / v a l i d e m a i l / ) ;
98
+ expect ( mockUserFindByEmail ) . not . toBeCalled ( ) ;
99
+ expect ( bcryptHashSpy ) . not . toBeCalled ( ) ;
100
+ expect ( mockUserCreate ) . not . toBeCalled ( ) ;
101
+ expect ( createTokensSpy ) . not . toBeCalled ( ) ;
102
+ } ) ;
103
+
104
+ it ( 'Should throw error when password is not valid format' , async ( ) => {
105
+ const response = await addHeaders ( request . post ( endpoint )
106
+ . send ( {
107
+ email : email ,
108
+ name : USER_NAME ,
109
+ password : '123' ,
110
+ profilePicUrl : USER_PROFILE_PIC
111
+ } )
112
+ ) ;
113
+ expect ( response . status ) . toBe ( 400 ) ;
114
+ expect ( response . body . message ) . toMatch ( / p a s s w o r d l e n g t h / ) ;
115
+ expect ( response . body . message ) . toMatch ( / 6 c h a r / ) ;
116
+ expect ( mockUserFindByEmail ) . not . toBeCalled ( ) ;
117
+ expect ( bcryptHashSpy ) . not . toBeCalled ( ) ;
118
+ expect ( mockUserCreate ) . not . toBeCalled ( ) ;
119
+ expect ( createTokensSpy ) . not . toBeCalled ( ) ;
120
+ } ) ;
121
+
122
+ it ( 'Should throw error when user is registered for email' , async ( ) => {
123
+ const response = await addHeaders ( request . post ( endpoint )
124
+ . send ( {
125
+ email : USER_EMAIL ,
126
+ name : USER_NAME ,
127
+ password : USER_PASSWORD ,
128
+ profilePicUrl : USER_PROFILE_PIC
129
+ } )
130
+ ) ;
131
+
132
+ expect ( response . status ) . toBe ( 400 ) ;
133
+ expect ( response . body . message ) . toMatch ( / a l r e a d y r e g i s t e r e d / ) ;
134
+ expect ( mockUserFindByEmail ) . toBeCalledTimes ( 1 ) ;
135
+ expect ( bcryptHashSpy ) . not . toBeCalled ( ) ;
136
+ expect ( mockUserCreate ) . not . toBeCalled ( ) ;
137
+ expect ( createTokensSpy ) . not . toBeCalled ( ) ;
138
+ } ) ;
139
+
140
+ it ( 'Should send success response for correct data' , async ( ) => {
141
+ const response = await addHeaders ( request . post ( endpoint )
142
+ . send ( {
143
+ email : email ,
144
+ name : USER_NAME ,
145
+ password : USER_PASSWORD ,
146
+ profilePicUrl : USER_PROFILE_PIC
147
+ } )
148
+ ) ;
149
+ expect ( response . status ) . toBe ( 200 ) ;
150
+ expect ( response . body . message ) . toMatch ( / S u c c e s s / i) ;
151
+ expect ( response . body . data ) . toBeDefined ( ) ;
152
+
153
+ expect ( response . body . data . user ) . toHaveProperty ( '_id' ) ;
154
+ expect ( response . body . data . user ) . toHaveProperty ( 'name' ) ;
155
+ expect ( response . body . data . user ) . toHaveProperty ( 'roles' ) ;
156
+ expect ( response . body . data . user ) . toHaveProperty ( 'profilePicUrl' ) ;
157
+
158
+ expect ( response . body . data . tokens ) . toBeDefined ( ) ;
159
+ expect ( response . body . data . tokens ) . toHaveProperty ( 'accessToken' ) ;
160
+ expect ( response . body . data . tokens ) . toHaveProperty ( 'refreshToken' ) ;
161
+
162
+ expect ( mockUserFindByEmail ) . toBeCalledTimes ( 1 ) ;
163
+ expect ( bcryptHashSpy ) . toBeCalledTimes ( 1 ) ;
164
+ expect ( mockUserCreate ) . toBeCalledTimes ( 1 ) ;
165
+ expect ( createTokensSpy ) . toBeCalledTimes ( 1 ) ;
166
+
167
+ expect ( bcryptHashSpy ) . toBeCalledWith ( USER_PASSWORD , 10 ) ;
168
+ } ) ;
169
+ } ) ;
0 commit comments