import {getListe, getListeEntrees} from './listeService';
import http from '@/http';

const mockedHttp = http as jest.Mocked<typeof http>;

describe('listeService', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('shouldLoadTheListWithItsDomaine', async () => {
    const bagadou = {lieId: 12, lieNom: 'Bagadoù', lieDpr: {dprNom: 'Musique traditionnelle'}};
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: bagadou});

    expect(await getListe('12')).toEqual(bagadou);
    expect(mockedHttp.get).toHaveBeenCalledWith('/socle_liste_entrees/12');
  });

  it('shouldLoadTheEntriesOfTheListSortedByLabel', async () => {
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {'hydra:member': [{aenId: 1, aenLibelle: 'Bagad Kemper'}]}});

    expect(await getListeEntrees('12')).toEqual([{aenId: 1, aenLibelle: 'Bagad Kemper'}]);
    expect(mockedHttp.get).toHaveBeenCalledWith('/annuaire_entrees?socleListeEntrees=12&aenActive=1&itemsPerPage=200&order[aenLibelle]=asc');
  });

  it('shouldReturnNoEntriesWhenTheApiFails', async () => {
    mockedHttp.get.mockRejectedValueOnce(new Error('HTTP 500'));

    expect(await getListeEntrees('12')).toEqual([]);
  });
});
