@@ -8,6 +8,7 @@ import { Model } from 'mongoose';
88
99import { User , UserDocument } from './entity/user.entity' ;
1010import { UserService } from './user.service' ;
11+ import { UpdateUserProfileDto } from '@shared/validation/user/dto/UpdateUserProfile.dto' ;
1112
1213const mockUserModel = {
1314 create : jest . fn ( ) ,
@@ -18,6 +19,7 @@ const mockUserModel = {
1819 exec : jest . fn ( ) ,
1920 select : jest . fn ( ) ,
2021 countDocuments : jest . fn ( ) ,
22+ findOneAndUpdate : jest . fn ( ) ,
2123} ;
2224
2325describe ( 'UserService' , ( ) => {
@@ -332,6 +334,7 @@ describe('UserService', () => {
332334 username : 'testuser' ,
333335 save : jest . fn ( ) . mockReturnThis ( ) ,
334336 } as unknown as UserDocument ;
337+
335338 const body = { username : 'newuser' } ;
336339
337340 jest . spyOn ( service , 'usernameExists' ) . mockResolvedValue ( false ) ;
@@ -358,4 +361,72 @@ describe('UserService', () => {
358361 ) ;
359362 } ) ;
360363 } ) ;
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+ } ) ;
361432} ) ;
0 commit comments