import {getDomaines} from './domaineService';
import http from '@/http';

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

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

  it('shouldFetchTheDomainsSortedByName', async () => {
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {'hydra:member': [{dprId: 2, dprNom: 'Musique'}, {dprId: 1, dprNom: 'Danse'}]}});

    expect(await getDomaines()).toEqual([{dprId: 1, dprNom: 'Danse'}, {dprId: 2, dprNom: 'Musique'}]);
    expect(mockedHttp.get).toHaveBeenCalledWith('/socle_domaines');
  });

  it('shouldReturnNoDomainWhenTheApiFails', async () => {
    mockedHttp.get.mockRejectedValueOnce(new Error('boom'));

    expect(await getDomaines()).toEqual([]);
  });
});
