@@ -8,6 +8,7 @@ import { Model } from 'mongoose';
8
8
9
9
import { User , UserDocument } from './entity/user.entity' ;
10
10
import { UserService } from './user.service' ;
11
+ import { UpdateUserProfileDto } from '@shared/validation/user/dto/UpdateUserProfile.dto' ;
11
12
12
13
const mockUserModel = {
13
14
create : jest . fn ( ) ,
@@ -18,6 +19,7 @@ const mockUserModel = {
18
19
exec : jest . fn ( ) ,
19
20
select : jest . fn ( ) ,
20
21
countDocuments : jest . fn ( ) ,
22
+ findOneAndUpdate : jest . fn ( ) ,
21
23
} ;
22
24
23
25
describe ( 'UserService' , ( ) => {
@@ -332,6 +334,7 @@ describe('UserService', () => {
332
334
username : 'testuser' ,
333
335
save : jest . fn ( ) . mockReturnThis ( ) ,
334
336
} as unknown as UserDocument ;
337
+
335
338
const body = { username : 'newuser' } ;
336
339
337
340
jest . spyOn ( service , 'usernameExists' ) . mockResolvedValue ( false ) ;
@@ -358,4 +361,72 @@ describe('UserService', () => {
358
361
) ;
359
362
} ) ;
360
363
} ) ;
364
+
365
+ describe ( 'updateProfile' , ( ) => {
366
+ it ( 'should update the user profile successfully' , async ( ) => {
367
+ const user = {
368
+ _id : 'userId' ,
369
+ description : 'old description' ,
370
+ socialLinks : { } ,
371
+ username : 'oldUsername' ,
372
+ } as unknown as UserDocument ;
373
+
374
+ const body : UpdateUserProfileDto = {
375
+ description : 'new description' ,
376
+ socialLinks : { github : 'https://github.com/newuser' } ,
377
+ username : 'newUsername' ,
378
+ } ;
379
+
380
+ const updatedUser = {
381
+ ...user ,
382
+ ...body ,
383
+ } ;
384
+
385
+ jest
386
+ . spyOn ( userModel , 'findOneAndUpdate' )
387
+ . mockResolvedValue ( updatedUser as any ) ;
388
+
389
+ const result = await service . updateProfile ( user , body ) ;
390
+
391
+ expect ( result ) . toEqual ( updatedUser ) ;
392
+
393
+ expect ( userModel . findOneAndUpdate ) . toHaveBeenCalledWith (
394
+ { _id : user . _id } ,
395
+ user ,
396
+ { new : true } ,
397
+ ) ;
398
+ } ) ;
399
+
400
+ it ( 'should update only provided fields' , async ( ) => {
401
+ const user = {
402
+ _id : 'userId' ,
403
+ description : 'old description' ,
404
+ socialLinks : { } ,
405
+ username : 'oldUsername' ,
406
+ } as unknown as UserDocument ;
407
+
408
+ const body : UpdateUserProfileDto = {
409
+ description : 'new description' ,
410
+ } ;
411
+
412
+ const updatedUser = {
413
+ ...user ,
414
+ description : 'new description' ,
415
+ } ;
416
+
417
+ jest
418
+ . spyOn ( userModel , 'findOneAndUpdate' )
419
+ . mockResolvedValue ( updatedUser as any ) ;
420
+
421
+ const result = await service . updateProfile ( user , body ) ;
422
+
423
+ expect ( result ) . toEqual ( updatedUser ) ;
424
+
425
+ expect ( userModel . findOneAndUpdate ) . toHaveBeenCalledWith (
426
+ { _id : user . _id } ,
427
+ user ,
428
+ { new : true } ,
429
+ ) ;
430
+ } ) ;
431
+ } ) ;
361
432
} ) ;
0 commit comments