1+ // Setup mocks for UserController 
2+ jest . mock ( '../controllers/user.controller' ) ; 
3+ const  {  UserController }  =  require ( '../controllers' ) ; 
4+ 
5+ // Must import usersRouter after setting up mocks for UserController 
6+ const  usersRouter  =  require ( './users.router' ) ; 
7+ const  express  =  require ( 'express' ) ; 
8+ const  supertest  =  require ( 'supertest' ) ; 
9+ 
10+ // Setup testapp with just usersRouter which calls mocked UserController 
11+ const  testapp  =  express ( ) ; 
12+ testapp . use ( express . json ( ) ) ; 
13+ testapp . use ( express . urlencoded ( {  extended : false  } ) ) ; 
14+ testapp . use ( '/api/users' ,  usersRouter ) ; 
15+ const  request  =  supertest ( testapp ) ; 
16+ 
17+ describe ( 'Unit Tests for userRouter' ,  ( )  =>  { 
18+     // Mocked user data 
19+     const  mockUser  =  { 
20+         name : { 
21+             firstName : 'test' , 
22+             lastName : 'user' , 
23+         } , 
24+         email : 'newtest@test.com' , 
25+     } ; 
26+     const  mockId  =  '12345' ; 
27+     const  mockUpdatedEmail  =  { 
28+         email : 'newtest@test.com' , 
29+     } ; 
30+ 
31+     afterEach ( ( )  =>  { 
32+         jest . clearAllMocks ( ) ; 
33+     } ) ; 
34+ 
35+     describe ( 'CREATE' ,  ( )  =>  { 
36+         it ( 'should create a User through the UserController' ,  async  ( done )  =>  { 
37+             //Setup 
38+             //Mock the UserController function that this route calls with expected results 
39+             UserController . create . mockImplementationOnce ( 
40+                 ( req ,  res )  =>  {  return  res . status ( 201 ) . send ( mockUser )  } 
41+             ) ; 
42+     
43+             //Functionality 
44+             //Post mockUser to CREATE API Endpoint 
45+             const  response  =  await  request 
46+                 . post ( '/api/users/' ) 
47+                 . send ( mockUser ) ; 
48+ 
49+             //Test 
50+             expect ( UserController . create ) . toHaveBeenCalledWith ( 
51+                 expect . objectContaining ( { body : mockUser } ) , 
52+                 expect . anything ( ) ,  // Mock the response object 
53+                 expect . anything ( )  // Mock the next function 
54+             ) ; 
55+             expect ( response . status ) . toBe ( 201 ) ; 
56+             expect ( response . body ) . toEqual ( mockUser ) ; 
57+ 
58+             done ( ) ; 
59+         } ) ; 
60+     } ) ; 
61+       
62+     describe ( 'READ' ,  ( )  =>  { 
63+         it ( 'should get a list of Users with with GET to /api/users/ through UserController' ,  async  ( done )  =>  { 
64+             //Setup 
65+             //Mock the UserController function that this route calls with expected results 
66+             UserController . user_list . mockImplementationOnce ( 
67+                 ( req ,  res )  =>  {  return  res . status ( 200 ) . send ( [ mockUser ] )  } 
68+             ) ; 
69+     
70+             //Functionality 
71+             //Get list of all users from READ API Endpoint 
72+             const  response  =  await  request 
73+                 . get ( '/api/users/' ) ; 
74+ 
75+             //Test 
76+             expect ( UserController . user_list ) . toHaveBeenCalled ( ) ; 
77+             expect ( response . status ) . toBe ( 200 ) ; 
78+             expect ( response . body [ 0 ] ) . toEqual ( mockUser ) ; 
79+ 
80+             done ( ) ; 
81+         } ) ; 
82+ 
83+         it ( 'should get a specific User by param with GET to /api/users?email=<query> through UserController' ,  async  ( done )  =>  { 
84+             //Setup 
85+             //Mock the UserController function that this route calls with expected results 
86+             UserController . user_list . mockImplementationOnce ( 
87+                 ( req ,  res )  =>  {  return  res . status ( 200 ) . send ( [ mockUser ] )  } 
88+             ) ; 
89+     
90+             //Functionality 
91+             //Get a user with a specific email using a query param to READ API Endpoint 
92+             const  response  =  await  request 
93+                 . get ( '/api/users?email=newtest@test.com' ) ; 
94+ 
95+             //Test 
96+             expect ( UserController . user_list ) . toHaveBeenCalled ( ) ; 
97+             expect ( response . status ) . toBe ( 200 ) ; 
98+             expect ( response . body [ 0 ] ) . toEqual ( mockUser ) ; 
99+ 
100+             done ( ) ; 
101+         } ) ; 
102+     
103+         it ( 'should get a list of Users with accessLevel of admin or superadmin with GET to /api/users/admins through UserController' ,  async  ( done )  =>  { 
104+             //Setup 
105+             //Mock the UserController function that this route calls with expected results 
106+             UserController . admin_list . mockImplementationOnce ( 
107+                 ( req ,  res )  =>  {  return  res . status ( 200 ) . send ( [ mockUser ] )  } 
108+             ) ; 
109+     
110+             //Functionality 
111+             //Get a list of admins and superadmins from READ API Endpoint for admins 
112+             const  response  =  await  request 
113+                 . get ( '/api/users/admins' ) ; 
114+ 
115+             //Test 
116+             expect ( UserController . admin_list ) . toHaveBeenCalled ( ) ; 
117+             expect ( response . status ) . toBe ( 200 ) ; 
118+             expect ( response . body [ 0 ] ) . toEqual ( mockUser ) ; 
119+ 
120+             done ( ) ; 
121+         } ) ; 
122+     
123+         it ( 'should get a list of Users with the ability to manage projects with GET to /api/users/projectManagers through UserController' ,  async  ( done )  =>  { 
124+             //Setup 
125+             //Mock the UserController function that this route calls with expected results 
126+             UserController . projectLead_list . mockImplementationOnce ( 
127+                 ( req ,  res )  =>  {  return  res . status ( 200 ) . send ( [ mockUser ] )  } 
128+             ) ; 
129+     
130+             //Functionality 
131+             //Get a list of project leads and admins from READ API Endpoint for project leads 
132+             const  response  =  await  request 
133+                 . get ( '/api/users/projectManagers' ) ; 
134+ 
135+             //Test 
136+             expect ( UserController . projectLead_list ) . toHaveBeenCalled ( ) ; 
137+             expect ( response . status ) . toBe ( 200 ) ; 
138+             expect ( response . body [ 0 ] ) . toEqual ( mockUser ) ; 
139+ 
140+             done ( ) ; 
141+         } ) ; 
142+     
143+         it ( 'should get a specific User by UserId with GET to /api/users/:UserId through UserController' ,  async  ( done )  =>  { 
144+             //Setup 
145+             //Mock the UserController function that this route calls with expected results 
146+             UserController . user_by_id . mockImplementationOnce ( 
147+                 ( req ,  res )  =>  {  return  res . status ( 200 ) . send ( mockUser )  } 
148+             ) ; 
149+     
150+             //Functionality 
151+             //Get a specific user from READ API Endpoint for specific UUIDs 
152+             const  response  =  await  request 
153+                 . get ( `/api/users/${ mockId }  ) ; 
154+ 
155+             //Test 
156+             expect ( UserController . user_by_id ) . toHaveBeenCalled ( ) ; 
157+             expect ( response . status ) . toBe ( 200 ) ; 
158+             expect ( response . body ) . toEqual ( mockUser ) ; 
159+ 
160+             done ( ) ; 
161+         } ) ; 
162+     } ) ; 
163+     
164+     describe ( 'UPDATE' ,  ( )  =>  { 
165+         it ( 'should update a User with PATCH to /api/users/:UserId through UserController' ,  async  ( done )  =>  { 
166+             //Setup 
167+             //Mock the UserController function that this route calls with expected results 
168+             UserController . update . mockImplementationOnce ( 
169+                 ( req ,  res )  =>  {  return  res . status ( 200 ) . send ( mockUser )  } 
170+             ) ; 
171+     
172+             //Functionality 
173+             //Patch a user with a specific id by sending new user data to UPDATE API Endpoint 
174+             const  response  =  await  request 
175+                 . patch ( `/api/users/${ mockId }  ) 
176+                 . send ( mockUpdatedEmail ) ; 
177+ 
178+             //Test 
179+             expect ( UserController . update ) . toHaveBeenCalled ( ) ; 
180+             expect ( response . status ) . toBe ( 200 ) ; 
181+             expect ( response . body ) . toEqual ( mockUser ) ; 
182+ 
183+             done ( ) ; 
184+         } ) ; 
185+     } ) ; 
186+     
187+     describe ( 'DELETE' ,  ( )  =>  { 
188+         it ( 'should delete a specific user by Id with DELETE /api/users/:UserId through UserController' ,  async  ( done )  =>  { 
189+             //Setup 
190+             //Mock the UserController function that this route calls with expected results 
191+             UserController . delete . mockImplementationOnce ( 
192+                 ( req ,  res )  =>  {  return  res . status ( 200 ) . send ( mockUser )  } 
193+             ) ; 
194+     
195+             //Delete user with a specific id via a request to DELETE API Endpoint 
196+             const  response  =  await  request 
197+                 . delete ( `/api/users/${ mockId }  ) 
198+                 . send ( mockUpdatedEmail ) ; 
199+ 
200+             //Test 
201+             expect ( UserController . delete ) . toHaveBeenCalled ( ) ; 
202+             expect ( response . status ) . toBe ( 200 ) ; 
203+             expect ( response . body ) . toEqual ( mockUser ) ; 
204+ 
205+             done ( ) ; 
206+         } ) ; 
207+     } ) ; 
208+ } ) ; 
0 commit comments