import {getSons, getVideos} from './mediaAnnuaireService';
import http from '@/http';

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

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

  it('shouldLoadTheSoundsOfTheEntry', async () => {
    const sons = [{asoId: 1, titre: 'Gavotte', taille: 1200, ordre: 0, url: 'https://media.tamm-kreiz.bzh/gavotte.mp3'}];
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {'hydra:member': sons}});

    expect(await getSons(42)).toEqual(sons);
    expect(mockedHttp.get).toHaveBeenCalledWith('/annuaire_entrees/42/sons');
  });

  it('shouldLoadTheVideosOfTheEntry', async () => {
    const videos = [{aviId: 1, titre: 'Fest-noz', sousTitre: null, credit: null, youtubeId: 'abc', url: 'https://www.youtube.com/watch?v=abc', embedUrl: 'https://www.youtube.com/embed/abc', ordre: 0}];
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {'hydra:member': videos}});

    expect(await getVideos('42')).toEqual(videos);
    expect(mockedHttp.get).toHaveBeenCalledWith('/annuaire_entrees/42/videos');
  });

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

    expect(await getSons(42)).toEqual([]);
    expect(await getVideos(42)).toEqual([]);
  });
});
