import VueI18n from 'vue-i18n'
import {useValidator} from '~/composables/form/useValidator'
import DataProvider from '~/services/data/dataProvider'
jest.mock('~/services/data/dataProvider')

let useValidatorMount:any
const DataProviderMock = DataProvider as jest.MockedClass<typeof DataProvider>
const VueI18nMock = VueI18n as jest.MockedClass<typeof VueI18n>

beforeAll(() => {
  useValidatorMount = useValidator(DataProviderMock.prototype, VueI18nMock.prototype)
})

describe('useHandleSiret()', () => {
  beforeEach(() => {
    // Efface toutes les instances et les appels au constructeur et à toutes les méthodes :
    DataProviderMock.mockClear()
  })

  it('should init 3 parameters correctly', async () => {
    const siretResponse = useValidatorMount.useHandleSiret()
    expect(siretResponse.siretErrorMessage.value).toEqual('')
    expect(siretResponse.siretError.value).toBeFalsy()
    expect(siretResponse.checkSiret).toBeDefined()
  })

  it('should update parameters if Siret is correct', async () => {
    const siretResponse = useValidatorMount.useHandleSiret()
    DataProviderMock.prototype.invoke.mockImplementation(async () => {
      return { isCorrect: true }
    })
    await siretResponse.checkSiret('123456')
    expect(siretResponse.siretErrorMessage.value).toEqual('')
    expect(siretResponse.siretError.value).toBeFalsy()
  })

  it('should update parameters if Siret is not correct', async () => {
    const siretResponse = useValidatorMount.useHandleSiret()
    DataProviderMock.prototype.invoke.mockImplementation(async () => {
      return { isCorrect: false }
    })
    VueI18nMock.prototype.t = jest.fn().mockReturnValue('siret_error')
    await siretResponse.checkSiret('123456')
    expect(siretResponse.siretErrorMessage.value).toEqual('siret_error')
    expect(siretResponse.siretError.value).toBeTruthy()
  })

  it('should update parameters if there is no response during the check siret', async () => {
    const siretResponse = useValidatorMount.useHandleSiret()
    DataProviderMock.prototype.invoke.mockImplementation(jest.fn())
    await siretResponse.checkSiret('123456')
    expect(siretResponse.siretErrorMessage.value).toEqual('')
    expect(siretResponse.siretError.value).toBeFalsy()
  })
})
