import DataProvider from "~/services/data/dataProvider";
import {useAddressPostalUtils} from "~/composables/data/useAddressPostalUtils";
jest.mock('~/services/data/dataProvider')

let useAddressPostalUtilsMount:any
const dataproviderMock = DataProvider as jest.Mocked<typeof DataProvider>

beforeAll(() => {
  useAddressPostalUtilsMount = useAddressPostalUtils(dataproviderMock.prototype)
})

describe('searchFunction()', () => {
  const apiResponse = {
    data:{
      features: [
        {
          properties: {
            id: 1, postcode: '00001', city: 'foo'
          }
        },
        {
          properties: {
            id: 2, postcode: '00002', city: 'bar'
          }
        }
      ]
    }
  }

  it('should return [] if no research params', async () => {
    const result = await useAddressPostalUtilsMount.searchFunction(null, 'field')
    expect(result).toHaveLength(0)
  })

  it('should return response with default postcode value', async () => {
    dataproviderMock.prototype.invoke = jest.fn().mockReturnValue(apiResponse)
    const result = await useAddressPostalUtilsMount.searchFunction('foo', 'postalCode')
    expect(result).toStrictEqual([
      {id: 0, postcode: 'foo', city: null},
      {id: 1, postcode: '00001', city: 'foo'},
      {id: 2, postcode: '00002', city: 'bar'}
    ])
  })

  it('should return response with default city value', async () => {
    dataproviderMock.prototype.invoke = jest.fn().mockReturnValue(apiResponse)
    const resultCity = await useAddressPostalUtilsMount.searchFunction('foo', 'addressPostal.addressCity')
    expect(resultCity).toStrictEqual([
      {id: 0, postcode: null, city: 'foo'},
      {id: 1, postcode: '00001', city: 'foo'},
      {id: 2, postcode: '00002', city: 'bar'}
    ])
  })
})
