import AppError from '@shared/errors/AppError' ; import FakeHashProvider from '../providers/HashProvider/fakes/FakeHashProvider' ; import FakeUsersRepository from '../repositories/fakes/FakeUsersRepository' ; import UpdateProfileService from './UpdateProfileService' ; let fakeUsersRepository ; let fakeHashProvider ; let updateProfile ; describe ( 'UpdateProfile' , ( ) => { beforeEach ( ( ) => { fakeUsersRepository = new FakeUsersRepository ( ) ; fakeHashProvider = new FakeHashProvider ( ) ; updateProfile = new UpdateProfileService ( fakeUsersRepository , fakeHashProvider ) ; } ) ; it ( 'should be able to update the profile' , async ( ) => { const user = await fakeUsersRepository . create ( { name : 'John Doe' , email : 'johndoe@example.com' , password : '123456' , } ) ; const updatedUser = await updateProfile . execute ( { user_id : user . id , name : 'John Treh' , email : 'johntreh@example.com' , } ) ; expect ( updatedUser . name ) . toBe ( 'John Treh' ) ; expect ( updatedUser . email ) . toBe ( 'johntreh@example.com' ) ; } ) ; it ( 'should NOT be able to update email to an already using email' , async ( ) => { const user = await fakeUsersRepository . create ( { name : 'John Tre' , email : 'johntre@example.com' , password : '123456' , } ) ; await fakeUsersRepository . create ( { name : 'John Doe' , email : 'johndoe@example.com' , password : '123456' , } ) ; await expect ( updateProfile . execute ( { user_id : user . id , name : 'John Treh' , email : 'johndoe@example.com' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; it ( 'should be able to update the password' , async ( ) => { const user = await fakeUsersRepository . create ( { name : 'John Doe' , email : 'johndoe@example.com' , password : '123456' , } ) ; const updatedUser = await updateProfile . execute ( { user_id : user . id , name : 'John Treh' , email : 'johntreh@example.com' , password : '123123' , old_password : '123456' , } ) ; expect ( updatedUser . password ) . toBe ( '123123' ) ; } ) ; it ( 'should NOT be able to update the password with wrong current password' , async ( ) => { const user = await fakeUsersRepository . create ( { name : 'John Doe' , email : 'johndoe@example.com' , password : '123456' , } ) ; await expect ( updateProfile . execute ( { user_id : user . id , name : 'John Treh' , email : 'johntreh@example.com' , password : '123123' , old_password : 'wrong-old-password' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; it ( 'should NOT be able to update the password without current password' , async ( ) => { const user = await fakeUsersRepository . create ( { name : 'John Doe' , email : 'johndoe@example.com' , password : '123456' , } ) ; await expect ( updateProfile . execute ( { user_id : user . id , name : 'John Treh' , email : 'johntreh@example.com' , password : '123123' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; it ( 'should NOT be able to update the profile of a non-existing user' , async ( ) => { await expect ( updateProfile . execute ( { user_id : 'non-existing-id' , name : 'Test' , email : 'test@example.com' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; } ) ;