text: const MissingParamError = require('../../utils/errors/missing-param-error')
  
  const LoadUserByEmailRepository = require('./load-user-by-email-repository')
  const MongoHelper = require('../helpers/mongo-helper')
  
  let userModel
  
  const makeSut = () => {
    return new LoadUserByEmailRepository()
  }
  
  describe('Load User by Email Repository', () => {
    beforeAll(async () => {
      await MongoHelper.connect(process.env.MONGO_URL)
      userModel = await MongoHelper.getCollection('users')
    })
  
    beforeEach(async () => {
      await userModel.deleteMany()
    })
  
    afterAll(async () => {
      await MongoHelper.disconnect()
    })
    it('Should return null if user is not found', async () => {
      const sut = makeSut()
      const user = await sut.load('invalid_email@mail.com')
      expect(user).toBeNull()
    })
  
    it('Should return an user if user is found', async () => {
      const sut = makeSut()
      const fakeUser = await userModel.insertOne({
        email: 'valid_email@mail.com',
        name: 'any_name',
        age: 50,
        state: 'any_state',
        password: 'hashed_password'
      })
      const user = await sut.load('valid_email@mail.com')
      // ** database return is a object with ops atribute which contains an array with the return data
      const { _id, password } = fakeUser.ops[0]
      expect(user).toEqual({ _id, password })
    })
  
    it('Should throw error if email is not provided', async () => {
      const sut = makeSut()
      const promise = sut.load()
      expect(promise).rejects.toThrow(new MissingParamError('email'))
    })
  })
  