import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { Depouillement, ResultatCourse } from './depouillement';
import { environment } from 'src/environments/environment.development';

describe('Depouillement', () => {
  let service: Depouillement;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({ imports: [HttpClientTestingModule] });
    service = TestBed.inject(Depouillement);
    httpMock = TestBed.inject(HttpTestingController);
  });

  afterEach(() => httpMock.verify());

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should POST resultat to depouillement endpoint', () => {
    const payload: ResultatCourse = {
      id: 12,
      course: { id: '1', hippodrome: undefined, reunionNumero: 0, reunionDate: '', nom: 'C1', numero: 1, heureDepartPrevue: '', discipline: '', distanceMetres: 0, categorie: '', nombrePartants: 0, statut: '', annulee: false, reporteeMemeJour: false, reporteeAutreJour: false, incidentTechnique: false, nonPartants: [], typesParisOuverts: [] },
      statut: 0 as any,
      ordreArrivee: '1,2,3',
    } as ResultatCourse;

    service.sendResultat(payload).subscribe((res) => {
      expect(res).toBeTruthy();
      expect(res.id).toEqual(payload.id);
    });

    const req = httpMock.expectOne(environment.apiBaseUrl + '/api/depouillement');
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual(payload);
    req.flush(payload);
  });
});
