import {hitVisite} from './visiteService';
import http from '@/http';

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

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

  it('shouldCountAVisitAndReturnTheNewTotal', async () => {
    mockedHttp.post.mockResolvedValueOnce({status: 200, data: {tableCible: 'annuaire_entree', cibleId: 42, nb: 8}});

    expect(await hitVisite('annuaire_entree', 42)).toBe(8);
    expect(mockedHttp.post).toHaveBeenCalledWith('/visites/annuaire_entree/42');
  });

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

    expect(await hitVisite('annuaire_entree', 42)).toBeNull();
  });

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

    expect(await hitVisite('geolocale_ville', 7)).toBeNull();
  });
});
