import http from '@/http';
import {repondreAnnonce, updateAnnonce} from './annonceService';

const mockPatch = http.patch as jest.Mock;
const mockPost = http.post as jest.Mock;

describe('annonceService — modification et réponse', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('shouldPatchOnlyTheGivenFieldsOfAnAd', async () => {
    mockPatch.mockResolvedValue({data: {id: 7, titre: 'Nouveau titre', texte: 'Texte', typeId: 2, date: '2026-09-18', auteur: {id: 1, pseudo: 'anna'}}});

    const annonce = await updateAnnonce(7, {titre: 'Nouveau titre', typeId: 2});

    expect(mockPatch).toHaveBeenCalledWith('/annonces/7', {titre: 'Nouveau titre', typeId: 2});
    expect(annonce.titre).toBe('Nouveau titre');
  });

  it('shouldPostAReplyMessageToTheAd', async () => {
    mockPost.mockResolvedValue({data: {message: 'Message sent'}});

    await repondreAnnonce(7, "J'ai une bombarde");

    expect(mockPost).toHaveBeenCalledWith('/annonces/7/reponses', {message: "J'ai une bombarde"});
  });
});
