import { Controller , SavePictureController } from '@/application/controllers' ; import { AllowedMimeTypes , MaxFileSize , Required , RequiredBuffer } from '@/application/validation' ; describe ( 'SavePictureController' , ( ) => { let buffer ; let mimeType ; let file ; let userId ; let sut ; let changeProfilePicture ; beforeAll ( ( ) => { buffer = Buffer . from ( 'any_buffer' ) ; mimeType = 'image/png' ; file = { buffer , mimeType } ; userId = 'any_user_id' ; changeProfilePicture = jest . fn ( ) . mockResolvedValue ( { initials : 'any_initials' , pictureUrl : 'any_url' } ) ; } ) ; beforeEach ( ( ) => { sut = new SavePictureController ( changeProfilePicture ) ; } ) ; it ( 'should extend Controller' , async ( ) => { expect ( sut ) . toBeInstanceOf ( Controller ) ; } ) ; it ( 'should build Validators correctly on save' , async ( ) => { const validators = sut . buildValidators ( { file , userId } ) ; expect ( validators ) . toEqual ( [ new Required ( file , 'file' ) , new RequiredBuffer ( buffer , 'file' ) , new AllowedMimeTypes ( [ 'png' , 'jpg' ] , mimeType ) , new MaxFileSize ( 5 , buffer ) ] ) ; } ) ; it ( 'should build Validators correctly on delete' , async ( ) => { const validators = sut . buildValidators ( { file : undefined , userId } ) ; expect ( validators ) . toEqual ( [ ] ) ; } ) ; it ( 'should call ChangeProfilePicture with correct input' , async ( ) => { await sut . handle ( { file , userId } ) ; expect ( changeProfilePicture ) . toHaveBeenCalledWith ( { id : userId , file } ) ; expect ( changeProfilePicture ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should return 200 with valid data' , async ( ) => { const httpResponse = await sut . handle ( { file , userId } ) ; expect ( httpResponse ) . toEqual ( { statusCode : 200 , data : { initials : 'any_initials' , pictureUrl : 'any_url' } } ) ; } ) ; } ) ;