import AppError from '@shared/errors/AppError' ; import FakeStorageProvider from '@shared/container/providers/StorageProvider/fakes/FakeStorageProvider' ; import FakeUsersRepository from '../repositories/fakes/FakeUsersRepository' ; import UpdateUserAvatarService from './UpdateUserAvatarService' ; describe ( 'UpdateUserAvatar' , ( ) => { let fakeUsersRepository ; let fakeStorageProvider ; let updateUserAvatar ; beforeEach ( ( ) => { fakeUsersRepository = new FakeUsersRepository ( ) ; fakeStorageProvider = new FakeStorageProvider ( ) ; updateUserAvatar = new UpdateUserAvatarService ( fakeUsersRepository , fakeStorageProvider ) ; } ) ; it ( 'should be able to update avatar of user' , async ( ) => { const user = await fakeUsersRepository . create ( { name : 'Thiago Marinho' , email : 'tgmarinho@gmail.com' , password : '123456' , } ) ; await updateUserAvatar . execute ( { user_id : user . id , avatarFilename : 'avatar.jpg' , } ) ; expect ( user . avatar ) . toBe ( 'avatar.jpg' ) ; } ) ; it ( 'should not be able to update avatar from non existing user' , async ( ) => { expect ( updateUserAvatar . execute ( { user_id : 'non-existing-user' , avatarFilename : 'avatar.jpg' , } ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; it ( 'should delete old avatar when updating new one' , async ( ) => { const deleteFile = jest . spyOn ( fakeStorageProvider , 'deleteFile' ) ; const user = await fakeUsersRepository . create ( { name : 'Thiago Marinho' , email : 'tgmarinho@gmail.com' , password : '123456' , } ) ; await updateUserAvatar . execute ( { user_id : user . id , avatarFilename : 'avatar.jpg' , } ) ; await updateUserAvatar . execute ( { user_id : user . id , avatarFilename : 'avatar2.jpg' , } ) ; expect ( deleteFile ) . toHaveBeenCalledWith ( 'avatar.jpg' ) ; expect ( user . avatar ) . toBe ( 'avatar2.jpg' ) ; } ) ; } ) ;