import { HttpClient } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { PopularRequestsApiService } from '@app/features/popular-requests/services/api.service';
import { popularRequestsMock } from '@app/features/popular-requests/services/api.service.mock';
import { EndpointService } from '@shared/services/api/endpoint.service';

describe('PopularRequestsApiService', () => {
    let service: PopularRequestsApiService;
    const buildCommonURLMock = jasmine.createSpy('buildCommonURL');
    const getMock = jasmine.createSpy('get');

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                PopularRequestsApiService,
                {
                    provide: EndpointService,
                    useValue: {
                        buildCommonURL: buildCommonURLMock,
                    },
                },
                {
                    provide: HttpClient,
                    useValue: {
                        get: getMock,
                    },
                },
            ],
        });

        service = TestBed.inject(PopularRequestsApiService);
        buildCommonURLMock.calls.reset();
        getMock.calls.reset();
    });

    it('should return promise resolved with popular requests', async () => {
        const url = '/api/Requests/1/getpopular';
        buildCommonURLMock
            .withArgs('getpopular', '1', 'Requests')
            .and.returnValue(url);
        getMock.withArgs(url).and.returnValue({
            toPromise: () => Promise.resolve(popularRequestsMock),
        });

        const result = await service.getPopularRequests();
        expect(result).toEqual(popularRequestsMock);
    });
});
