import {getArticlesLies} from './articleLienService';
import http from '@/http';

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

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

  it('shouldLoadThePublishedArticlesLinkedToAFiche', async () => {
    const articles = [{id: 1, title: 'Les sonneurs de couple', slug: 'sonneurs-de-couple', image: null, createdAt: '2026-09-01T10:00:00+02:00'}];
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: articles});

    expect(await getArticlesLies('annuaire_entree', 42)).toEqual(articles);
    expect(mockedHttp.get).toHaveBeenCalledWith('/articles_lies/annuaire_entree/42');
  });

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

    expect(await getArticlesLies('evenement', 72)).toEqual([]);
  });

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

    expect(await getArticlesLies('formation_stage', 1521)).toEqual([]);
  });
});
