// unit test JsonConfigLoader
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator';
import { CORE_JSON_CONFIG_LOADER_URL } from '../tokens';
import { JsonConfigLoader } from './json.config-loader';

describe('JsonConfigLoader', () => {
  let spectator: SpectatorService<JsonConfigLoader>;
  const createService = createServiceFactory({
    imports: [HttpClientTestingModule],
    service: JsonConfigLoader,
    mocks: [],
    providers: [{ provide: CORE_JSON_CONFIG_LOADER_URL, useValue: '/assets/config.json' }],
  });
  let httpTestingController: HttpTestingController;

  beforeEach(() => {
    spectator = createService();
    httpTestingController = spectator.inject(HttpTestingController);
  });

  it('should create', () => {
    expect(spectator.service).toBeTruthy();
  });

  describe('load', () => {
    it('should call the provided url', async () => {
      const reqPromise = spectator.service.load();
      const req = httpTestingController.expectOne('/assets/config.json');
      req.flush({ unit: 'test' });
      const result = await reqPromise;
      httpTestingController.verify();
      expect(result).toEqual({ unit: 'test' });
    });
  });
});
