import {getFichiers} from './entityFilesService';
import http from '@/http';

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

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

  it('shouldLoadTheFilesAttachedToAnEventIncludingItsEnsembles', async () => {
    const files = [
      {medId: 1, url: 'https://media.tamm-kreiz.bzh/programme.pdf', filename: 'programme_ab12cd34.pdf', label: 'Programme', mimetype: 'application/pdf', source: 'own'},
      {medId: 2, url: 'https://media.tamm-kreiz.bzh/plan.pdf', filename: 'plan.pdf', label: null, mimetype: 'application/pdf', source: 'ensemble:5'},
    ];
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {files}});

    expect(await getFichiers('evenement', 9)).toEqual(files);
    expect(mockedHttp.get).toHaveBeenCalledWith('/entity-files/evenement/9');
  });

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

    expect(await getFichiers('evenement', 9)).toEqual([]);
  });
});
