import {getSoutiens} from './soutienService';
import http from '@/http';

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

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

  it('shouldFetchEveryActiveSupporter', async () => {
    const soutiens = [{souId: 1, souNom: 'Région Bretagne', souUrl: 'https://bretagne.bzh', souDescription: null, souPiedDePage: true, logoUrl: 'https://pimcore.test/region.png'}];
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: soutiens});

    expect(await getSoutiens()).toEqual(soutiens);
    expect(mockedHttp.get).toHaveBeenCalledWith('/soutiens');
  });

  it('shouldFetchOnlyTheFooterSupportersWhenAsked', async () => {
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: []});

    await getSoutiens(true);

    expect(mockedHttp.get).toHaveBeenCalledWith('/soutiens?piedDePage=1');
  });

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

    expect(await getSoutiens(true)).toEqual([]);
  });
});
