/*
 * Copyright (C) 2018 Robert Bosch GmbH Copyright (C) 2018 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { CoreConfig } from './core-config';
import { CoreConfigurationLoaderService } from './core-configuration-loader.service';
import { CoreConfigurationService } from './core-configuration.service';

describe('CoreConfigurationService', () => {
  let service: CoreConfigurationService;
  let loaderConfigPromise: Promise<CoreConfig>;
  let loaderConfigPromiseResolve: (value?: PromiseLike<CoreConfig> | CoreConfig) => void;

  const mockConfig = {
    environment: 'dev',
    endpointNavigation: 'foo',
  };

  beforeEach(() => {
    loaderConfigPromise = new Promise<CoreConfig>((resolve) => (loaderConfigPromiseResolve = resolve));
    TestBed.configureTestingModule({
      providers: [
        {
          provide: CoreConfigurationLoaderService,
          useValue: {
            config: loaderConfigPromise,
          },
        },
      ],
    });
    service = TestBed.inject(CoreConfigurationService);
  });

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

  it('should have promise for config', fakeAsync(() => {
    expect(service.configPromise).not.toBeNull();
  }));

  it('should have resolved promise after loading', fakeAsync(() => {
    let resolvedConfig = null;
    service.configPromise.then((config) => (resolvedConfig = config));

    loaderConfigPromiseResolve(mockConfig);
    tick();

    expect(resolvedConfig).toEqual(mockConfig);
  }));
});
