import AppError from '@shared/errors/AppError' ; import FakeCacheProvider from '@shared/container/providers/CacheProvider/implementations/fakes/FakeCacheProvider' ; import FakeHashProvider from '../providers/HashProvider/fakes/FakeHashProvider' ; import FakeUsersRepository from '../repositories/fakes/FakeUsersRepository' ; import CreateUserService from './CreateUserService' ; describe ( 'CreateUser' , ( ) => { let fakeUsersRepository ; let fakeHashProvider ; let fakeCacheProvider ; let createUserService ; beforeEach ( ( ) => { fakeUsersRepository = new FakeUsersRepository ( ) ; fakeHashProvider = new FakeHashProvider ( ) ; fakeCacheProvider = new FakeCacheProvider ( ) ; createUserService = new CreateUserService ( fakeUsersRepository , fakeHashProvider , fakeCacheProvider ) ; } ) ; it ( 'should be able to create a new user' , async ( ) => { const user = await createUserService . execute ( { name : 'Thiago Marinho' , email : 'tgmarinho@gmail.com' , password : '123456' , } ) ; expect ( user ) . toHaveProperty ( 'id' ) ; expect ( user ) . toHaveProperty ( 'password' ) ; expect ( user . email ) . toBe ( 'tgmarinho@gmail.com' ) ; } ) ; it ( 'should not be able to create new user with the same email from another' , async ( ) => { const inputUser = { name : 'Thiago Marinho' , email : 'tgmarinho@gmail.com' , password : '123456' , } ; await createUserService . execute ( inputUser ) ; await expect ( createUserService . execute ( inputUser ) ) . rejects . toBeInstanceOf ( AppError ) ; } ) ; } ) ;